1.使用annotation的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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> </beans>
2.@Autowireda) 默认按类型by type
b) 如果想用byName,使用@Qulifier
c) 写在private field(第三种注入形式)(不建议,破坏封装)
d) 如果写在set上,@qualifier需要写在参数上
xmlns为xml的命名空间(namespace)
xmlns:context="http://www.springframework.org/schema/context"
表明context节点在http://www.springframework.org/schema/context 上寻找
隐式注册 post-processors 包括了
AutowiredAnnotationBeanPostProcessor
,CommonAnnotationBeanPostProcessor
,PersistenceAnnotationBeanPostProcessor
,也包括了前面提到的RequiredAnnotationBeanPostProcessor
。)
AutowiredAnnotationBeanPostProcessor:默认的注入方式byType
如果bean.xml中有两个同样类型的bean,将会报错org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userservice': Autowiring of methods failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.sh.spring.Services.UserServices.setImpl(org.sh.spring.DAO.IUserDAO); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.sh.spring.DAO.IUserDAO] is defined: expected single matching bean but found 2: [u, u1]bean.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> <bean id="u" class="org.sh.spring.impl.IUserDAOImpl"> <property name="daoId" value="1"></property> </bean> <bean id="userservice" class="org.sh.spring.Services.UserServices" init-method="init" destroy-method="destory"> </bean> <!-- more bean definitions go here --> </beans>
Userservices.javaUserservicesTest.java@Autowired public void setImpl(IUserDAO impl) { this.impl = impl; }
@Test public void testSave() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserServices ud = (UserServices)ctx.getBean("userservice"); //UserServices ud1 = (UserServices)ctx.getBean("userservice"); System.out.println(ud.getImpl()); User u = new User(); ud.save(u); ctx.destroy(); }
测试结果:user saved 测试成功2.如果bean.xml中一定要有两个一样类型的bean 解决方法如下@Autowired public void setImpl(@Qualifier(value="u1") IUserDAO impl) { this.impl = impl; }
@Qualifier 可以指定使用哪个名字的那个bean
测试结果:user saved 测试成功@Autowired(required=false):缺省情况下,当出现0个候选的 beans时自动连接将失败;缺省行为把连接方法,构造器,字段假设为 required 的依赖