-
启动类的@SpringBootApplication注解:组合了@SpringBootConfiguration、@EnableAutoConfiguration以及@ComponentScan。
@SpringBootConfiguration:表明此类为配置类
@EnableAutoConfiguration:表明让spring-boot根据类路径中的jar包依赖当前项目进行自动配置
@ComponentScan:表明可以让spring扫描到此类,装入到spring容器中。(可以添加过滤器扫描特定类,例如:@SpringBootApplication(exclude = DataSourceAutoConfiguration.class))
(使用SpringBootApplication注解,系统会去入口类的同级包以及下级包中去扫描实体类)
-
Spring Boot的配置文件
Spring Boot使用一个全局的配置文件application.properties或者application.yml,配置文件放在src/main/resources目录下。
Profile配置问题:
即在不同环境下使用不同的配置文件。
在Spring Boot中,全局Profile配置使用application-{profile}.yml来定义,在application.yml中通过spring.profiles.active来指定使用哪个Profile。生产环境下使用application-prod.yml(在application.yml中指定spring.profiles.active=prod),开发环境下使用application-dev.yml(在application.yml中指定spring.profiles.active=dev)。
日志配置:
删除pom文件对所有日志jar包的引用与依赖,增加依赖包[spring-boot-starter-log4j.jar]
增加log4j.properties配置(如果想要使用yml文件需要在pom中添加[jackson-dataformat-yaml]的依赖)
根据不同环境指定不同log4j文件:可以在application-{profile}.yml中通过[logging.config=classpath:log4j2-{profile}.properties]来指定
-
拦截器中依赖注入为null
因为拦截器加载的时间点在springcontext之前(即自动bean初始化之前),所以拦截器中注入自然就是null
解决方案:在配置拦截器链的类中先注入这个拦截器
@Bean public Interceptor getInterceptor() { return new Interceptor(); } @Configuration class WebMvcConfiger extends WebMvcConfigurerAdapter{ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(getInterceptor()).addPathPatterns("/**"); } }










网友评论