詳解 | Spring Boot 核心的 3 個注解詳解
本文轉載自微信公眾號「小明菜市場」,作者小明菜市場 。轉載本文請聯(lián)系小明菜市場公眾號。
前言
Spring Boot 最大的特點是無需 XML 配置文件,能夠實現(xiàn)自動裝配,并進行全自動化的jar包配置。Spring Boot 是微服務的核心,其Spring Cloud 是基于Spring Boot 為基礎的。其框架是用來簡化Spring應用的初始搭建和開發(fā)過程,即,簡化了框架,便捷了開發(fā)。下面開始介紹Spring Boot 最核心的三個注解
Configuration
在Spring4以后,官方推薦使用 Java Config 來代替 Application.xml 聲明將Bean交給容器管理。在Spring Boot 中,Java Config 使用完全代替了application.xml 實現(xiàn)了xml的零配置, 開下面這個例子
實例
創(chuàng)建一個bean類
- public class SomeBean {
- public void doWork() {
- System.out.println("do work...");
- }
- }
其中,dowork是邏輯方法 再創(chuàng)建一個Config類
- @Configuration
- public class Config {
- @Bean
- public SomeBean someBean() {
- return new SomeBean();
- }
- }
在這里,在Config類上添加了一個@configuration注解,可以理解為Spring中的配置類,其返回值為someBean,someBean方法上也添加了一個@bean注解,其返回對象也將會交由Spring容器進行管理。
簡單測試
- public class Test {
- public static void main(String[] args) {
- ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
- SomeBean sb = context.getBean(SomeBean.class);
- sb.doWork();
- }
- }
這里,創(chuàng)建了一個AnnotationConfigApplicationContext對象,傳入了Config.class 后,得到了someBean對象。
do work...
擴展
一般的,一個完整的bean需要包括,id,class,initMethod,destroyMethod,·ref,scope。所以這里使用 Java Config 進行相關的配置這些屬性。修改第一個例子代碼
- public class SomeBean {
- private void init() {
- System.out.println("init...");
- }
- public void doWork() {
- System.out.println("do work...");
- }
- private void destroy() {
- System.out.println("destroy...");
- }
- }
增加,init,destroy方法
- @Configuration
- public class Config {
- @Bean(initMethod = "init",destroyMethod = "destroy")
- public SomeBean someBean() {
- return new SomeBean();
- }
- }
在bean注解上,屬性指向對應的方法。
- public class Test {
- public static void main(String[] args) {
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
- SomeBean sb1 = context.getBean(SomeBean.class);
- System.out.println(sb1);
- SomeBean sb2 = context.getBean(SomeBean.class);
- System.out.println(sb2);
- context.close();
- }
- }
輸出結果為
- init...
- com.spring.SomeBean@16022d9d
- com.spring.SomeBean@16022d9d
- destroy...
這樣就完成了一個配置的生命周期。
@ComponentScan
@ComponentScan注解,用于類或接口上主要指定的掃描路徑,Spring會把指定路徑下帶有指定注解的類自動裝配到bean容器里,會被自動裝配的注解包括@Controller,@Service,@Component,@Repository等。其作用相當于,
- <context:component-scan base-package=”com.maple.learn” />
配置。
基本使用
常用的屬性如下 basePackages,value,指定掃描路徑,如果為空,則以@ComponentScan注解的類所在的包掃描路徑。basePackageClasses:指定具體掃描的類 includeFilters:指定滿足Filter條件的類 excludeFilters:指定排除Filter條件的類 includeFilters和excludeFilters 的FilterType可選:ANNOTATION=注解類型 默認、ASSIGNABLE_TYPE(指定固定類)、ASPECTJ(ASPECTJ類型)、REGEX(正則表達式)、CUSTOM(自定義類型),自定義的Filter需要實現(xiàn)TypeFilter接口 @ComponentScan的常見的配置如下:
- @ComponentScan(value="com.maple.learn",
- excludeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)},
- includeFilters = {@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class})}
- )
- public class SampleClass{
- ……
- }
@EnableAutoConfiguration
其注解是一個組合注解, 其源碼如下
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- @AutoConfigurationPackage
- @Import(AutoConfigurationImportSelector.class)
- public @interface EnableAutoConfiguration {
- String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
- Class<?>[] exclude() default {};
- String[] excludeName() default {};
- }
其中最重要的是@Import(AutoConfigurationImportSelector.class)注解,借助AutoConfigurationImportSelector,@EnableAutoConfiguration 幫助Spring Boot 應用將所有符合條件的@Configuration 配置加載到IOC容器中,而最主要的還需要借助于 Spring 框架的一個工具類,SpringFactoriestLoader 將META-INF/spring.factories加載配置,spring.factories文件是一個典型的properties配置文件,配置格式為key=value形式,不過key和value都是完整的類名,例如
- org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.jpa.repository.support.JpaRepositoryFactory