剖析@Resource注解的实现原理

本文详细介绍了Spring框架中依赖注入的实现过程,包括读取配置文件、实例化Bean、属性注入及注解处理等关键步骤。

1:遍历所有的bean对象  得到一个bean对象

2:获取对象的属性描述

3:得到属性的setter方法 并判断setter方法上面是否有注解

4:取得注解 判断注解是否有name

5:如果有name的话  就直接把引用对象注入到属性

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
public class ClassPathXMLApplicationContext {
	//用来存放bean的信息
	private List<BeanDefinition> BeanDefines = new ArrayList<BeanDefinition>(); 
	//保存bean
	private Map<String ,Object > sigletons =new HashMap<String, Object>();

	public  ClassPathXMLApplicationContext(String fileName){
		this.readXML(fileName);//读XML文件
		this.instanceBeans();//实例化bean对象
		this.annotationInject();//处理注解的方法
		this.injectObject();//为bean对象的属性注入值
	}

	//处理注解的方法
	private void annotationInject() {
		// 遍历所有的bean对象
		for(String beanName:sigletons.keySet()){
			Object  bean = sigletons.get(beanName);
			if(bean!=null ){
				try {
					PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
					for(PropertyDescriptor properdesc:ps){
						Method setter = properdesc.getWriteMethod();
						if(setter!=null && setter.isAnnotationPresent(AllenResource.class)){
							AllenResource resource = setter.getAnnotation(AllenResource.class);
							Object value = null;
							if(resource.name()!=null && !"".equals(resource.name())){
								value = sigletons.get(resource.name());
							}else {
								value = sigletons.get(properdesc.getName());
								if(value==null){
									for(String key : sigletons.keySet()){
										if(properdesc.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(AllenResource.class)){
							AllenResource resource = field.getAnnotation(AllenResource.class);
							Object value = null;
							if(resource.name()!=null && !"".equals(resource.name())){
								value = sigletons.get(resource.name());
							}else {
								value = sigletons.get(field.getName());
								if(value==null){
									for(String key : sigletons.keySet()){
										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();
				}
			}
		}
	}
	//为bean对象的属性注入值
	private void injectObject() {
		for (BeanDefinition beanDefinition :BeanDefines) {
			//实例化过后的bean
			Object bean = sigletons.get(beanDefinition.getId());
			if(bean!=null){
				try {
					PropertyDescriptor[] ps=Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
					for(PropertyDefinition propertyDefinition:beanDefinition.getProperties()){
						for(PropertyDescriptor properdesc:ps){
							if(propertyDefinition.getName().equals(properdesc.getName())){
								Method setter =properdesc.getWriteMethod();//获取setter方法
								if(setter!=null){
									Object value =null;
									if(propertyDefinition.getRef()!=null && !"".equals(propertyDefinition.getRef())){
										value  =sigletons.get(propertyDefinition.getRef());
										System.out.println(value);
									}else {
										System.out.println(properdesc.getPropertyType());
										value  =propertyDefinition.getValue();
									}
									setter.setAccessible(true);//允许调用private的方法
									setter.invoke(bean, value);//把应用对象注入到属性
								}
							}
						}
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					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) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
	}
	
	//读取app*.xml里面的内容
	private void readXML(String fileName) {
		SAXReader  saxReader = new SAXReader();
		Document  document =null;
		try{
			URL xmlPath=this.getClass().getClassLoader().getResource(fileName);
			//System.out.println(xmlPath);file:/G:/workspaces1/spring/bin/beans.xml
			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");
			//System.out.println(xsub);[XPath: /descendant-or-self::node()/child::ns:beans/child::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);
				
				//得到bean下面的所有property节点
				XPath propertyPath = document.createXPath("ns:property");
				propertyPath.setNamespaceURIs(nsMap);
				List<Element> properties =propertyPath.selectNodes(element);
				
				for(Element property:properties){
					String  propertyName = property.attributeValue("name");
					String  propertyRef  = property.attributeValue("ref");
					String  propertyValue  = property.attributeValue("value");
					//System.out.println(propertyName+"="+propertyRef+propertyValue);
					PropertyDefinition propertyDefinition =new  PropertyDefinition(propertyName, propertyRef,propertyValue);
					beanDefine.getProperties().add(propertyDefinition);
				}
				
				BeanDefines.add(beanDefine);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	//获取bean实例
	public Object getBean(String beanName){
		return this.sigletons.get(beanName);
	}
}

//如有错误 请马上指出来  望你我共享 


<think>首先,用户的问题是:“我想理解@Autowired和@Resource注解在Java中的区别 请问Java中@Autowired和@Resource注解的区别”。这基于系统级指令和用户可见层的要求。 系统级指令: - 所有行内数学表达式必须使用$...$格式。但用户的问题是关于Java注解的,没有数学表达式,所以可能不需要。 - 独立公式用$$...$$,但同样,这里不涉及数学。 - LaTeX语法正确:确保如果使用数学公式,要正确。 - 使用中文回答:整个响应必须用中文。 - 生成相关问题:在回答后,添加相关问题部分。 - 引用标识:在回答中引用的段落末尾自然地添加引用标识,如[^1]。 用户可见层: - 回答结构清晰,帮助用户逐步解决问题。 - 尽量保证回答真实可靠。 - 参考站内引用:用户提供了引用[1]到[4],这些是关于@Autowired和@Resource注解的区别的。我需要基于这些引用构建回答。 提供的引用: - [^1]: 讨论@Autowired和@Resource的区别,包括来源不同(Spring vs Java规范)。 - [^2]: 强调来源不同和依赖查找顺序。 - [^3]: 提到当接口有多个实现时,都需要名称匹配,@Autowired用@Qualifier,@Resource用name。 - [^4]: 给出示例代码,比较默认匹配策略。 回答结构: 1. **逐步解释**:清晰地解释每个注解,然后比较区别。 2. **使用引用**:在相关部分添加引用标识。 3. **语言**:全部用中文。 4. **相关问题**:在最后添加相关问题部分。 大纲回答: - **介绍**:简述两个注解的作用(用于依赖注入)。 - **来源不同**:@Autowired来自Spring框架,@Resource来自Java规范(JSR-250)。 - **依赖查找顺序**:解释默认行为的不同。 - **名称匹配**:当有多个实现时,如何指定名称。 - **示例**:提供代码示例来对比。 - **总结**:概括主要区别。 - **相关问题**:生成3-4个相关问题。 确保回答真实可靠:基于提供的引用。引用[1],[2],[3],[4]都一致地描述了这些区别。 最终响应结构: - 回答部分:用清晰段落。 - 相关问题部分:以“§§相关问题§§”开头,列出问题。 现在,构建回答。</think>### Java中@Autowired和@Resource注解的区别 在Java开发中,特别是使用Spring框架时,`@Autowired`和`@Resource`都是常用的注解,用于实现依赖注入(DI)。它们的主要目的是自动装配Bean,减少手动配置的代码量。然而,它们在来源、依赖查找顺序、名称匹配策略等方面存在显著差异。下面我将逐步解释这些区别,帮助您清晰理解。 #### 1. **来源不同** - **@Autowired**:这是Spring框架专有的注解,由Spring 2.5版本引入。它属于Spring的生态系统,主要用于Spring容器中的Bean管理[^2]。 - **@Resource**:这是Java标准规范的一部分,源自JSR-250(Java Specification Request 250)。它不依赖于Spring,而是Java EE/Jakarta EE的一部分,因此更通用,可以在非Spring环境中使用[^1][^2]。 #### 2. **依赖查找顺序不同** - **@Autowired**:默认按**类型(byType)** 进行依赖查找。也就是说,Spring容器会查找与字段或方法参数类型匹配的Bean。如果找到多个匹配的Bean,它会抛出异常,除非指定了名称[^2][^4]。 - 例如:`@Autowired private NotificationService service;` 会注入类型为`NotificationService`的Bean。 - **@Resource**:默认按**名称(byName)** 进行依赖查找。它会先根据Bean的名称匹配,如果名称未指定,再回退到按类型匹配。这减少了歧义,尤其在多个实现存在时[^1][^4]。 - 例如:`@Resource private NotificationService service;` 会查找名为"service"的Bean。 #### 3. **名称匹配策略(当接口有多个实现时)** 当同一个接口有多个实现类时,两者都需要指定Bean名称来避免歧义,但指定方式不同: - **@Autowired**:需要配合`@Qualifier`注解来显式指定Bean名称。这增加了灵活性,但代码稍显冗长[^3][^4]。 - 示例代码: ```java @Autowired @Qualifier("emailService") private NotificationService service; ``` - **@Resource**:可以直接使用`name`属性指定Bean名称,不需要额外注解。这更简洁,且符合Java标准[^3][^4]。 - 示例代码: ```java @Resource(name = "emailService") private NotificationService service; ``` #### 4. **默认匹配策略和兼容性** - **@Autowired**:作为Spring特有注解,它深度集成Spring的功能,如支持构造函数注入和字段注入。但它不适用于非Spring项目[^4]。 - **@Resource**:作为Java标准注解,它兼容性更好,可以在任何支持JSR-250的容器中使用(如Tomcat或WildFly)。但它在Spring中可能不如`@Autowired`灵活,尤其在处理复杂依赖时[^1][^2]。 #### 总结 - **主要区别**:`@Autowired`是Spring专有,默认按类型查找,需用`@Qualifier`指定名称;`@Resource`是Java标准,默认按名称查找,可直接用`name`属性指定名称[^1][^2][^3][^4]。 - **推荐使用场景**: - 在纯Spring项目中,`@Autowired`更常用,因为它与Spring特性无缝集成。 - 在需要跨框架兼容性或避免Spring依赖时,优先使用`@Resource`。 - **注意事项**:两者都能简化依赖注入,但过度使用可能导致代码耦合。建议结合单元测试确保注入正确。 如果您有具体代码场景,我可以进一步分析!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值