续Spring依赖注入的方式:http://huangminwen.iteye.com/blog/1041743
在一个项目中,我们的Spring文件中会有许多bean,每个bean中都有一些属性,如果把所有bean的属性都配置在配置文件中,那么这时候我们的Spring配置文件是臃肿不堪的,而且维护起来也不方便,Spring为我们提供了用@Resource注解完成属性装配的功能解决这个问题
此时我们的配置文件变成了
<?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="personDAOBean" class="org.spring.dao.impl.PersonDAOBean" /> <bean id="personServiceBean" class="org.spring.service.impl.PersonServiceBean" scope="prototype" /> </beans>
此时的配置中就只有bean的信息了,显得很简洁,可以看到配置文件中增加了命名空间
xmlns:context=http://www.springframework.org/schema/context
与schemaLocation
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
还有标签
<context:annotation-config />
这个配置隐式注册了多个对注释进行解析处理的处理器:
AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
要想应用@Resource注解,则需要引入common-annotations.jar,该jar文件在spring安装目录的lib\j2ee下面
此时PersonServiceBean修改如下
package org.spring.service.impl; import javax.annotation.Resource; import org.spring.dao.PersonDAO; import org.spring.service.PersonService; public class PersonServiceBean implements PersonService { @Resource private PersonDAO personDAO; public void showMessage() { personDAO.add(); } }
测试类
package org.spring; import org.junit.Test; import org.spring.service.PersonService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring.xml"); PersonService personService = (PersonService) ctx .getBean("personServiceBean"); personService.showMessage(); } }
控制台结果
执行PersonDAOBean中的增加方法!
我们也可以把@Resource注解作用在setter方法上面,此时PersonServiceBean修改如下
package org.spring.service.impl; import javax.annotation.Resource; import org.spring.dao.PersonDAO; import org.spring.service.PersonService; public class PersonServiceBean implements PersonService { private PersonDAO personDAO; public void showMessage() { personDAO.add(); } @Resource public void setPersonDAO(PersonDAO personDAO) { this.personDAO = personDAO; } }
@Resource默认按名称装配,名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象,当找不到与名称匹配的bean才会按类型装配。
注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。如:
package org.spring.service.impl; import javax.annotation.Resource; import org.spring.dao.PersonDAO; import org.spring.service.PersonService; public class PersonServiceBean implements PersonService { private PersonDAO personDAO; public void showMessage() { personDAO.add(); } @Resource(name="xxx") public void setPersonDAO(PersonDAO personDAO) { this.personDAO = personDAO; } }
此时Spring配置中并没有名称为xxx的bean,所以会报错
相关推荐
用@Resource注解完成属性装配
spring注解 -----最简单的注解与自动装配例子
Spring@Autowired注解与自动装配.rar
基于java实现的类似spring自动装配的代码。
11_用@Resource注解完成属性装配 12_编码剖析@Resource注解的实现原理 13.@Autowire注解与自动装配 14.让Spring自动扫描和管理Bean 15.使用JDK中的Proxy技术实现AOP功能 16.使用CGLIB实现AOP功能与AOP概念解释...
自己实现的简易的模拟Spring的IoC容器,实现注解自动装配
@ComponentScan 组件扫描,可自动发现和装配一些 Bean。 @Component 可配合 CommandLineRunner 使用,在程序启动后执行一些基础任务。 @RestController 注解是 @Controller 和 @ResponseBody 的合集, 表示这是个...
spring注解方式解析 1.ioc自动装配 2.注入解析
Spring 自动装配及其注解 博客:https://blog.csdn.net/u010476739/article/details/76735241
@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。如果我们想使用按照名称(byName)来装配,可以结合@Qualifier注解一起...
主要介绍了Spring注解实现自动装配过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了简单了解Spring Bean常用注解的装配,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了通过@Resource注解实现属性装配代码详解,具有一定借鉴价值,需要的朋友可以参考下
NULL 博文链接:https://364232252.iteye.com/blog/2369853
《spring实战》中给装配下了一个定义:创建应用对象之间协作关系的行为称为装配。也就是说当一个对象的属性是另一个对象时,实例化时,需要为这个对象属性进行实例化。这就是装配。如果一个对象只通过接口来表明依赖...
在Spring 2.5以后出现了注解,使用注解结合XML的方式,简化了XML配置的复杂度。 老版本中纯XML配置实现IOC 在配置文件中配置如下: <bean id=userService class=...
主要为大家详细介绍了Java注解机制之Spring自动装配实现原理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
代码及ppt涵盖 Spring4.0 的所有核心内容:在 Eclipse 中安装 SpringIDE 插件、IOC & DI、在 Spring 中配置 Bean、自动装配、Bean 之间的关系(依赖、继承)、Bean 的作用域、使用外部属性文件、SpEL、管理 Bean 的...