做項目用到spring boot 感覺spring boot用起來比較流暢。想總結一下,別的不多說,從入口開始。
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的入口啟動類詳細解釋
飛過導入代碼不看,我們先看第一部分:
- @SpringBootApplication
- @EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class) 配置
- @EnableScheduling 從應用的全局定義一個調度器
@SpringBootApplication 是什么?@EnableAutoConfiguration 又是什么? 記住一句話:學習很簡單,動手玩一玩,要是還不懂,再問1、2、3。
好吧,我們就進去玩一玩,點擊進去就以看到源碼。
@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 注解的替代品出現的, @SpringBootConfiguration是@Configuration 的殼。儲君上位成了國王,換上丞相的兒子做丞相,其它官位不變;甚至連兒子都算不上,頂多是給老丞相新定做了一身衣服。
@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan
@Configuration、 @EnableAutoConfiguration、@ComponentScan
總結: Springboot 提供了統一的注解@SpringBootApplication 來替代以上三個注解,簡化程序的配置。下面解釋一下各注解的功能。
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
關於@Target、@Retention、@Documented等注釋 可以參照(http://blog.csdn.net/bluetjs/article/details/52250596)自行腦補。
哎呀還是再開一篇來講吧。
spring boot 提倡約定優於配置,方便之處就是用約定來代替配置,減少的是手工配置不等於去掉配置。
凡是遇到注解我們都要問一句:那原來的配置是什么樣子?現在的約定是什么樣子? 好吧,
@Configuration
原來是這個樣子:
<beans xmlns="http://www.springframework.org/schema/beans" ... ...> </beans>
現在:
@Configuration
@EnableAutoConfiguration
顧名思義,@EnableAutoConguration是自動化配置。那就是@Configuration的高級形式,其本質不變的就是,最終形成的配置放在 beans內部,和@Bean的效果相同。不過它的適用范圍大部分是內部默認包。也就是它對這些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 duplicates List<String> factories = new ArrayList<String>(new LinkedHashSet<String>( SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, this.beanClassLoader))); // Remove those specifically disabled factories.removeAll(Arrays.asList(attributes.getStringArray("exclude"))); // Sort factories = new AutoConfigurationSorter(this.resourceLoader) .getInPriorityOrder(factories); return factories.toArray(new String[factories.size()]); } catch (IOException ex) { throw new IllegalStateException(ex); } }
獲取類路徑下spring.factories下key為EnableAutoConfiguration全限定名對應值
@AutoConfigurationPackage
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage { }
@Import
每當我看到這個注釋,意味着我快觸摸到問題所在。
@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默認會掃描啟動類同包以及子包下的注解,實現的途徑就是必須在啟動類引入注解@ComponetScan。
可以看到,項目中引入了注解@SpringBootApplication 這就意味(本質上)引入注解@ComponetScan,所以就會掃描Application.Class所在包以及子包的注解。
當然,如果你要改變這種掃描包的方式,原理很簡單就是:用@ComponentScan注解進行指定要掃描的包以及要掃描的類。
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Repeatable(ComponentScans.class) public @interface ComponentScan {
}
注解:@ComponentScan的內容:

知識點:
@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標注在方法上(返回某個實例的方法),等價於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 的條件注冊的一套快速開發整合包
Spring 最初利用“工廠模式”( DI )和“代理模式”( AOP )解耦應用組件,並構建了一些列功能組件。大家覺得挺好用,於是按照 MVC 框架模式,(用Spring 解耦的組件)搞了一個MVC用來開發 web 應用也就是( SpringMVC )。然后有發現每次開發都要搞很多依賴,寫很多樣板代碼很麻煩,於是搞了一些懶人整合包( starter ),這套就是 Spring Boot 。
spring 框架有超多的延伸產品例如 boot security jpa etc... 但它的基礎就是 spring 的 ioc 和 aop ioc 提供了依賴注入的容器 aop 解決了面向橫切面的編程 然后在此兩者的基礎上實現了其他延伸產品的高級功能 Spring MVC 呢是基於 Servlet 的一個 MVC 框架 主要解決 WEB 開發的問題 因為 Spring 的配置太復雜了 各種 XML JavaConfig hin 麻煩 於是懶人改變世界推出了 Spring boot 約定優於配置 簡化了 spring 的配置流程 簡單談下自己的理解 以上來自度娘,感覺和自己的理解相當。直接拿來用,占個坑。以后完善。
springApplication可以讀取不同種類的源文件:
- 類- java類由
AnnotatedBeanDefinitionReader加載。 Resource- xml資源文件由XmlBeanDefinitionReader讀取, 或者groovy腳本由GroovyBeanDefinitionReader讀取Package- java包文件由ClassPathBeanDefinitionScanner掃描讀取。CharSequence- 字符序列可以是類名、資源文件、包名,根據不同方式加載。如果一個字符序列不可以解析程序到類,也不可以解析到資源文件,那么就認為它是一個包。- 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
