我在“ application.properties
”文件夹中有一个Spring Boot应用程序以及一个src/main/resources
文件。我有一个自定义的Servlet过滤器作为独立的jar,它将作为上面的Spring Boot Application中的依赖项作为maven依赖项的一部分进入。自定义Servlet过滤器需要一个属性(例如版本)才能通过Spring Boot主应用“ application.properties
”传递。
我的问题是,如何将version
从Spring引导应用程序属性传递到jar?我尝试使用@Value
,但它总是使我为空。
示例片段:
过滤器
package hello; //delibaretly made the same package as Spring boot App
//Valid Spring Imports
@Component
public class VersionFilter implements Filter {
@Value("${version}")
private String version; //This always returns null.
FilterConfig filterConfig = null;
public void destroy() {}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
}
public void init(FilterConfig fConfig) throws ServletException {
this.filterConfig = fConfig;
}
}
来自https://spring.io/guides/gs/spring-boot/的示例应用程序,我在其中将上述过滤器添加为pom中的jar。另外,在“ application.properties”中添加了版本密钥。
将Registartion Bean过滤为Springboot应用程序的一部分
package hello;
//Valid Spring Imports
import hello.VersionFilter;
@Configuration
public class WebConfig {
//Register VersionFilter
@Bean
public FilterRegistrationBean<VersionFilter> abcFilter() {
FilterRegistrationBean<VersionFilter> filterRegBean = new FilterRegistrationBean<>();
filterRegBean.setFilter(new VersionFilter());
filterRegBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 1 );
return filterRegBean;
}
}
//更新了WebConfig:但是如何设置Order? -现在,通过订单
import hello.VersionFilter;
@Configuration
public class WebConfig {
//Register VersionFilter
@Autowired
private VersionFilter versionFilter;
public FilterRegistrationBean<VersionFilter> abcFilter() {
FilterRegistrationBean<VersionFilter> filterRegBean = new FilterRegistrationBean<VersionFilter>();
filterRegBean.setFilter(versionFilter);
filterRegBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 1 );
return filterRegBean;
}
}