@Autowired 1、Spring 通過一個 BeanPostProcessor 對 @Autowired 進行解析,所以要讓 @Autowired 起作用必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。
- <!-- 該 BeanPostProcessor 將自動起作用,對標注 @Autowired 的 Bean 進行自動注入 -->
- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
或者使用隱式注冊(隱式注冊 post-processors 包括了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor。)
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www./schema/beans"
- xmlns:xsi="http://www./2001/XMLSchema-instance"
- xmlns:context="http://www./schema/context"
- xsi:schemaLocation="http:
- http:
- http:
- http:
-
- <context:annotation-config/>
- </beans>
2、@Autowired默認按照類型匹配的方式進行注入
3、@Autowired注解可以用于成員變量、setter方法、構造器函數(shù)等
4、使用@Autowired注解須有且僅有一個與之匹配的Bean,當找不到匹配的 Bean 或者存在多個匹配的Bean時,Spring 容器將拋出 異常
5、Spring 允許我們通過 @Qualifier 注釋指定注入 Bean 的名稱。@Autowired 和 @Qualifier 結合使用時,自動注入的策略就從 byType 轉變成 byName 了。
- public class MovieRecommender {
-
- @Autowired
- @Qualifier("mainCatalog")
- private MovieCatalog movieCatalog;
-
- private CustomerPreferenceDao customerPreferenceDao;
-
- @Autowired
- public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
- this.customerPreferenceDao = customerPreferenceDao;
- }
-
-
- }
@Resource 1、@Resource 的作用相當于 @Autowired,只不過 @Autowired 按 byType 自動注入,@Resource 默認按 byName 自動注入罷了。
2、要讓 JSR-250 的注釋生效,除了在 Bean 類中標注這些注釋外,還需要在 Spring 容器中注冊一個負責處理這些注釋的 BeanPostProcessor
- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
3、@Resource 有兩個屬性是比較重要的,分別是 name 和 type,Spring 將 @Resource 注釋的 name 屬性解析為 Bean 的名字,而 type 屬性則解析為 Bean 的類型。所以如果使用 name 屬性,則使用 byName 的自動注入策略,而使用 type 屬性時則使用 byType 自動注入策略。如果既不指定 name 也不指定 type 屬性,這時將通過反射機制使用 byName 自動注入策略。
- public class SimpleMovieLister {
-
- private MovieFinder movieFinder;
-
- @Resource
- public void setMovieFinder(MovieFinder movieFinder) {
- this.movieFinder = movieFinder;
- }
- }
@PostConstruct 和 @PreDestroy 標注了 @PostConstruct 注釋的方法將在類實例化后調用,而標注了 @PreDestroy 的方法將在類銷毀之前調用。
- public class CachingMovieLister {
-
- @PostConstruct
- public void populateMovieCache() {
-
- }
-
- @PreDestroy
- public void clearMovieCache() {
-
- }
- }
@Component 1、使用@Component注解可以直接定義Bean,而無需在xml定義。但是若兩種定義同時存在,xml中的定義會覆蓋類中注解的Bean定義。
2、@Component 有一個可選的入?yún)?,用于指?Bean 的名稱。
- @Component
- public class ActionMovieCatalog implements MovieCatalog {
-
- }
3、<context:component-scan/> 允許定義過濾器將基包下的某些類納入或排除。Spring 支持以下 4 種類型的過濾方式:
過濾器類型 | 表達式范例 |
annotation | org.example.SomeAnnotation |
assignable | org.example.SomeClass |
regex | org\.example\.Default.* |
aspectj | org.example..*Service+ |
下面這個XML配置會忽略所有的@Repository注解并用“stub”儲存庫代替。
- <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>
4、默認情況下通過 @Component 定義的 Bean 都是 singleton 的,如果需要使用其它作用范圍的 Bean,可以通過 @Scope 注釋來達到目標
- @Scope("prototype")
- @Repository
- public class MovieFinderImpl implements MovieFinder {
-
- }
5、Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。 @Component是所有受Spring管理組件的通用形式; 而@Repository、@Service和 @Controller則是@Component的細化, 用來表示更具體的用例(例如,分別對應了持久化層、服務層和表現(xiàn)層)
- @Service
- public class SimpleMovieLister {
-
- private MovieFinder movieFinder;
-
- @Autowired
- public SimpleMovieLister(MovieFinder movieFinder) {
- this.movieFinder = movieFinder;
- }
- }
-
- @Repository
- public class JpaMovieFinder implements MovieFinder {
-
- }
6、要檢測這些類并注冊相應的bean,需要在XML中包含以下元素,其中'basePackage'是兩個類的公共父包 (或者可以用逗號分隔的列表來分別指定包含各個類的包)。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www./schema/beans"
- xmlns:xsi="http://www./2001/XMLSchema-instance"
- xmlns:context="http://www./schema/context"
- xsi:schemaLocation="http:
- http:
- http:
- http:
-
- <context:component-scan base-package="org.example"/>
-
- </beans>
此外,在使用組件掃描元素時,AutowiredAnnotationBeanPostProcessor 和CommonAnnotationBeanPostProcessor會隱式地被包括進來。 也就是說,連個組件都會被自動檢測并織入 - 所有這一切都不需要在XML中提供任何bean配置元數(shù)據(jù)。