自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解 | Spring Boot 核心的 3 個注解詳解

開發(fā) 架構
Spring Boot 最大的特點是無需 XML 配置文件,能夠實現(xiàn)自動裝配,并進行全自動化的jar包配置。

[[349724]]

本文轉載自微信公眾號「小明菜市場」,作者小明菜市場 。轉載本文請聯(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類

  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 進行相關的配置這些屬性。修改第一個例子代碼

  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注解上,屬性指向對應的方法。

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

輸出結果為

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

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

@ComponentScan

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

  1. <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的常見的配置如下:

  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 應用將所有符合條件的@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 

 

責任編輯:武曉燕 來源: 小明菜市場
相關推薦

2024-10-14 17:18:27

2011-04-15 09:44:45

Spring

2023-02-09 08:01:12

核心組件非阻塞

2020-10-28 09:50:33

SpringBootJava

2017-04-26 11:00:34

Spring BootHelloWorld詳解

2024-12-16 08:10:00

Spring開發(fā)

2024-11-21 14:42:31

2025-01-15 08:19:12

SpringBootRedis開源

2024-08-13 08:41:18

2025-04-16 10:03:40

開發(fā)Spring應用程序

2024-01-02 07:04:23

2025-02-28 08:14:53

2020-06-24 09:35:50

SpringSpring BooJava

2024-11-05 09:25:45

2023-11-06 07:25:51

Spring配置應用程序

2024-01-10 12:26:16

2025-01-03 16:27:35

SpringBoot代碼打包

2022-05-25 09:00:00

令牌JWT安全

2024-04-03 15:40:14

WebSocketWeb應用Spring

2024-08-05 08:45:35

SpringKafkaSCRAM
點贊
收藏

51CTO技術棧公眾號