当前位置 博文首页 > 这行代码没Bug:Spring学习03
自动装配是使用spring满足bean依赖的一种方法
spring会在应用上下文中为某个bean寻找其依赖的bean。
Spring中bean的三种装配机制:
在xml中显式配置;
在java中显式配置;
隐式的bean发现机制和自动装配。
这里主要看:自动化的装配bean。
Spring的自动装配需要从两个角度来实现,或者说是两个操作:
组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;
自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;
组件扫描和自动装配组合发挥巨大威力,使得显示的配置降低到最少。
1、新建一个项目
2、新建两个实体类,Cat Dog 都有一个叫的方法
public class Cat { public void shout() { System.out.println("miao~"); } } public class Dog { public void shout() { System.out.println("wang~"); } }
3、新建一个用户类 User
public class User { private Cat cat; private Dog dog; private String str; }
4、编写Spring配置文件
<?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="dog" class="com.chen.pojo.Dog"/> <bean id="cat" class="com.chen.pojo.Cat"/> ? <bean id="user" class="com.chen.pojo.User"> <property name="cat" ref="cat"/> <property name="dog" ref="dog"/> <property name="str" value="chen"/> </bean> </beans>
5、测试
public class MyTest { @Test public void testMethodAutowire() { ApplicationContext context = newClassPathXmlApplicationContext("beans.xml"); User user = (User) context.getBean("user"); user.getCat().shout(); user.getDog().shout(); } }
autowire byName (按名称自动装配)
由于在手动配置xml过程中,常常发生字母缺漏和大小写等错误,而无法对其进行检查,使得开发效率降低。
采用自动装配将避免这些错误,并且使配置简单化。
测试:
1、修改bean配置,增加一个属性 autowire="byName"
<bean id="user" class="com.chen.pojo.User" autowire="byName"> <property name="str" value="chen"/> </bean>
2、再次测试,结果依旧成功输出!
3、我们将 cat 的bean id修改为 cat11
4、再次测试, 执行时报空指针 java.lang.NullPointerException 。因为按byName规则找不对应set方法,真正的setCat就没执行,对象就没有初始化,所以调用时就会报空指针错误。
小结:
当一个bean节点带有 autowire byName的属性时。
将查找其类中所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。
去spring容器中寻找是否有此字符串名称id的对象。
如果有,就取出注入;如果没有,就报空指针异常。
autowire byType (按类型自动装配)
使用autowire byType首先需要保证:同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常 NoUniqueBeanDefinitionException 。
测试:
1、将user的bean配置修改一下 : autowire="byType"
2、测试,正常输出
3、在注册一个cat 的bean对象!
<bean id="dog" class="com.chen.pojo.Dog"/> <bean id="cat" class="com.chen.pojo.Cat"/> <bean id="cat2" class="com.chen.pojo.Cat"/> ? <bean id="user" class="com.chen.pojo.User" autowire="byType"> <property name="str" value="chen"/> </bean>
4、测试,报错:NoUniqueBeanDefinitionException
5、删掉cat2,将cat的bean名称改掉!测试!因为是按类型装配,所以并不会报异常,也不影响最后的结果。甚至将id属性去掉,也不影响结果。
这就是按照类型自动装配。
使用注解
jdk1.5开始支持注解,spring2.5开始全面支持注解。
准备工作:利用注解的方式注入属性。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> ?