3.自动配置控制器以及扫描控制器的原理,核心注解
3.1.如何自动配置Spring+SpringMVC
3.1.1.原理
Spring Boot在进行SpringApplication 对象实例化是会加载META-INF/spring.factories文件,将该配置文件中的配置载入到Spring容器,进行自动配置。
3.1.2.源码分析
以是视图转换器为例
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/html/" />
<property name="suffix" value=".html" />
<property name="order" value="2"/>
</bean>
org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration
在项目查找 Ctrl+Shift+T 查找"类":WebClientAutoConfiguration
在application.properites配置
前缀名
spring.mvc.view.prefix = /WEB-INF/jsp/
后缀名
spring.mvc.view.suffix=.jsp
3.2.如何扫描控制器的
原理:当启动App.java时,SpringBoot 会议App.java为基准点依次向上扫描同一级的包也就是controller包以及其子包内控制器,或者数据访问接口
,@Service,@Component等。
3.3.Spring Boot的核心注解
3.3.1.启动类与@SpringBootApplication
Spring Boot的项目一般都会是有注解*application标注的入口类,入口类中会有main方法,main方法是一个标准的JAVA应用程序的入口方法,可以直接启动。
@SpringBootApplication 注解是Spring Boot的核心注解,以此注解标注的入口类是应用的启动类,通常会在启动类的main方法中通过SpringApplication.run(App.calss,args)
来起到Spring Boot应用项目。
@SpringBootApplication public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); //启动Spring Boot的核心代码 SpringApplication.run(App.class, args); } }
@SpringBootApplication源码
1 @Target(ElementType.TYPE) 2 @Retention(RetentionPolicy.RUNTIME) 3 @Documented 4 @Inherited 5 @SpringBootConfiguration 6 @EnableAutoConfiguration 7 @ComponentScan(excludeFilters = { 8 @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), 9 @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) 10 public @interface SpringBootApplication { 11 12 /** 13 * Exclude specific auto-configuration classes such that they will never be applied. 14 * @return the classes to exclude 15 */ 16 @AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude") 17 Class<?>[] exclude() default {}; 18 19 /** 20 * Exclude specific auto-configuration class names such that they will never be 21 * applied. 22 * @return the class names to exclude 23 * @since 1.3.0 24 */ 25 @AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName") 26 String[] excludeName() default {}; 27 28 /** 29 * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses} 30 * for a type-safe alternative to String-based package names. 31 * @return base packages to scan 32 * @since 1.3.0 33 */ 34 @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") 35 String[] scanBasePackages() default {}; 36 37 /** 38 * Type-safe alternative to {@link #scanBasePackages} for specifying the packages to 39 * scan for annotated components. The package of each class specified will be scanned. 40 * <p> 41 * Consider creating a special no-op marker class or interface in each package that 42 * serves no purpose other than being referenced by this attribute. 43 * @return base packages to scan 44 * @since 1.3.0 45 */ 46 @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses") 47 Class<?>[] scanBasePackageClasses() default {}; 48 49 }
SpringBootConfiguration的源码
这是Spring Boot项目的配置注解,这也是一个组合注解:
在Spring Boot项目中推选使用@SpringBootConfiguration注解来替代@Configuration注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { }
3.3.2.@EnableAutoConfiguration
启动自动配置,该注解会让Spring Boot根据当前项目所依赖的JAR包自动配置项目的相关的配置项
3.3.3.@ComponentScan
扫描配置,Spring Boot默认扫描@SpringBootApplication所在类的同级包以及他的子包,
所以建议将@SpringBootApplication修饰的入口类放置在项目包下(Group Id+ArtifactId)可以保证SpringBoot项目启动时扫描所有的包
3.3.4.关闭自动配置
如何关闭自动配置那?
通过查看@SpringBootApplication的源码可以看出,关闭特定的自动配置应该使用@SpringBootApplication下的exclude参数,现在已关闭Redis自动配置为例:
@SpringBootApplication(exclude={RedisAutoConfiguration.class})