And hello to u too:) Click the icon above for more detail

Java On Call 6

  1. Java基础知识
  2. 面向对象
  3. 常用API
  4. 集合I/O
  5. 多线程、网络编程、反射、设计模式

这是准备java面试的第二天,以上的分类的思维导图,来自->这里

Notes of this video

package com.taoma.TestMaven2;

import org.springframework.stereotype.Component;

@Component
public class Car implements Vehicle {

	public void drive() {
		
		System.out.println("I am TaoMa");
		
	}

}

那么此时相应的在xml文件中就应该加上这一行并且把刚刚定义的bean给删除来告诉框架现在使用这种方法来声明

<context:component-scan base-package="com.taoma.TestMaven2"></context:component-scan>
package com.taoma.TestMaven2;

import org.springframework.stereotype.Component;

@Component
public class Car implements Vehicle {

	Private String brand;

	public void drive() {
		
		System.out.println("I am TaoMa");
		
	}

}

注入这个变量,并且可以定义这个值,这就是这个bean的参数(property),使用这个property且定义他的参数就像是用setter一样

	<bean id="vehicle" class="com.taoma.TestMaven2.car">
		<property name="brand" value="Tao"></property>
	</bean>
	<bean id="vehicle" class="com.taoma.TestMaven2.car">
		<constructor-arg value="Tao"></constructor-arg>
	</bean>
package com.baobaotao;   
  
public class Boss {   
    private Car car;   
    private Office office;   
  
    // 省略 get/setter   
  
    @Override  
    public String toString() {   
        return "car:" + car + "/n" + "office:" + office;   
    }   
}   

//-------------------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>   
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">   
    <bean id="boss" class="com.baobaotao.Boss">   
        <property name="car" ref="car"/>   
        <property name="office" ref="office" />   
    </bean>   
    <bean id="office" class="com.baobaotao.Office">   
        <property name="officeNo" value="002"/>   
    </bean>   
    <bean id="car" class="com.baobaotao.Car" scope="singleton">   
        <property name="brand" value=" 红旗 CA72"/>   
        <property name="price" value="2000"/>   
    </bean>   
</beans> 

package com.baobaotao;   
  
public class Boss {   
	@Autowired
    private Car car;   
	@Autowired
    private Office office;   
  
    @Override  
    public String toString() {   
        return "car:" + car + "/n" + "office:" + office;   
    }   
}   

//-------------------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>   
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">   
    <bean id="boss" class="com.baobaotao.Boss"></bean>   
    <bean id="office" class="com.baobaotao.Office">   
        <property name="officeNo" value="002"/>   
    </bean>   
    <bean id="car" class="com.baobaotao.Car" scope="singleton">   
        <property name="brand" value=" 红旗 CA72"/>   
        <property name="price" value="2000"/>   
    </bean>   
</beans> 

把getter和setter可以去掉,并且加上注释。这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。

package com.taoma.TestMaven2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.configuration;

@Configuration
public class AppConfig
{
	@Bean
	public Samsung getPhone(){return new Samsung();}
}

//-----------------------------
ApplicationContext context = new ClassPathXmlApplicationContext(AppCongig.class);
    	 
Samsung newCar = (Samsung)context.getBean(Samsung.class);

所以这样定义就方便很多了,在配置类写bean,用@Configuration来声明配置类,用@Bean来声明bean的方法,像上面那样