溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring Boot核心的注解是什么

發(fā)布時間:2021-10-25 16:17:40 來源:億速云 閱讀:166 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Spring Boot核心的注解是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“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容器進(jìn)行管理。

簡單測試

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...

擴(kuò)展

一般的,一個完整的bean需要包括,id,class,initMethod,destroyMethod,·ref,scope。所以這里使用 Java  Config 進(jìn)行相關(guān)的配置這些屬性。修改第一個例子代碼

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注解上,屬性指向?qū)?yīng)的方法。

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();     } }

輸出結(jié)果為

init... com.spring.SomeBean@16022d9d com.spring.SomeBean@16022d9d destroy...

這樣就完成了一個配置的生命周期。

@ComponentScan

@ComponentScan注解,用于類或接口上主要指定的掃描路徑,Spring會把指定路徑下帶有指定注解的類自動裝配到bean容器里,會被自動裝配的注解包括@Controller,@Service,@Component,@Repository等。其作用相當(dāng)于,

<context:component-scan base-package=”com.maple.learn” />

配置。

基本使用

常用的屬性如下  basePackages,value,指定掃描路徑,如果為空,則以@ComponentScan注解的類所在的包掃描路徑。basePackageClasses:指定具體掃描的類  includeFilters:指定滿足Filter條件的類 excludeFilters:指定排除Filter條件的類  includeFilters和excludeFilters 的FilterType可選:ANNOTATION=注解類型  默認(rèn)、ASSIGNABLE_TYPE(指定固定類)、ASPECTJ(ASPECTJ類型)、REGEX(正則表達(dá)式)、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{    &hellip;&hellip; }

@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 應(yīng)用將所有符合條件的@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

感謝各位的閱讀,以上就是“Spring Boot核心的注解是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Spring Boot核心的注解是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI