一:@Autowired
最常见的注解,用于注入一个已经配置好的Spring Bean,常用元素之上
如:
@Autowired
private Sessionfactory sessionfactory
1: 默认使用类型匹配
2: 可以用于成员变量,setter方法,构造函数等
3: 只能有一个匹配的类型,如果没有指定类型或存在多个匹配则会出错
4: 允许配合@Qualifier,转变成使用Bean的name的名称进行注入
如:
@Autowired
@Qualifier('name')
private ....
二: @Resource
属于java标准的注解,原来用于jni的注入,在与spring配合时,作用类似Autowired,区别在于默认使用byName自动注入,使用的方式与Autowired
相同
三 @PostConstruct与@PreDestory
@PostConstruct 将会在实例化后自动调用,@PreDestory 将会在类销毁之前调用
四 @Component
用于直接定义Bean,主要需要在ApplicationContent.xml进行自动注解包的扫描
<context:component-scan base-package="org.example..."> //注意引入相应的xsi:schemaLocation
可以在注解中写入参数,用于指定Bean的名称,可选.
五 @Scope
默认使用Component注解的Bean都是singleton的,可以使用Scope指定具体的作用域,如:prototype
六 @Repository,@Service @Controller
均为在2.5引入的对Component的细化,分别代表持久层,服务层,表现层,使用的时候与Component相同
最常见的注解,用于注入一个已经配置好的Spring Bean,常用元素之上
如:
@Autowired
private Sessionfactory sessionfactory
1: 默认使用类型匹配
2: 可以用于成员变量,setter方法,构造函数等
3: 只能有一个匹配的类型,如果没有指定类型或存在多个匹配则会出错
4: 允许配合@Qualifier,转变成使用Bean的name的名称进行注入
如:
@Autowired
@Qualifier('name')
private ....
二: @Resource
属于java标准的注解,原来用于jni的注入,在与spring配合时,作用类似Autowired,区别在于默认使用byName自动注入,使用的方式与Autowired
相同
三 @PostConstruct与@PreDestory
@PostConstruct 将会在实例化后自动调用,@PreDestory 将会在类销毁之前调用
四 @Component
用于直接定义Bean,主要需要在ApplicationContent.xml进行自动注解包的扫描
<context:component-scan base-package="org.example..."> //注意引入相应的xsi:schemaLocation
可以在注解中写入参数,用于指定Bean的名称,可选.
五 @Scope
默认使用Component注解的Bean都是singleton的,可以使用Scope指定具体的作用域,如:prototype
六 @Repository,@Service @Controller
均为在2.5引入的对Component的细化,分别代表持久层,服务层,表现层,使用的时候与Component相同