JavaShuo
栏目
标签
spring ioc注解 IOC
时间 2019-11-06
标签
spring
ioc
注解
栏目
Spring
繁體版
原文
原文链接
@Autowired
一、Spring 经过一个 BeanPostProcessor 对 @Autowired 进行解析,因此要让 @Autowired 起做用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
Java代码
<!-- 该 BeanPostProcessor 将自动起做用,对标注
@Autowired
的 Bean 进行自动注入 -->
<bean
class
=
"org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"
/>
或者使用隐式注册(隐式注册 post-processors 包括了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor。)
Java代码
<?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/>
</beans>
二、@Autowired默认按照类型匹配的方式进行注入
三、@Autowired注解能够用于成员变量、setter方法、构造器函数等
四、使用@Autowired注解须有且仅有一个与之匹配的Bean,当找不到匹配的 Bean 或者存在多个匹配的Bean时,Spring 容器将抛出 异常
五、Spring 容许咱们经过 @Qualifier 注释指定注入 Bean 的名称。@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。
Java代码
public
class
MovieRecommender {
@Autowired
@Qualifier
(
"mainCatalog"
)
private
MovieCatalog movieCatalog;
private
CustomerPreferenceDao customerPreferenceDao;
@Autowired
public
MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this
.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
@Resource
一、@Resource 的做用至关于 @Autowired,只不过 @Autowired 按 byType 自动注入,@Resource 默认按 byName 自动注入罢了。
二、要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还须要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor
Java代码
<bean
class
=
"org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"
/>
三、@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。因此若是使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。若是既不指定 name 也不指定 type 属性,这时将经过反射机制使用 byName 自动注入策略。
Java代码
public
class
SimpleMovieLister {
private
MovieFinder movieFinder;
@Resource
public
void
setMovieFinder(MovieFinder movieFinder) {
this
.movieFinder = movieFinder;
}
}
@PostConstruct 和 @PreDestroy
标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁以前调用。
Java代码
public
class
CachingMovieLister {
@PostConstruct
public
void
populateMovieCache() {
// populates the movie cache upon initialization...
}
@PreDestroy
public
void
clearMovieCache() {
// clears the movie cache upon destruction...
}
}
@Component
一、使用@Component注解能够直接定义Bean,而无需在xml定义。可是若两种定义同时存在,xml中的定义会覆盖类中注解的Bean定义。
二、@Component 有一个可选的入参,用于指定 Bean 的名称。
Java代码
@Component
public
class
ActionMovieCatalog
implements
MovieCatalog {
// ...
}
三、<context:component-scan/> 容许定义过滤器将基包下的某些类归入或排除。Spring 支持如下 4 种类型的过滤方式:
过滤器类型
表达式范例
annotation
org.example.SomeAnnotation
assignable
org.example.SomeClass
regex
org\.example\.Default.*
aspectj
org.example..*Service+
下面这个XML配置会忽略全部的@Repository注解并用“stub”储存库代替。
Java代码
<beans ...>
<context:component-scan base-
package
=
"org.example"
>
<context:include-filter type=
"regex"
expression=
".*Stub.*Repository"
/>
<context:exclude-filter type=
"annotation"
expression=
"org.springframework.stereotype.Repository"
/>
</context:component-scan>
</beans>
四、默认状况下经过 @Component 定义的 Bean 都是 singleton 的,若是须要使用其它做用范围的 Bean,能够经过 @Scope 注释来达到目标
Java代码
@Scope
(
"prototype"
)
@Repository
public
class
MovieFinderImpl
implements
MovieFinder {
// ...
}
五、Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。 @Component是全部受Spring管理组件的通用形式; 而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)
Java代码
@Service
public
class
SimpleMovieLister {
private
MovieFinder movieFinder;
@Autowired
public
SimpleMovieLister(MovieFinder movieFinder) {
this
.movieFinder = movieFinder;
}
}
@Repository
public
class
JpaMovieFinder
implements
MovieFinder {
// implementation elided for clarity
}
六、要检测这些类并注册相应的bean,须要在XML中包含如下元素,其中'basePackage'是两个类的公共父包 (或者能够用逗号分隔的列表来分别指定包含各个类的包)。
Java代码
<?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:component-scan base-
package
=
"org.example"
/>
</beans>
此外,在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor 和CommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 - 全部这一切都不须要在XML中提供任何bean配置元数据。
相关文章
1.
Spring-IOC注解
2.
Spring - 3.IOC注解
3.
Spring注入(IOC):
4.
spring-IOC注入
5.
Spring IoC @Autowired 注解详解
6.
spring02-IOC注解
7.
Spring的IOC注解开发
8.
Spring-IOC进阶注解
9.
Spring IOC常用注解
10.
Spring注解实现IOC(DI)
更多相关文章...
•
Spring IoC容器:BeanFactory和ApplicationContext
-
Spring教程
•
Spring DI(依赖注入)的实现方式:属性注入和构造注入
-
Spring教程
•
Spring Cloud 微服务实战(三) - 服务注册与发现
•
Scala 中文乱码解决
相关标签/搜索
ioc
spring+ioc
Spring-IOC
spring+ioc+di+aop
Spring IOC&DI
ioc&di
aop+ioc
jna&ioc
springmvc+ioc
ioc+aop
Spring
Spring教程
MyBatis教程
Thymeleaf 教程
spring cloud
注册中心
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
《给初学者的Windows Vista的补遗手册》之074
2.
CentoOS7.5下编译suricata-5.0.3及简单使用
3.
快速搭建网站
4.
使用u^2net打造属于自己的remove-the-background
5.
3.1.7 spark体系之分布式计算-scala编程-scala中模式匹配match
6.
小Demo大知识-通过控制Button移动来学习Android坐标
7.
maya检查和删除多重面
8.
Java大数据:大数据开发必须掌握的四种数据库
9.
强烈推荐几款IDEA插件,12款小白神器
10.
数字孪生体技术白皮书 附下载地址
本站公众号
欢迎关注本站公众号,获取更多信息
相关文章
1.
Spring-IOC注解
2.
Spring - 3.IOC注解
3.
Spring注入(IOC):
4.
spring-IOC注入
5.
Spring IoC @Autowired 注解详解
6.
spring02-IOC注解
7.
Spring的IOC注解开发
8.
Spring-IOC进阶注解
9.
Spring IOC常用注解
10.
Spring注解实现IOC(DI)
>>更多相关文章<<