(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();



本文來源:

通過,學習傳智播客黎活明老師的視頻講解。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章