日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解|SpringBoot核心的3個注解詳解

本文轉(zhuǎn)載自微信公眾號「小明菜市場」,作者小明菜市場 。轉(zhuǎn)載本文請聯(lián)系小明菜市場公眾號。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比丹東網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式丹東網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋丹東地區(qū)。費用合理售后完善,10年實體公司更值得信賴。

前言

Spring Boot 最大的特點是無需 XML 配置文件,能夠?qū)崿F(xiàn)自動裝配,并進行全自動化的jar包配置。Spring Boot 是微服務(wù)的核心,其Spring Cloud 是基于Spring Boot 為基礎(chǔ)的。其框架是用來簡化Spring應(yīng)用的初始搭建和開發(fā)過程,即,簡化了框架,便捷了開發(fā)。下面開始介紹Spring Boot 最核心的三個注解

Configuration

在Spring4以后,官方推薦使用 Java Config 來代替 Application.xml 聲明將Bean交給容器管理。在Spring Boot 中,Java Config 使用完全代替了application.xml 實現(xiàn)了xml的零配置, 開下面這個例子

實例

創(chuàng)建一個bean類

 
 
 
  1. public class SomeBean { 
  2.     public void doWork() { 
  3.         System.out.println("do work..."); 
  4.     } 

其中,dowork是邏輯方法 再創(chuàng)建一個Config類

 
 
 
  1. @Configuration 
  2. public class Config { 
  3.     @Bean 
  4.     public SomeBean someBean() { 
  5.         return new SomeBean(); 
  6.     } 

在這里,在Config類上添加了一個@configuration注解,可以理解為Spring中的配置類,其返回值為someBean,someBean方法上也添加了一個@bean注解,其返回對象也將會交由Spring容器進行管理。

簡單測試

 
 
 
  1. public class Test { 
  2.     public static void main(String[] args) { 
  3.         ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); 
  4.         SomeBean sb = context.getBean(SomeBean.class); 
  5.         sb.doWork(); 
  6.     } 

這里,創(chuàng)建了一個AnnotationConfigApplicationContext對象,傳入了Config.class 后,得到了someBean對象。

do work...

擴展

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

 
 
 
  1. public class SomeBean { 
  2.  
  3.     private void init() { 
  4.         System.out.println("init..."); 
  5.     } 
  6.  
  7.     public void doWork() { 
  8.         System.out.println("do work..."); 
  9.     } 
  10.  
  11.     private void destroy() { 
  12.         System.out.println("destroy..."); 
  13.     } 
  14.  

增加,init,destroy方法

 
 
 
  1. @Configuration 
  2. public class Config { 
  3.  
  4.     @Bean(initMethod = "init",destroyMethod = "destroy") 
  5.     public SomeBean someBean() { 
  6.         return new SomeBean(); 
  7.     } 

在bean注解上,屬性指向?qū)?yīng)的方法。

 
 
 
  1. public class Test { 
  2.     public static void main(String[] args) { 
  3.         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); 
  4.         SomeBean sb1 = context.getBean(SomeBean.class); 
  5.         System.out.println(sb1); 
  6.  
  7.         SomeBean sb2 = context.getBean(SomeBean.class); 
  8.         System.out.println(sb2); 
  9.         context.close(); 
  10.     } 

輸出結(jié)果為

 
 
 
  1. init... 
  2. com.spring.SomeBean@16022d9d 
  3. com.spring.SomeBean@16022d9d 
  4. destroy... 

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

@ComponentScan

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

 
 
 
  1.  

配置。

基本使用

常用的屬性如下 basePackages,value,指定掃描路徑,如果為空,則以@ComponentScan注解的類所在的包掃描路徑。basePackageClasses:指定具體掃描的類 includeFilters:指定滿足Filter條件的類 excludeFilters:指定排除Filter條件的類 includeFilters和excludeFilters 的FilterType可選:ANNOTATION=注解類型 默認(rèn)、ASSIGNABLE_TYPE(指定固定類)、ASPECTJ(ASPECTJ類型)、REGEX(正則表達式)、CUSTOM(自定義類型),自定義的Filter需要實現(xiàn)TypeFilter接口 @ComponentScan的常見的配置如下:

 
 
 
  1. @ComponentScan(value="com.maple.learn", 
  2.    excludeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)}, 
  3.    includeFilters = {@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class})} 
  4.         ) 
  5. public class SampleClass{ 
  6.    …… 

@EnableAutoConfiguration

其注解是一個組合注解, 其源碼如下

 
 
 
  1. @Target(ElementType.TYPE) 
  2. @Retention(RetentionPolicy.RUNTIME) 
  3. @Documented 
  4. @Inherited 
  5. @AutoConfigurationPackage 
  6. @Import(AutoConfigurationImportSelector.class) 
  7. public @interface EnableAutoConfiguration { 
  8.  
  9.     String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; 
  10.  
  11.     Class[] exclude() default {}; 
  12.      
  13.     String[] excludeName() default {}; 
  14.  

其中最重要的是@Import(AutoConfigurationImportSelector.class)注解,借助AutoConfigurationImportSelector,@EnableAutoConfiguration 幫助Spring Boot 應(yīng)用將所有符合條件的@Configuration 配置加載到IOC容器中,而最主要的還需要借助于 Spring 框架的一個工具類,SpringFactoriestLoader 將META-INF/spring.factories加載配置,spring.factories文件是一個典型的properties配置文件,配置格式為key=value形式,不過key和value都是完整的類名,例如

 
 
 
  1. org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.jpa.repository.support.JpaRepositoryFactory 

分享題目:詳解|SpringBoot核心的3個注解詳解
網(wǎng)頁URL:http://www.5511xx.com/article/ccdocoi.html