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.
說說Python中的垃圾回收機制?
2.
螞蟻金服面試分享,阿里的offer真的不難,3位朋友全部offer
3.
Spring Boot (三十一)——自定義歡迎頁及favicon
4.
Spring Boot核心架構
5.
IDEA創建maven web工程
6.
在IDEA中利用maven創建java項目和web項目
7.
myeclipse新導入項目基本配置
8.
zkdash的安裝和配置
9.
什麼情況下會導致Python內存溢出?要如何處理?
10.
CentoOS7下vim輸入中文
本站公眾號
歡迎關注本站公眾號,獲取更多信息
相關文章
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)
>>更多相關文章<<