spring中@Resource和@Autowired理解
一、@Resource的理解
@Resource在bean注入的时候使用,@Resource所属包其实不是spring,而是javax.annotation.Resource,只不过spring支持该注解
@Resource里有name,lookup,type,authenticationType,shareable,mappedName,description这几个属性
具体看源码结构截图
0、可以直接在要注入的属性上这样写
@Resource
private User user;
不管配置文件里有没有写id或name,都能识别到,默认应该是根据类class来识别的。
1、name
当spring的xml配置文件中的bean配置了name或id时,它都可以识别到,也就是如果<bean id="user_id" name="user_name" class="xxx.xxx.xxx"></bean>,那么可以在要注入的属性上加@Resource(name="user_id")或者@Resource(name="user_name")都可以
2、lookup
3、type
Class type() default java.lang.Object.class;这个是源码,默认值是Object.class,那就一目了然了,无需多说了
4、authenticationType
AuthenticationType authenticationType() default AuthenticationType.CONTAINER;这个是源码,AuthenticationType这是个枚举类,有两个属性CONTAINER,APPLICATION。
暂时没搞懂这俩有啥区别,有知道的麻烦告知
5、shareable
指示此组件和其他组件之间是否可以共享该资源。这可以指定代表任何支持类型的连接工厂的资源,并且不能为其他类型的资源指定。它是一个布尔型,默认是true
6、mappedName
7、description
当然,spring的注入不光是可以属性注入,也可以set方法和构造函数注入,也就是说
@Resource
public void setUser(User user) {
this.user = user;
}
二、@Autowired的理解
@Autowired是属于spring的注解,它所在的包org.springframework.beans.factory.annotation,它是按byType注入的,默认是要依赖的对象必须存在,看源码就可以理解,boolean required() default true;可以看到默认值是true,如果需要允许依赖对象是null,那就@Autowired(required=false)就可以了。
如果我们想@Autowired按名称装配,可以结合@Qualifier注解一起使用
@Autowired
@Qualifier("user")
private User user;
spring中@Resource和@Autowired理解的更多相关文章
- Spring中@Resource与@Autowired、@Qualifier的用法与区别
1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...
- Spring中@Resource与@Autowired、@Qualifier的用法与区别(转)
1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...
- Spring中@Resource与@Autowired
问题 这其实就是@Autoware与@Resource没有正确的使用,这个错误是因为wmPoiOplogService这个变量装配方式是@Resource,按照@Resource的按名字查找的方式,并 ...
- Spring注解 @Resource和@Autowired
@Resource和@Autowired两者都是做bean的注入使用.其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spri ...
- Spring注解@Resource和@Autowired的区别
@Resource和@Autowired都是用来做bean的依赖注入的,两者都可以写在字段和setter方法上. java为我们提供了 javax.annotation.Resource这个注解. s ...
- Spring注解@Resource和@Autowired区别对比、spring扫描的默认bean的Id、程序获取spring容器对象
-------------------------注解扫面的bean的ID问题-------------------------- 0.前提需要明白注解扫描出来的bean的id默认是类名首字母小写,当 ...
- Spring注解@Resource和@Autowired区别对比
转载:http://www.cnblogs.com/think-in-java/p/5474740.html @Resource和@Autowired都是做bean的注入时使用,其实@Resource ...
- Spring 注解 @Resource和@Autowired(转)
鸣谢:http://my.oschina.net/u/216467/blog/205951 @Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spri ...
- Spring 注解 @Resource和@Autowired
@Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spr ...
随机推荐
- MulticastSocket 使用
/** * ServerMulticastSocketTest.java * 版权所有(C) 2014 * 创建者:cuiran 2014-1-9 下午3:22:01 */ package com.u ...
- 二分算法C实现
#include <stdio.h> #include <stdlib.h> #define NR(x) (sizeof(x)/sizeof(x[0])) int Binary ...
- charles抓取https请求包
说明: 用charles抓取https请求,会出现SSL Proxying disabled in Proxy Settings这样的提示,如下图.要通过charles抓取数据,还需要进行一些简单的设 ...
- Android网络请求框架之Retrofit实践
网络访问框架经过了从使用最原始的AsyncTask构建简单的网络访问框架(甚至不能称为框架),后来使用开源的android-async-http库,再到使用google发布的volley库,一直不懈的 ...
- PS 图像特效-非线性滤波器
利用非线性滤波器,使图像的色彩凝块,形成一种近似融化的特效. clc; clear all; addpath('E:\PhotoShop Algortihm\Image Processing\PS A ...
- 关于linux内核驱动开发中Makefile编译的问题
obj-y:打个比方,我要编译的是hello.c这个文件,obj-y就会把hello.c或者hello.c编译生成的hello.s文件链接到内核中去. obj-m:打个比方,我要编译的是hello.c ...
- Win10家庭版中的SQL2005无法远程连接
最近公司重新更换了电脑,电脑自事Win10家庭版本.在安装开发工具中发现有不少的问题,如无法安装SQL Server 2005,无法安装VS2013等.最终通过网上寻找安装SQL Server 200 ...
- Spring Boot 2.0系列文章(七):SpringApplication 深入探索
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...
- 前端技术之_CSS详解第四天
前端技术之_CSS详解第四天 一.第三天的小总结 盒模型box model,什么是盒子? 所有的标签都是盒子.无论是div.span.a都是盒子.图片.表单元素一律看做文本. 盒模型有哪些组成: wi ...
- linux中安装程序及账户管理
程序安装及管理 1. Linux 应用程序基础 Linux命令与应用程序的关系 1):文件位置 系统命令:一般在/bin和/sbin目录中,或为Shell内部指令 应用程序:通常在/usr/bin和 ...