@Autowired @Resource用法
@Autowired的用法和作用
这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get。
<bean id="userDao"class="..."/>
<bean id="userService"class="...">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
这样你在userService里面要做一个userDao的setter/getter方法。
但如果你用了@Autowired的话,你只需要在UserService的实现类中声明即可。
@Autowired
private IUserDao userdao;
Spring@Autowired注解与自动装配
1 配置文件的方法
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。
Boss 拥有 Office 和 Car 类型的两个属性:
3. Boss.java
public class Boss {
private Car car;
private Office office;
// 省略 get/setter
@Override
public String toString() {
return "car:" + car + "/n" + "office:" +office;
}
}
我们在 Spring 容器中将 Office 和 Car 声明为 Bean,并注入到 Boss Bean 中:下面是使用传统 XML 完成这个工作的配置文件 beans.xml:
beans.xml 将以上三个类配置成 Bean
<?xml version="1.0"encoding="UTF-8" ?>
<beansxmlns="http://www.springframework.org/schema/beans"
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">
<bean id="boss" class="Boss">
<property name="car" ref="car"/>
<property name="office" ref="office" />
</bean>
<bean id="office" class="Office">
<property name="officeNo" value="002"/>
</bean>
<bean id="car" class="Car"scope="singleton">
<property name="brand"value=" 红旗 CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
当我们运行以下代码时,控制台将正确打出 boss 的信息:
清单 5. 测试类:AnnoIoCTest.java
import org.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnoIoCTest {
public static void main(String[] args) {
String[] locations = {"beans.xml"};
ApplicationContext ctx =
new ClassPathXmlApplicationContext(locations);
Boss boss = (Boss) ctx.getBean("boss");
System.out.println(boss);
}
}
这说明 Spring 容器已经正确完成了 Bean 创建和装配的工作。
2. @Autowired
Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。通过 @Autowired的使用来消除 set ,get方法。
要实现我们要精简程序的目的。需要这样来处理:
在applicationContext.xml中加入:
<!-- 该 BeanPostProcessor 将自动对标注@Autowired 的 Bean 进行注入 -->
<beanclass="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
Spring 通过一个BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
修改在原来注入spirng容器中的bean的方法。
在域变量上加上标签@Autowired,并且去掉相应的get 和set方法
使用 @Autowired 注释的 Boss.java
importorg.springframework.beans.factory.annotation.Autowired;
public class Boss {
@Autowired
private Car car;
@Autowired
private Office office;
}
在applicatonContext.xml中把原来 引用的<porpery>标签也去掉。
<?xml version="1.0"encoding="UTF-8" ?>
<beansxmlns="http://www.springframework.org/schema/beans"
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">
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
<bean class="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor"/>
<!-- 移除 boss Bean 的属性注入配置的信息 -->
<bean id="boss" class="Boss"/>
<bean id="office" class="Office">
<property name="officeNo" value="001"/>
</bean>
<bean id="car" class=" Car"scope="singleton">
<property name="brand" value=" 红旗CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。
按照上面的配置,Spring 将直接采用 Java 反射机制对 Boss 中的 car 和 office 这两个私有成员变量进行自动注入。所以对成员变量使用 @Autowired 后,您大可将它们的 setter 方法(setCar() 和 setOffice())从 Boss 中删除。
当然,您也可以通过 @Autowired 对方法或构造函数进行标注,如果构造函数有两个入参,分别是 bean1 和 bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1 bean1 ,Bean2 bean2) 的入参来创建CountryService Bean。来看下面的对方法
public class Boss {
private Car car;
private Office office;
@Autowired
public void setCar(Car car) {
this.car = car;
}
@Autowired
public void setOffice(Office office) {
this.office = office;
}
…
}
这时,@Autowired 将查找被标注的方法的入参类型的 Bean,并调用方法自动注入这些 Bean。而下面的使用方法则对构造函数进行标注:
public class Boss {
private Car car;
private Office office;
@Autowired
public Boss(Car car ,Office office){
this.car = car;
this.office = office ;
}
}
由于 Boss() 构造函数有两个入参,分别是 car 和 office,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 Boss(Car car ,Office office) 的入参来创建 Boss Bean。
@Resource的作用和用法
@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,面 @Resource 默认按 byName自动注入罢了。
@Resource 有两个属性是比较重要的,分别是 name和 type,Spring将 @Resource 注释的 name属性解析为 Bean的名字,
而 type属性则解析为 Bean的类型。所以如果使用 name属性,则使用 byName的自动注入策略,而使用 type属性时则使用 byType
自动注入策略。如果既不指定 name也不指定 type属性,这时将通过反射机制使用 byName自动注入策略。
Resource 注释类位于 Spring发布包的 lib/j2ee/common-annotations.jar类包中,因此在使用之前必须将其加入到项目的类库中。
来看一个使用 @Resource 的例子:
使用 @Resource注释的 Boss.java
importjavax.annotation.Resource;
public class Boss{
// 自动注入类型为 Car 的 Bean
@Resource
private Car car; // 自动注入 bean 名称为 office 的 Bean
@Resource(name = "office")
private Office office;
}
一般情况下,我们无需使用类似于 @Resource(type=Car.class) 的注释方式,因为 Bean 的类型信息可以通过 Java反射从代码中获取。
要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring容器中注册一个负责处理这些注释的BeanPostProcessor:
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
CommonAnnotationBeanPostProcessor 实现了 BeanPostProcessor 接口,它负责扫描使用了 JSR-250 注释的 Bean,并对它们进行相应的操作。
@Autowired @Resource用法的更多相关文章
- @Autowired & @Resource 区别 & 解读@Bean
一样 Autowired & @Resource 都可以用来Bean的注入,可以写在属性(字段)上.也可以写在setter方法上 不一样 1.来源不一样 @Autowired 由Spr ...
- @Autowired @Resource @Qualifier的区别
参考博文: http://www.cnblogs.com/happyyang/articles/3553687.html http://blog.csdn.net/revent/article/det ...
- Spring通过注解@Autowired/@Resource获取bean实例时为什么可以直接获取接口而不是注入的类
问: 这个问题困扰了我好久,一直疑问这个接口的bean是怎么注入进去的?因为只看到使用@Service注入了实现类serviceImpl,使用时怎么却获取的接口,而且还能调用到实现类的方法,难道这个接 ...
- Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法
一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...
- @Autowired与@Resource用法
官方文档中有这样一段话. If you intend to express annotation-driven injection by name, do not primarily use @Aut ...
- IOC @Autowired/@Resource/@Qulified的用法实例
首先要知道另一个东西,default-autowire,它是在xml文件中进行配置的,可以设置为byName.byType.constructor和autodetect:比如byName,不用显式的在 ...
- @Autowired,@Resource,@Qualifier,@Primary,@Inject的作用和区别
@Autowired注解的用法:可以用于构造器,方法,参数,字段进行属性注入,有一个required属性,默认是true,当改成false时,如果注入的属性在容器中不存在也不会报错@Resource该 ...
- @Required @Autowired @Resource注解详解
一.@Required注解用于检查特定的属性是否设置 1.RequiredAnnotationBeanPostProcessor 为该注解的处理器,即bean后置处理器,检查所有带有该解的bean属性 ...
- @Autowired @Resource @Inject 自动注入
一.@AutoWired ( spring 的注解 )自动注入 /** * @Autowired: * 默认按照 Student 类型去容器中找对应的组件:applicationContext.get ...
随机推荐
- git学习系列--六分之一
版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统,可以对任何类型的文件进行版本控制. 细说分布式版本控制系统 在这类系统中,像 Git,Mercurial,Bazaar 以 ...
- CSS从大图片上截取小图标的操作
注:图片名称(tabicons.png)每个小图标width:18px;height:18px从左上角坐标为(-0px;-0px;); 例如第一个对号的坐标为(-0px;-0px;)第二个加号的图标为 ...
- Servlet Cookie处理
Servlet Cookie 处理 Cookie 是存储在客户端计算机上的文本文件,并保留了各种跟踪信息.Java Servlet 显然支持 HTTP Cookie. 识别返回用户包括三个步骤: 服务 ...
- JavaScript:JSON
JSON是一种数据格式,它并不从属于JavaScript,很多语言都有JSON的解析器和序列化器. 语法 JSON可以表示三种类型: 简单值:使用与JavaScript相同的语法,可以在JSON中 ...
- 解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid
重新启动服务器,访问web服务发现无法浏览啦!登陆服务器之后进到nginx使用./nginx -s reload重新读取配置文件,发现报nginx: [error] open() "/usr ...
- 3.openssl speed和openssl rand
(1).openssl speed 测试加密算法的性能. 支持的算法有: openssl speed [md2] [mdc2] [md5] [hmac] [sha1] [rmd160] [idea-c ...
- SQL Server 2008中删除errorlog文件的方法
删除error咯个文件[SSQL\MSSQL10.MSSQLSERVER\MSSQL\Log目录下面] 由于默认情况下,SQL Server 保存 7 个 ErrorLog 文件,名为: ErrorL ...
- input输入子系统
一.什么是input输入子系统? 1.Linux系统支持的输入设备繁多,例如键盘.鼠标.触摸屏.手柄或者是一些输入设备像体感输入等等,Linux系统是如何管理如此之多的不同类型.不同原理.不同的输入信 ...
- 线程池pool
参考链接 http://www.open-open.com/lib/view/open1415453575730.html 参考配置 http://www.cnblogs.com/linjiqin/a ...
- AS错误:Error:Execution failed for task ':gM99SDK:processReleaseResources'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'F:\BaiduYunDown
原因,buildToolsVersion 版本太低. 在build.gradle文件设置 buildToolsVersion 设置高一点,但必须是SDK里面有的.