日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot 启动类

發(fā)布時間:2023/11/29 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot 启动类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

做項目用到spring boot 感覺spring boot用起來比較流暢。想總結(jié)一下,別的不多說,從入口開始。

spring boot啟動類Application.class 不能直接放在main/java文件夾下

一、spring boot的入口啟動類概覽。

import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter;@SpringBootApplication @EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class) @EnableScheduling public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}/** * 跨域過濾器 * @return CorsFilter 跨域過濾器對象*/ @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", buildConfig()); // 4 return new CorsFilter(source); }
   private CorsConfiguration buildConfig() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); return corsConfiguration; } }

?二、spring boot的入口啟動類詳細(xì)解釋

飛過導(dǎo)入代碼不看,我們先看第一部分:

  • @SpringBootApplication???
  • @EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class)??? 配置
  • @EnableScheduling??????????????? 從應(yīng)用的全局定義一個調(diào)度器

@SpringBootApplication?? 是什么?@EnableAutoConfiguration?? 又是什么? 記住一句話:學(xué)習(xí)很簡單,動手玩一玩,要是還不懂,再問1、2、3。

好吧,我們就進(jìn)去玩一玩,點擊進(jìn)去就以看到源碼。?

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)) public @interface SpringBootApplication {/*** Exclude specific auto-configuration classes such that they will never be applied.* @return the classes to exclude*/Class<?>[] exclude() default {};/*** Exclude specific auto-configuration class names such that they will never be* applied.* @return the class names to exclude* @since 1.3.0*/String[] excludeName() default {};/*** Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}* for a type-safe alternative to String-based package names.* @return base packages to scan* @since 1.3.0*/@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")String[] scanBasePackages() default {};/*** Type-safe alternative to {@link #scanBasePackages} for specifying the packages to* scan for annotated components. The package of each class specified will be scanned.* <p>* Consider creating a special no-op marker class or interface in each package that* serves no purpose other than being referenced by this attribute.* @return base packages to scan* @since 1.3.0*/@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")Class<?>[] scanBasePackageClasses() default {};}

?

?從源代碼中可以看到 @SpringBootApplication 被 @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan 注解所修飾。

再深入的玩一下 @SpringBootConfiguration 注意其中的一句話:就是下文中的紅色部分。
Can be used as an alternative to the Spring's * standard {@code @Configuration} annotation so that configuration can be found * automatically (for example in tests).
/*** Indicates that a class provides Spring Boot application* {@link Configuration @Configuration}. Can be used as an alternative to the Spring's* standard {@code @Configuration} annotation so that configuration can be found* automatically (for example in tests). * <p>* Application should only ever include <em>one</em>* {@code @SpringApplicationConfiguration} and most idiomatic Spring Boot applications* will inherit it from {@code @SpringBootApplication}.** @author Phillip Webb* @since 1.4.0*/ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration {} ?

這就是說,@SpringBootConfiguration是作為 原來Spring 注解體系中@Configuration 注解的替代品出現(xiàn)的, @SpringBootConfiguration是@Configuration 的殼。儲君上位成了國王,換上丞相的兒子做丞相,其它官位不變;甚至連兒子都算不上,頂多是給老丞相新定做了一身衣服。

?@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

?@Configuration、????????????????? @EnableAutoConfiguration、@ComponentScan

總結(jié):?Springboot 提供了統(tǒng)一的注解@SpringBootApplication 來替代以上三個注解,簡化程序的配置。下面解釋一下各注解的功能。

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan

