(7) 用@Resource注解完成属性装配 【依赖注入----手动装配】 以及 编码解析其原理【经典】

      手工装配依赖对象,在这种方式中又有两种编程方式

1. 在xml配置文件中,通过在bean节点下配置,如
   <bean id="orderService" class="cn.itcast.service.OrderServiceBean">
          <constructor-arg index=“0” type=“java.lang.String” value=“xxx”/>//构造器注入
           <property name=“name” value=“zhao/>//属性setter方法注入
    </bean>
2. 在java代码中使用@Autowired或@Resource注解方式进行装配。但我们需要在xml配置文件中配置以下信息:
<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>

这个配置隐式注册了多个对注释进行解析处理的处理器:AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
      注: @Resource注解在spring安装目录的lib\j2ee\common-annotations.jar



       在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。

(1)

    @Autowired
    private PersonDao  personDao;//用于字段上
    @Autowired
    public void setOrderDao(OrderDao orderDao) {//用于属性的setter方法上
        this.orderDao = orderDao;
    }
@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
    @Autowired  @Qualifier("personDaoBean")

         private PersonDao  personDao;


(2)

@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。

      名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。

    @Resource(name=“personDaoBean”)
    private PersonDao  personDao;//用于字段上


注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。



参照代码验证:实践出真知


<?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="personDao" class="cn.itm.dao.impl.PersonDaoBean"></bean>
           <bean id="personService" class="cn.itm.service.impl.PersonServiceBean">
           
           		 <!-- 
			           <constructor-arg index="0" type="cn.itm.dao.PersonDao"/>
			           <constructor-arg index="1" value="大家好。"></constructor-arg>
           		 -->
           </bean>
   
</beans>


2,PersonServiceBean.java


package cn.itm.service.impl;

import javax.annotation.Resource;

import cn.itm.dao.PersonDao;
import cn.itm.service.PersonService;

public class PersonServiceBean implements PersonService{

	// 首先获取,personDao的名称,然后 在SPring容器里面,寻找与此名称所匹配的bean,如果找到 就把 这个bean 注入到这个字段上来。
	@Resource private PersonDao personDao;
	private String name;
	
	public PersonServiceBean(){}
	
	public PersonServiceBean(PersonDao personDao, String name) {
		this.personDao = personDao;
		this.name = name;
	}

	
	public void save(){
		personDao.add();
	}
	
}


package cn.itm.dao.impl;

import cn.itm.dao.PersonDao;


public class PersonDaoBean implements PersonDao {

	
	public void add(){
		System.out.println("执行PersonDaoBean的add方法。。。");
	}
}

package cn.itm.dao;

public interface PersonDao {

	public abstract void add();

}

package cn.itm.service;


public interface PersonService {

	public void save();
}

package junit.test;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itm.service.PersonService;

public class SpringTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	// 专门用来实例化  Spring 容器的。
	@Test public void instanceSpring(){
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService) ctx.getBean("personService");
		personService.save();
	}
}

可以正常运行,通过。


编码解析其原理:

1,主要实现的方法:

