Java Spring控制器:如何在@RequestMapping中使用属性${..}?

Java Spring控制器:如何在@RequestMapping中使用属性${..}?,java,spring-mvc,request-mapping,Java,Spring Mvc,Request Mapping,我已经找到了带答案的问题,但它们对我没有帮助 我有一个WebServlet项目,其中使用了Spring控制器(4.2.5)和Spring安全性(4.0.2)。我不使用弹簧靴 我的项目运作良好 但现在我的任务是: 使@RequestMapping(值={“auth/**”}可配置(将“auth/**”替换为${dm.filterPattern}) 问题:在@RequestMapping${dm.filterPattern}中,虽然已处理@PropertySource,但问题未得到解决。 这是dmC

我已经找到了带答案的问题,但它们对我没有帮助

我有一个WebServlet项目,其中使用了Spring控制器(4.2.5)和Spring安全性(4.0.2)。我不使用弹簧靴

我的项目运作良好

但现在我的任务是:
使
@RequestMapping(值={“auth/**”}
可配置
(将
“auth/**”
替换为
${dm.filterPattern}

问题:在
@RequestMapping
${dm.filterPattern}
中,虽然已处理@PropertySource,但问题未得到解决。

这是dmConfig.properties中的条目
dm.filterPattern

dm.filterPattern=/auth/*
下面是一些基本代码,以及所有Spring注释

控制器: 方法
init()
的输出显示
@PropertySource
处理正确。
env.getProperty(“…”)
返回正确的值

@控制器
@PropertySource(“类路径:/dmConfig.properties”)
@RequestMapping(值={${dm.filterPattern}})
公共类DmProxyController实现ApplicationContextAware
{
私人环境署;
@自动连线
公共DmProxyController(环境环境)
{
this.env=env;
}
@RequestMapping(路径={${dm.filterPattern}},方法=RequestMethod.POST)
受保护的void doPost(HttpServletRequest customerRequest、HttpServletResponse响应)
抛出ServletException、IOException、DmException
{
//邮政编码
}
@RequestMapping(路径={${dm.filterPattern}},方法=RequestMethod.GET)
受保护的无效数据集(HttpServletRequest customerRequest、HttpServletResponse响应)
抛出ServletException、IOException、DmException
{
//获取请求的代码
}
@施工后
public void init()引发ServletException
{
RequestMappingHandlerMappingRequestMapping=
(RequestMappingHandlerMapping)appContext.getBean(“RequestMappingHandlerMapping”);
Map handlerMethods=requestMapping.getHandlerMethods();
debug(“通过dm.filterPattern:{}请求映射”,
环境getProperty(“dm.filterPattern”);
debug(“Handler方法:{}”,handlerMethods.size());
对于(RequestMappingInfo mapInfo:handlerMethods.keySet())
{
debug(“Mappinginfo:{}-->{}”,mapInfo,handlerMethods.get(mapInfo));
}
}
}
使用bean定义初始化
@配置
@PropertySource(“类路径:/dmConfig.properties”)
@ComponentScan(basePackages=“com.dm.filter,com.dm.controller”)
@EnableTransactionManagement(mode=AdviceMode.PROXY,proxyTargetClass=false)
@导入({dmsecurityconfig.class,dmwebconfig.class})
公共类DmRoot
{
}
调度员服务初始化器
公共类DmDispatcherServletInitializer扩展了AbstractAnnotationConfigDispatcherServletInitializer
{
@凌驾
受保护类[]getRootConfigClasses()
{返回新类[]{DmRoot.Class};}
@凌驾
受保护类[]getServletConfigClasses()
{返回null;}
@凌驾
受保护的字符串[]getServletMappings()
{返回新字符串[]{”/“};}
@凌驾
受保护的字符串getServletName()
{返回“dmDispatcherServlet”;}
@凌驾
受保护的无效自定义注册(ServletRegistration.Dynamic registration)
{
超级用户注册(注册);
注册。设置加载启动(1);
}
}
网络配置器
公共类DMWebConfigure扩展了WebMVCConfigureAdapter
{
@凌驾
public void addResourceHandlers(ResourceHandlerRegistry注册表)
{
super.addResourceHandlers(注册表);
registry.addResourceHandler(“/index.html”).addResourceLocations(“/”);
注册表.setOrder(整数.MAX_值-5);
}
}
SecurityWebApplicationInitializer
公共类DmSecurityWebApplicationInitializer扩展了AbstractSecurityWebApplicationInitializer
{
公共DmSecurityWebApplicationInitializer()
{
//一些伐木
}
@凌驾
受保护的SpringSecurityFilterChain之前的void(ServletContext ServletContext)
{//添加自己的筛选器}
@凌驾
受保护的void afterSpringSecurityFilterChain(ServletContext ServletContext)
{//添加自己的筛选器}
}
证券配置师
@EnableWebMvc
@启用Web安全性
@PropertySource(“类路径:dmConfig.properties”)
公共类dmsecurityconfig扩展了websecurityconfig适配器
{
私有静态记录器Logger=LogManager.getLogger(dmsecurityconfig.class.getName());
@自动连线
私人环境署;
@自动连线
私有用户详细信息服务dmUserDetailsService;
@凌驾
受保护的无效配置(HttpSecurity HttpSecurity)引发异常
{
字符串urlPattern=env.getProperty(“dm.springSecurityPattern”);
字符串realmName=env.getProperty(“dm.springSecurityRealm”);
httpSecurity.httpBasic().realmName(realmName)
.和().userDetailsService(dmUserDetailsService)
.授权请求()
.antMatchers(urlPattern).authenticated()
.and().sessionManagement().sessionCreationPolicy(sessionCreationPolicy.STATELESS)
.及()
.csrf().disable();
}
}

在spring上下文中,
PropertySourcesPlaceholderConfigurer
可能比控制器初始化得晚,因此无法解析值。请尝试在根配置文件之一中为
PropertySourcesPlaceholderConfigurer
添加显式bean定义,如下所示

@PropertySource("classpath:/dmConfig.properties")
public class DmWebConfigurer extends WebMvcConfigurerAdapter
{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("/index.html").addResourceLocations("/");
        registry.setOrder(Integer.MAX_VALUE-5);
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
您可以在
init()
方法中正确地看到这些值的原因是,在初始化所有bean(包括
propertysourcesplaceconfigurer
)后调用了该方法,它的工作方式如下