?關(guān)于@Target、@Retention、@Documented等注釋 可以參照(http://blog.csdn.net/bluetjs/article/details/52250596)自行腦補。

?哎呀還是再開一篇來講吧。

?spring boot 提倡約定優(yōu)于配置,方便之處就是用約定來代替配置,減少的是手工配置不等于去掉配置。

?凡是遇到注解我們都要問一句:那原來的配置是什么樣子?現(xiàn)在的約定是什么樣子? 好吧,

@Configuration

原來是這個樣子:

<beans xmlns="http://www.springframework.org/schema/beans" ... ...></beans>

現(xiàn)在:

@Configuration

?

?

?

?

?@EnableAutoConfiguration

????顧名思義,@EnableAutoConguration是自動化配置。那就是@Configuration的高級形式,其本質(zhì)不變的就是,最終形成的配置放在 beans內(nèi)部,和@Bean的效果相同。不過它的適用范圍大部分是內(nèi)部默認(rèn)包。也就是它對這些Bean的有特殊要求。

1 @Target(ElementType.TYPE) 2 @Retention(RetentionPolicy.RUNTIME) 3 @Documented 4 @Inherited 5 @AutoConfigurationPackage 6 @Import(EnableAutoConfigurationImportSelector.class) 7 public @interface EnableAutoConfiguration { 8 9 String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; 10 11 /** 12 * Exclude specific auto-configuration classes such that they will never be applied. 13 * @return the classes to exclude 14 */ 15 Class<?>[] exclude() default {}; 16 17 /** 18 * Exclude specific auto-configuration class names such that they will never be 19 * applied. 20 * @return the class names to exclude 21 * @since 1.3.0 22 */ 23 String[] excludeName() default {}; 24 25 }

?

EnableAutoConfigurationImportSelector.Class

?

?

public String[] selectImports(AnnotationMetadata metadata) {try {AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(EnableAutoConfiguration.class.getName(),true));Assert.notNull(attributes, "No auto-configuration attributes found. Is "+ metadata.getClassName()+ " annotated with @EnableAutoConfiguration?");// Find all possible auto configuration classes, filtering duplicatesList<String> factories = new ArrayList<String>(new LinkedHashSet<String>(SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,this.beanClassLoader)));// Remove those specifically disabledfactories.removeAll(Arrays.asList(attributes.getStringArray("exclude")));// Sortfactories = new AutoConfigurationSorter(this.resourceLoader).getInPriorityOrder(factories);return factories.toArray(new String[factories.size()]);}catch (IOException ex) {throw new IllegalStateException(ex);}}

?

?獲取類路徑下spring.factories下key為EnableAutoConfiguration全限定名對應(yīng)值

?

@AutoConfigurationPackage

?

?

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage {}

?

@Import

每當(dāng)我看到這個注釋,意味著我快觸摸到問題所在。

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Import {/*** {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}* or regular component classes to import.*/Class<?>[] value();}

?

?

@ComponentScan

我們知道Spring Boot默認(rèn)會掃描啟動類同包以及子包下的注解,實現(xiàn)的途徑就是必須在啟動類引入注解@ComponetScan。

可以看到,項目中引入了注解@SpringBootApplication 這就意味(本質(zhì)上)引入注解@ComponetScan,所以就會掃描Application.Class所在包以及子包的注解。

當(dāng)然,如果你要改變這種掃描包的方式,原理很簡單就是:用@ComponentScan注解進(jìn)行指定要掃描的包以及要掃描的類。

?

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Repeatable(ComponentScans.class) public @interface ComponentScan {

} 注解:@ComponentScan的內(nèi)容:

?

知識點:

@ComponentScan has a Annotation @Repeatable with has a vale of ComponentScans.class

?

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented public @interface ComponentScans {ComponentScan[] value();}

?

?

?知識點:

@Repeatable :indicate that the annotation type whose declaration it @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Repeatable {/*** Indicates the <em>containing annotation type</em> for the* repeatable annotation type.* @return the containing annotation type*/Class<? extends Annotation> value(); }

知識點:

@Retention:specify Annotation retention policy such as SOURCE、CLASS、RUNTIME @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention {/*** Returns the retention policy.* @return the retention policy*/RetentionPolicy value(); }

?

?

?接下來看代碼

/** * 跨域過濾器 * @return CorsFilter */ @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", buildConfig()); // 4 return new CorsFilter(source); }

?

@Bean

?原來

<bean id = "corsFilter" class="org.springframework.web.filter.CorsFilter"> </bean>

?@Bean標(biāo)注在方法上(返回某個實例的方法),等價于spring的xml配置文件中的<bean>,作用為:注冊bean對象。

?

在這個類中涉及到的配置就是:

<beans xmlns="http://www.springframework.org/schema/beans" ... ... ><bean id = "corsFilter" class="org.springframework.web.filter.CorsFilter"></bean></beans>

?

@EnableScheduling

?

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Import(SchedulingConfiguration.class) @Documented public @interface EnableScheduling {}

?

@Configuration @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public class SchedulingConfiguration {@Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)@Role(BeanDefinition.ROLE_INFRASTRUCTURE)public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {return new ScheduledAnnotationBeanPostProcessor();}}

?

?

忍不住要插點小常識:
大概說一下:
Spring 是一個“引擎”
Spring MVC 是基于 Spring 的一個 MVC 框架
Spring Boot 是基于 Spring4 的條件注冊的一套快速開發(fā)整合包

Spring 最初利用“工廠模式”( DI )和“代理模式”( AOP )解耦應(yīng)用組件,并構(gòu)建了一些列功能組件。大家覺得挺好用,于是按照 MVC 框架模式,(用Spring 解耦的組件)搞了一個MVC用來開發(fā) web 應(yīng)用也就是( SpringMVC )。然后有發(fā)現(xiàn)每次開發(fā)都要搞很多依賴,寫很多樣板代碼很麻煩,于是搞了一些懶人整合包( starter ),這套就是 Spring Boot 。?
spring 框架有超多的延伸產(chǎn)品例如 boot security jpa etc... 但它的基礎(chǔ)就是 spring 的 ioc 和 aop ioc 提供了依賴注入的容器 aop 解決了面向橫切面的編程 然后在此兩者的基礎(chǔ)上實現(xiàn)了其他延伸產(chǎn)品的高級功能 Spring MVC 呢是基于 Servlet 的一個 MVC 框架 主要解決 WEB 開發(fā)的問題 因為 Spring 的配置太復(fù)雜了 各種 XML JavaConfig hin 麻煩 于是懶人改變世界推出了 Spring boot 約定優(yōu)于配置 簡化了 spring 的配置流程 簡單談下自己的理解?? 以上來自度娘,感覺和自己的理解相當(dāng)。直接拿來用,占個坑。以后完善。

?

?springApplication可以讀取不同種類的源文件:

  • 類- java類由AnnotatedBeanDefinitionReader加載。
  • Resource?- xml資源文件由XmlBeanDefinitionReader讀取, 或者groovy腳本由GroovyBeanDefinitionReader讀取
  • Package?- java包文件由ClassPathBeanDefinitionScanner掃描讀取。
  • CharSequence?- 字符序列可以是類名、資源文件、包名,根據(jù)不同方式加載。如果一個字符序列不可以解析程序到類,也不可以解析到資源文件,那么就認(rèn)為它是一個包。
  • http://www.51drhome.com
  • http://www.sohu.com/a/157811214_405968
  • http://www.wang1314.com/doc/topic-2664759-1.html
  • http://jianfangmi.com/qinggangbieshu/qinggangbieshuanli/201612/00001662.html

轉(zhuǎn)載于:https://www.cnblogs.com/huangsxj/p/7728123.html

總結(jié)

以上是生活随笔為你收集整理的spring boot 启动类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。