spring注入方式之AutoWired注入

注解方式有两种(注解在变量上和注解在方法上)
DAO
public class PersonDao {
	public void add(){
		System.out.println("dao的add方法");
	}
}

1.Service(注解在变量上)
public class Service {

	/*
	 * @Autowired注解
	 *   * 默认按类型注入,如果类型不匹配抛出异常
	 *   * 该注解能应用到字段上和属性的set方法上(两种方式)
	 *   
	 *   当该注解应用到字段上(spring容器已经初始化)
	 *      * 注解解析器获取@Autowired注解标注(private PersonDao personDao;)的变量的类型.看类型是com.xxc.iocc.autoWiredField.dao.PersonDao
	 *      
	 *      * 以该类型为查找条件到spring容器中去查找类型为com.xxc.iocc.autoWiredField.dao.PersonDao的bean节点
	 *              <bean id="personDao" class="com.xxc.iocc.autoWiredField.dao.PersonDao"></bean>
	 *              
	 *      * 反射该节点的,并将实例赋给 private PersonDao personDao变量    
	 *      
	 *      * @Autowired配合 @Qualifier("personDao") 可以按bean的id属性匹配来进行注入,@Qualifier不能单独使用
	 */
	
	@Autowired @Qualifier("personDao22")
	private PersonDao personDao;
	
	public void add(){
		System.out.println("service的add方法");
		personDao.add();
	}
}


2.Service注解在方法上
public class Service {

	private PersonDao personDao;
	
	
	public PersonDao getPersonDao() {
		return personDao;
	}

	
	/*
	 * @Autowired注解
	 *   * 默认按类型注入,如果类型不匹配抛出异常
	 *   * 该注解能应用到字段上和属性的set方法上(两种方式)
	 *   
	 *   当该注解应用到方法上(spring容器已经初始化)
	 *      * 注解解析器获取@Autowired注解setPersonDao(PersonDao personDao),获取方法的参数类型
	 *      
	 *      * 以该参数类型为查找条件到spring容器中去查找类型为com.xxc.iocc.autoWiredMethod.dao.PersonDao的bean节点
	 *              <bean id="personDao" class="com.xxc.iocc.autoWiredMethod.dao.PersonDao"></bean>
	 *              
	 *      * 以bean节点的类型的实例对象作为实参传递给setPersonDao(PersonDao personDao)的形参进行注入    
	 *      
	 *      * @Qualifier("personDao") 可以使用该标记按名称匹配,但是位置要放在形参前面
	 */
	@Autowired 
	public void setPersonDao(@Qualifier("personDao22")PersonDao personDao) {
		this.personDao = personDao;
	}

	public void add(){
		System.out.println("service的add方法");
		personDao.add();
	}
}


applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
		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
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<!-- 使用基于注解的进行注入步骤如下
              * 引入context命名空间 
                  xmlns:context="http://www.springframework.org/schema/context"
                                 http://www.springframework.org/schema/context 
		                         http://www.springframework.org/schema/context/spring-context-2.5.xsd
		      
		      * 注册对注解进行解析的处理器     
		          context:annotation-config              
        
     -->
     
	<context:annotation-config/>
	
	<bean id="service" class="com.xxc.iocc.autoWiredField.service.Service"/> 
	<bean id="personDao22" class="com.xxc.iocc.autoWiredField.dao.PersonDao"/>
</beans>

测试类
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/iocc/autoWiredField/applicationContext.xml");
		Service service = (Service)ac.getBean("service");
		service.add();
	}
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值