/**
	 * 实现注解的方式  注入:
	 * 
	 */
	private void annotationInject() {
		// 循环所有的 bean对象:
		for(String beanName  : sigletons.keySet()){
			// 获取bean对象:
			Object bean = sigletons.get(beanName);
			// 判断 bean对象是否存在:
			if(bean != null){
				// 得到 bean的属性描述:
				try {
					PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
					for(PropertyDescriptor propertyDesc /*这里是 bean 里面的属性*/ : ps){
						// 获取setter方法:
						Method setter = propertyDesc.getWriteMethod();
						if(setter != null  && setter.isAnnotationPresent(ItmResource.class)){  // 还要   看看   setter方法是否存在 注解:
							// 取得注解:
							ItmResource resource = setter.getAnnotation(ItmResource.class);
							
							Object value  = null;
							if(resource.name() != null && !"".equals(resource.name())){
								// 判断是否在  集合里面:从集合里取出来  注入进去。
								value = sigletons.get(resource.name());
							}else{
								// 如果 没有指定 name属性  怎么办 ?
								value = sigletons.get(propertyDesc.getName());
								if(value == null){
									for(String key  : sigletons.keySet()){ // 历遍所有的bean
										// 是否存在  类型匹配的bean。
										if(propertyDesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())){
											value = sigletons.get(key);
											break;
										}
									}
								}
							}
							setter.setAccessible(true);
							setter.invoke(bean, value);  // 注入进去。
						}
					}
					// 对字段进行处理:
					Field[] fields  = bean.getClass().getDeclaredFields();
					for(Field field : fields ){
						if(field.isAnnotationPresent(ItmResource.class)){
							// 取得注解:
							ItmResource resource = field.getAnnotation(ItmResource.class);
							
							Object value  = null;
							if(resource.name() != null && !"".equals(resource.name())){
								// 判断是否在  集合里面:从集合里取出来  注入进去。
								value = sigletons.get(resource.name());
							}else{
								// 如果 没有指定 name属性  怎么办 ?
								value = sigletons.get(field.getName());  // 有 name直接取得对象。
								if(value == null){ 
									for(String key  : sigletons.keySet()){ // 历遍所有的bean
										// 是否存在  类型匹配的bean。 【再根据 字段的类型去寻找】
										if(field.getType().isAssignableFrom(sigletons.get(key).getClass())){
											value = sigletons.get(key);
											break;
										}
									}
								}
							}
							
							field.setAccessible(true);
							field.set(bean, value);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
	}

2,把上面的方法放到此类中:

package junit.test;


import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.ConvertUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

public class ItmClassPathXMLApplicationContext {
	
	private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
	// 存放实例
	private Map<String,Object> sigletons = new HashMap<String,Object>();
	
	public ItmClassPathXMLApplicationContext(String fileName){
		this.readXML(fileName);
		this.instanceBeans();
		this.annotationInject();
		this.injectObject();
	}

	/**
	 * 实现注解的方式  注入:
	 * 
	 */
	private void annotationInject() {
		// 循环所有的 bean对象:
		for(String beanName  : sigletons.keySet()){
			// 获取bean对象:
			Object bean = sigletons.get(beanName);
			// 判断 bean对象是否存在:
			if(bean != null){
				// 得到 bean的属性描述:
				try {
					PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
					for(PropertyDescriptor propertyDesc /*这里是 bean 里面的属性*/ : ps){
						// 获取setter方法:
						Method setter = propertyDesc.getWriteMethod();
						if(setter != null  && setter.isAnnotationPresent(ItmResource.class)){  // 还要   看看   setter方法是否存在 注解:
							// 取得注解:
							ItmResource resource = setter.getAnnotation(ItmResource.class);
							
							Object value  = null;
							if(resource.name() != null && !"".equals(resource.name())){
								// 判断是否在  集合里面:从集合里取出来  注入进去。
								value = sigletons.get(resource.name());
							}else{
								// 如果 没有指定 name属性  怎么办 ?
								value = sigletons.get(propertyDesc.getName());
								if(value == null){
									for(String key  : sigletons.keySet()){ // 历遍所有的bean
										// 是否存在  类型匹配的bean。
										if(propertyDesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())){
											value = sigletons.get(key);
											break;
										}
									}
								}
							}
							setter.setAccessible(true);
							setter.invoke(bean, value);  // 注入进去。
						}
					}
					// 对字段进行处理:
					Field[] fields  = bean.getClass().getDeclaredFields();
					for(Field field : fields ){
						if(field.isAnnotationPresent(ItmResource.class)){
							// 取得注解:
							ItmResource resource = field.getAnnotation(ItmResource.class);
							
							Object value  = null;
							if(resource.name() != null && !"".equals(resource.name())){
								// 判断是否在  集合里面:从集合里取出来  注入进去。
								value = sigletons.get(resource.name());
							}else{
								// 如果 没有指定 name属性  怎么办 ?
								value = sigletons.get(field.getName());  // 有 name直接取得对象。
								if(value == null){ 
									for(String key  : sigletons.keySet()){ // 历遍所有的bean
										// 是否存在  类型匹配的bean。 【再根据 字段的类型去寻找】
										if(field.getType().isAssignableFrom(sigletons.get(key).getClass())){
											value = sigletons.get(key);
											break;
										}
									}
								}
							}
							
							field.setAccessible(true);
							field.set(bean, value);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
	}

	private void injectObject() {
		for(BeanDefinition beanDefinition : beanDefines){
			// 得到  bean 。。
			Object bean = sigletons.get(beanDefinition.getId());
			if(bean != null){
				try {
					// 得到 bean的属性描述:
					PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
					// 循环  bean里面的 所有的属性:
					for( PropertyDefinition propertyDefinition: beanDefinition.getPropertys()){
						for(PropertyDescriptor propertyDesc /*这里是 bean 里面的属性*/ : ps){
							if(propertyDefinition.getName().equals(propertyDesc.getName())){
								// 如果相等 说明是存在 于 这个bean的。。。
								Method setter = propertyDesc.getWriteMethod();   // 获取属性的 setter方法。
								// 最好做一下判断:
								if(setter != null){
									
									Object value = null;
									if(propertyDefinition.getRef() != null && !"".equals(propertyDefinition.getRef().trim())){
										// 注入依赖对象:
										value = sigletons.get(propertyDefinition.getRef());
									}else{
										// 注入基本类型。。。把字符串的值  传换成  属性的值。
										value = ConvertUtils.convert(propertyDefinition.getValue(), propertyDesc.getPropertyType());
									}
									setter.setAccessible(true); // 允许访问 私有的方法。。
									setter.invoke(bean, value);// 把引用对象注入到属性。
								}
								break;
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 通过反射技术,完成  bean 的实例化:
	 */
	private void instanceBeans() {
		for(BeanDefinition beanDefinition : beanDefines){
			try {
				if(beanDefinition.getClassName() != null && !"".equals(beanDefinition.getClassName().trim())){
					sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	


	/**
	 * 读取  XML 的配置文件:
	 * @param fileName
	 */
	@SuppressWarnings("unchecked")
	private void readXML(String fileName) {
		// 创建读取器:
		SAXReader saxReader = new SAXReader();
		Document document = null;
		
		try{
			URL xmlPath = this.getClass().getClassLoader().getResource(fileName);
			document = saxReader.read(xmlPath); // 读取文件的内容。。。
			
			Map<String,String> nsMap = new HashMap<String,String>();
			nsMap.put("ns", "http://www.springframework.org/schema/beans"); // 加入命名空间
			
			// 创建beans/bean 查询路径。
			XPath xsub = document.createXPath("//ns:beans/ns:bean");  
			
			// 设置命名空间。
			xsub.setNamespaceURIs(nsMap);  								  
			
			// 获取文档下 所有bean节点:
			List<Element> beans = xsub.selectNodes(document);
			for(Element element : beans){
				String id = element.attributeValue("id");             // 获取id属性值。
				String clazz = element.attributeValue("class");		  //  获取  class 属性值。
				BeanDefinition beanDefine = new BeanDefinition(id, clazz);
				
				// 查询的相对路径:
				XPath propertysub = element.createXPath("ns:property");
				propertysub.setNamespaceURIs(nsMap);// 设置命名空间。
				
				List<Element> propertys = propertysub.selectNodes(element);
				for(Element property : propertys){
					String propertyName = property.attributeValue("name");
					String propertyRef = property.attributeValue("ref");
					
					String propertyValue = property.attributeValue("value");
					
					System.out.println(propertyName + "==" + propertyRef);
					PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyRef, propertyValue);
					
					// 放到  bean里面去:
					beanDefine.getPropertys().add(propertyDefinition);
					
				}
				beanDefines.add(beanDefine);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	
	/**
	 * 获取  bean实例
	 * @param beanName
	 * @return
	 */
	public Object getBean(String beanName){
		return this.sigletons.get(beanName);
	}
	
}

3,

/**
 * 1 解析属性。
 * 2 看看  在哪里 有注解。  
 * 3 看看 有没有 配置 name属性。如果没有,再怎么办?
 * 
 * @author Administrator
 *
 */

@Retention(RetentionPolicy.RUNTIME)   // 运行期。
@Target( {ElementType.FIELD ,ElementType.METHOD})            // 指定在  字段 与 方法上。
public @interface ItmResource {
	public String name() default "";
}


验证:

在测试类中,换成:

ItmClassPathXMLApplicationContext ctx = new ItmClassPathXMLApplicationContext("beans.xml");
		PersonService personService = (PersonService) ctx.getBean("personService");
		personService.save();



本文来源:

通过,学习传智播客黎活明老师的视频讲解。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值