偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

詳解 | Spring Boot 核心的 3 個(gè)注解詳解

開(kāi)發(fā) 架構(gòu)
Spring Boot 最大的特點(diǎn)是無(wú)需 XML 配置文件,能夠?qū)崿F(xiàn)自動(dòng)裝配,并進(jìn)行全自動(dòng)化的jar包配置。

[[349724]]

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

前言

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

Configuration

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

實(shí)例

創(chuàng)建一個(gè)bean類(lèi)

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

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

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

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

簡(jiǎn)單測(cè)試

  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)建了一個(gè)AnnotationConfigApplicationContext對(duì)象,傳入了Config.class 后,得到了someBean對(duì)象。

do work...

擴(kuò)展

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

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

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

@ComponentScan

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

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

配置。

基本使用

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

  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

其注解是一個(gè)組合注解, 其源碼如下

  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 框架的一個(gè)工具類(lèi),SpringFactoriestLoader 將META-INF/spring.factories加載配置,spring.factories文件是一個(gè)典型的properties配置文件,配置格式為key=value形式,不過(guò)key和value都是完整的類(lèi)名,例如

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

 

責(zé)任編輯:武曉燕 來(lái)源: 小明菜市場(chǎng)
相關(guān)推薦

2024-10-14 17:18:27

2011-04-15 09:44:45

Spring

2023-02-09 08:01:12

核心組件非阻塞

2017-04-26 11:00:34

Spring BootHelloWorld詳解

2020-10-28 09:50:33

SpringBootJava

2024-12-16 08:10:00

Spring開(kāi)發(fā)

2024-11-21 14:42:31

2025-01-15 08:19:12

SpringBootRedis開(kāi)源

2024-08-13 08:41:18

2025-04-16 10:03:40

開(kāi)發(fā)Spring應(yīng)用程序

2024-01-02 07:04:23

2025-02-28 08:14:53

2025-07-08 07:15:00

Spring配置多種方式

2020-06-24 09:35:50

SpringSpring BooJava

2024-11-05 09:25:45

2023-11-06 07:25:51

Spring配置應(yīng)用程序

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應(yīng)用Spring
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)