byType 根据属性类型自动装配。BeanFactory查找容器中的全部Bean,如果正好有一个与依赖属性类型相同的Bean,就自动装配这个属性;如果有多个这样的Bean,Spring无法决定注入哪个Bean,就抛出一个致命异常;如果没有匹配的Bean,就什么都不会发生,属性不会被设置
byName 根据属性名自动装配。BeanFactory查找容器中的全部Bean,找出id与属性的setter方法入参匹配的Bean。找到即自动注入,否则什么都不做
constructor 与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的Bean,那么将会抛出异常
一、xml配置
byName:
spring-config.xml:
<?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.xsd">
<!--根据byName类型识别,id要取名为属性名就可以自动装配 -->
<bean id="tool" class="com.wance.entity.Plane">
<!--<property name="message" value="Hello Spring"/>-->
</bean>
<!--自动装配 -->
<bean class="com.wance.entity.SuperMan" id="sm" autowire="byName">
</bean>
</beans>
byType:
spring-config.xml:
<?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.xsd">
<bean id="tool" class="com.wance.entity.Plane">
<!--<property name="message" value="Hello Spring"/>-->
</bean>
<bean class="com.wance.entity.SuperMan" id="sm" autowire="byType">
</bean>
</beans>
constructor:
<?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.xsd">
<bean id="tool" class="com.wance.entity.Plane">
<!--<property name="message" value="Hello Spring"/>-->
</bean>
<!--构造器默认会先使用byName -->
<bean class="com.wance.entity.SuperMan" id="sm" autowire="constructor">
</bean>
</beans>
二、注解注入
plane.java:
//如果不写,名字默认是类名首字母小写
@Component("f1")
@Scope("prototype")
public class Plane implements Fly {
public Plane() {
System.out.println("飞机被创建");
}
@Override
public void fly() {
System.out.println("飞机飞");
}
}
HotBallon.java:
@Component
public class HotBallon implements Fly {
@Override
public void fly() {
System.out.println("热气球在飞");
}
}
SuperMan.java:
//超人可以通过工具飞,也可以自己飞
@Component
public class SuperMan implements Fly {
//自动装配
@Autowired
@Qualifier("hotBallon")
private Fly tool;//持有抽象属性
@Value("king")
private String name;
public SuperMan() {
}
public SuperMan(Fly tool,String name){
this.tool=tool;
this.name=name;
}
//构造时,指定工具
public SuperMan(Fly tool){
this.tool=tool;
}
public void setTool(Fly tool) {
this.tool = tool;
}
public void setName(String name) {
this.name = name;
}
@Override
public void fly() {
if (tool==null){
System.out.println("超人"+name+"自己飞");
}else {
System.out.println("超人"+name+"通过");
tool.fly();//工具飞行
}
}
@PostConstruct
public void init(){
System.out.println("超人完成初始化");
}
@PreDestroy
public void destor(){
System.out.println("超人完成销毁");
}
}
测试:
@Test
public void test2(){
ApplicationContext factory=new ClassPathXmlApplicationContext("spring-config.xml");
SuperMan p1=factory.getBean("superMan",SuperMan.class);
p1.fly();
}
结果:
超人完成初始化
超人king通过
热气球在飞