1. 程式人生 > >Spring框架中bean註冊相關的註解

Spring框架中bean註冊相關的註解

官方資訊

XML名稱空間中的context(上下文,環境)。

xmlns:context="http://www.springframework.org/schema/context"
Element : component-scan
Scans the classpath for annotated components that will be 
 auto-registered as Spring beans. By default, the Spring-
 provided @Component, @Repository, @Service, @Controller, 
 @RestController, @ControllerAdvice, and @Configuration 
 stereotypes will be detected. Note: This tag implies the 
 effects of the 'annotation-config' tag, activating 
 @Required, @Autowired, @PostConstruct, @PreDestroy, 
 @Resource, @PersistenceContext and @PersistenceUnit 
 annotations in the component classes, which is usually 
 desired for autodetected components (without external 
 configuration). Turn off the 'annotation-config' attribute 
 to deactivate this default behavior, for example in order 
 to use custom BeanPostProcessor definitions for handling 
 those annotations. Note: You may use placeholders in 
 package paths, but only resolved against system properties 
 (analogous to resource paths). A component scan results in 
 new bean definitions being registered; Spring's 
 PropertySourcesPlaceholderConfigurer will apply to those 
 bean definitions just like to regular bean definitions, 
 but it won't apply to the component scan settings 
 themselves. See javadoc for 
 org.springframework.context.annotation.ComponentScan for 
 information on code-based alternatives to bootstrapping 
 component-scanning.

Content Model : (include-filter*, exclude-filter*)

我自己的翻譯如下:

元素:component-scan
掃描那些即將作為spring bean被自動註冊的註解元件的位元組碼路徑。
spring預設的提供了以下將會檢測的模板:
@Component, @Repository, @Service, @Controller,
 @RestController, @ControllerAdvice和@Configuration等。

注意:這個標籤意味著“註解式配置”標籤的影響,
他激活了那些通常渴求被自動檢測的元件包中的註解(不用使用外部配置)。
(@Required, @Autowired, @PostConstruct, @PreDestroy, 
@Resource, @PersistenceContext和@PersistenceUnit等)。

關閉"annotation-config"屬性就可以取消啟用本反應。
比如說為了使用使用者自定義的beanPost處理器定義來處理這些註解。
注意:你可能會在包路徑中使用佔位符,但是隻解決系統屬性(類似於資源路徑)。

元件掃描會導致新的bean定義被註冊。
spring的屬性資源佔位符配置器將會像常規的bean定義那樣應用到這些bean定義之上。
但是他不會被用於元件掃描本身。

檢視org.springframework.context.annotation.ComponentScan的文件
以查詢更多關於基於程式碼來引導包掃描的資訊。

內容模型:(include-filter*, exclude-filter*)

Spring中bean註冊相關的註解如下:

@Component, @Repository, @Service, @Controller, 
@RestController, @ControllerAdvice, @Configuration,
@Required, @Autowired, @PostConstruct, @PreDestroy, 
@Resource, @PersistenceContext, PersistenceUnit