spring学习笔记(8)--xml annotation

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.@Autowired

a)     默认按类型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 包括了 AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessorPersistenceAnnotationBeanPostProcessor,也包括了前面提到的 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.java
@Autowired
	public void setImpl(IUserDAO impl) {
		this.impl = impl;
	}
UserservicesTest.java
@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 的依赖

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值