Spring 核心註解

@Configuration 配置類註解

聲明一個類是配置類,等同於配置文件中的applicationcontext.xml
加載Spring配置的兩種方式:

  1. ClassPathXmlApplicationContext XML方式
ApplicationContext app = new ClassPathXmlApplicationContext("applicationcontext.xml");
  1. AnnoatationConfigApplicationContext 註解方式
ApplicationContext app = new AnnotationConfigApplicationContext(MainConfig.class);

@ComponentScan 包掃描註解

@ComponentScan(value="com.enjoy.cap2")表示掃描此目錄下的包。

參數說明:

  • value:指定要掃描的包
  • excludeFilters = Filter[]:指定掃描的時候按照什麼規則排除那些組件
  • includeFilters = Filter[]:指定掃描的時候只需要包含哪些組件
  • useDefaultFilters = false:默認是true,掃描所有組件,要改成false,includeFilters 和excludeFilters纔會生效

掃描規則如下:

  • FilterType.ANNOTATION:按照註解
  • FilterType.ASSIGNABLE_TYPE:按照給定的類型;比如按BookService類型
  • FilterType.ASPECTJ:使用ASPECTJ表達式
  • FilterType.REGEX:使用正則指定
  • FilterType.CUSTOM:使用自定義規則,自已寫類,實現TypeFilter接口

示例:

/**
 * 包掃描測試類
 *
 * @author yuhao.wang3
 * @since 2019/8/24 10:39
 */
@Configuration
@ComponentScan(value = "com.xiaolyuh.component.scan", includeFilters = {
        // 只掃描Controller註解的 Bean
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
        // 只掃描特定的類
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {TestService.class}),
        // 使用特定的過濾規則
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {CustomTypeFilter.class})
}, useDefaultFilters = false)
public class ComponentScanConfig {

}
@Test
public void contextTest() {
    String[] names = context.getBeanDefinitionNames();
    Arrays.stream(names).forEach(System.out::println);
}

輸出結果:

componentScanConfig
testController
customTypeFilter
customTypeFilterService
testService

TestDao就沒有被加載,完整代碼請到倉庫拉取

@Bean 聲明一個Bean,並放到Spring容器

將一個普通類聲明成一個Spring裏面的Bean。等同於XML中的 <bean id="xxx" class="xxx"/>Spring bean 的初始化和銷燬的三種方式和執行順序
可參考這裏。

@Scope 描述的是Spring容器如何創建Bean的實例的

  • singleton:一個Spring容器中只有一個Bean的實例,以爲Spring的默認配置,全容器共享一個實例,在容器初始化的時候,完成Bean的初始化。
  • prototype:每次調用新建一個Bean的實例。在容器初始化的時候也會存一個沒有初始化的Bean標示到容器。
  • request:Web項目中,給每一個HTTP request新建一個Bean的實例。
  • session:Web項目中,給每一個HTTP session新建一個Bean的實例。

示例:

@Configuration
public class ScopeConfig {

    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Bean("scopeTestBean")
    public ScopeTestBean scopeTestBean() {
        return new ScopeTestBean();
    }
}

@Lazy

在 Spring 單例模式下,默認在容器初始化的時候創建Bean,如果加了@Lazy註解,那麼容器啓動的時候不會去實例化對象,而是在第一次使用Bean的時候纔回去創建對象。

示例:

@Configuration
public class LazyConfig {

    @Lazy
    @Bean
    public LazyTestBean lazyTestBean() {
        return new LazyTestBean();
    }
}

Spring 聲明Bean的註解

  • @Component: 組件,沒有明確的角色。
  • @Service : 在業務邏輯層(Service層)使用。
  • @Repository:  再數據訪問層(Dao層)使用。
  • @Controller: 再展現層(MVC->Spring MVC)使用。

Spring 注入Bean的註解:

  • @Autowired:Spring提供的註解。
  • @inject:JSR-330提供的註解。
  • @Resource:JSP-250提供的註解。

聲明Spring Bean和注入Bean的幾種常用註解和區別可參考這裏。

@Conditional 條件注入

當引入@Conditional時, 容器可以選擇性的註冊bean。具體可以參考:Spring 條件註解(@Conditional)

@Import註冊bean

@Import註解一次可以注入多個Bean,默認使用全類名作爲Bean id;而@Bean一次只能注入一個Bean,默認使用方法名作爲Bean id。
詳情可參考:聲明Spring Bean和注入Bean的幾種常用註解和區別

源碼

https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases

spring-boot-student-spring 工程

layering-cache

爲監控而生的多級緩存框架 layering-cache這是我開源的一個多級緩存框架的實現,如果有興趣可以看一下

發佈了204 篇原創文章 · 獲贊 153 · 訪問量 86萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章