Spring boot的@Configuration
就在我惊艳于spring 4的AbstractAnnotationConfigDispatcherServletInitializer小巧简洁(如下)的时候却发现spring boot下面竟然无效。
public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override
protected String[] getServletMappings() {
return new String[] { "/" };
} @Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { TracingConfiguration.class, AppConfiguration.class };
} /** Ensures tracing is setup for all HTTP requests. */
@Override
protected Filter[] getServletFilters() {
return new Filter[] { new DelegatingTracingFilter() };
} @Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return null;
}
}
后来经过调研发现其实是需要通过@Configuration(注意类级别注解)的类来进行处理,比如下例就是通过FilterRegistrationBean 来指定filter(相当于上面的getServletFilters函数):
@Configuration
public class AppConfiguration {
@Autowired
private AutowireCapableBeanFactory beanFactory; @Bean
public FilterRegistrationBean myFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
Filter myFilter = new DelegatingTracingFilter();
beanFactory.autowireBean(myFilter);
registration.setFilter(myFilter);
//registration.addUrlPatterns("/myfilterpath/*");
return registration;
}
}
与之类似的还有下面的bean定义对应于原生Spring的servlet以及多个Filter:
@Bean
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new DispatcherServlet(), "/");
registration.setAsyncSupported(true);
return registration;
} @Bean
public Filter compositeFilter() {
CompositeFilter compositeFilter = new CompositeFilter();
compositeFilter.setFilters(ImmutableList.of(new CorsFilter(), shiroFilter));
return compositeFilter
}
Spring boot的@Configuration的更多相关文章
- Spring Boot Externalized Configuration
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html Ex ...
- Spring Boot通过Configuration配置多数据源
本文结合SpringBoot + MyBatis + MySql进行多数据源配置,DataSource信息采用自定义dataSource.properties进行配置. 1.文件结构如下: 2.1 p ...
- Spring boot 使用 configuration 获取的属性为 null
1. 未设置 getter(),setter()方法,导致属性值注入失败: 2. spring 未扫描到该组件,在其他类中注入该对象失败,可在配置类添加 @configuration 或者 @comp ...
- spring boot -Properties & configuration
72. Properties & configuration72.1 Automatically expand properties at build timeRather than hard ...
- idea 启动项目提示 Command line is too long. Shorten command line for Application or also for Spring Boot default configuration.
在.idea 文件夹中打开workspace.xml文件找到<component name="PropertiesComponent">,在标签里加一行 <pr ...
- 【springcloud】【idea】启动服务报错Command line is too long. Shorten command line for XXXApplication or also for Spring Boot default configuration.
在workspace.xml 在标签<component name="PropertiesComponent">里 添加<property name=" ...
- Error running 'App': Command line is too long. Shorten command line for App or also for Spring Boot default configuration.
找到标签 <component name="PropertiesComponent">.在标签里加一行 : <property name="dynam ...
- Spring Boot 探索系列 - 自动化配置篇
26. Logging Prev Part IV. Spring Boot features Next 26. Logging Spring Boot uses Commons Logging f ...
- Spring Boot整合Quartz实现定时任务表配置
最近有个小项目要做,spring mvc下的task设置一直不太灵活,因此在Spring Boot上想做到灵活的管理定时任务.需求就是,当项目启动的时候,如果有定时任务则加载进来,生成schedule ...
随机推荐
- P1270 “访问”美术馆(树形dp)
P1270 “访问”美术馆 艺术馆最多有100个展室 ------> 节点数$<=100*2<2^{8}=256$ 所以可以开一个$f[i][j]$表示到第$i$个点为止花去$j$分 ...
- SQL 中 not in 查询不到数据问题
在开发的过程中,遇到过not in 始终查询不到数据问题 select * from T_CustomerInfo where CustomerID not in (select CustomerID ...
- UVa 11552 最小的块数(序列划分模型:状态设计)
https://vjudge.net/problem/UVA-11552 题意:输入一个正整数k和字符串S,字符串的长度保证为k的倍数.把S的字符按照从左到右的顺序每k个分成一组,每组之间可以任意重排 ...
- eclipse中下载maven插件解决办法
https://blog.csdn.net/qq_30546099/article/details/71195446 解决Eclipse Maven插件的最佳方案 https://www.cnblog ...
- JVM知识总结-运行时区域划分
区域简介 JVM运行时区域有些随着虚拟机进程的启动而存在,有些依赖于用户线程的启动和结束而建立和销毁,大致分为以下几类:方法区,虚拟机栈,本地方法栈,堆,程序计数器,概念图如下(源于<深入理解J ...
- Html之网页分屏浏览
Hi! Every Body!Welcome to my blog! My name is Caiduping,I hope we learn to make progress together! ...
- Kolakoski数列
2018-04-16 15:40:16 Kolakoski序列是一个仅由1和2组成的无限数列,是一种通过“自描述”来定义的数列.他在整数数列大全网站上排名第二位,足见该数列在组合数学界中的重要性. K ...
- 13个能快速开发android的经典项目
一.okhttp一个让网络请求更简单的框架 项目地址 https://github.com/jeasonlzy/okhttp-OkGo 二. TwinklingRefreshLayout-下拉刷新和上 ...
- fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突——我的解决方案
本文转载于:http://blog.csdn.net/tfy1028/article/details/8660823 win7 下,安装的VS2010,然后搭配opencv2.4.3运行,报错为:fa ...
- hdu5618
题解: CDQ分治 三重分治 第一重排序 第二重CDQ 第三重树状数组 代码: #include<cstdio> #include<cmath> #include<cst ...