title: Spring controller: How to use property ${..} in @RequestMapping?
body: <p>I already found questions with answers but they don't help me.</p>
  
  <p>I have a web servlet project where I use a Spring Controller (4.2.5) and Spring Security (4.0.2). I don't use Spring Boot.</p>
  
  <p>My project works fine.  </p>
  
  <p>But now my task is this:<br>
  <strong>Make <code>@RequestMapping(value={"auth/**"}</code> configurable</strong> (replace <code>"auth/**"</code> with <code>${dm.filterPattern}</code>)</p>
  
  <p><strong>Problem: in <code>@RequestMapping</code> <code>${dm.filterPattern}</code> isn't resolved, although @PropertySource is processed.</strong></p>
  
  <p>This is the entry <code>dm.filterPattern</code> in dmConfig.properties:</p>
  
  <pre><code>dm.filterPattern=/auth/*
  </code></pre>
  
  <p>Here is some essential code, with all Spring annotations.</p>
  
  <h3>Controller:</h3>
  
  <p>The output of the method <code>init()</code> shows me that <code>@PropertySource</code> is processed correctly. <code>env.getProperty("...")</code> returns correct values.</p>
  
  <pre class="lang-java prettyprint-override"><code>@Controller
  @PropertySource("classpath:/dmConfig.properties")
  @RequestMapping(value ={ "${dm.filterPattern}"})
  public class DmProxyController implements ApplicationContextAware
  {
      private Environment env;
  
      @Autowired
      public DmProxyController(Environment env)
      {
          this.env = env;
      }
  
      @RequestMapping(path={"${dm.filterPattern}"} ,method = RequestMethod.POST)
      protected void doPost(HttpServletRequest customerRequest, HttpServletResponse response)
              throws ServletException, IOException, DmException
      {
             // code for POST request
      }
  
      @RequestMapping(path={"${dm.filterPattern}"} ,method = RequestMethod.GET)
      protected void doGet(HttpServletRequest customerRequest, HttpServletResponse response)
              throws ServletException, IOException, DmException
      {
             // code for GET request
      }
  
      @PostConstruct
      public void init() throws ServletException
      {
          RequestMappingHandlerMapping requestMapping=
                  (RequestMappingHandlerMapping) appContext.getBean("requestMappingHandlerMapping");
  
          Map&lt;RequestMappingInfo, HandlerMethod&gt; handlerMethods = requestMapping.getHandlerMethods();
          logger.debug("RequestMapping via dm.filterPattern: {}",
                  env.getProperty("dm.filterPattern"));
                  logger.debug("Handler Methods: {}", handlerMethods.size());
  
          for (RequestMappingInfo mapInfo : handlerMethods.keySet())
          {
              logger.debug(" Mappinginfo: {} --&gt; {}", mapInfo, handlerMethods.get(mapInfo));
          }
      }
  }
  </code></pre>
  
  <h3>Class with bean definitions</h3>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @PropertySource("classpath:/dmConfig.properties")
  @ComponentScan(basePackages = "com.dm.filter, com.dm.controller")
  @EnableTransactionManagement(mode = AdviceMode.PROXY, proxyTargetClass = false)
  @Import({DmSecurityConfigurer.class, DmWebConfigurer.class})
  public class DmRoot
  {
  
  }
  </code></pre>
  
  <h3>DispatcherServletInitializer</h3>
  
  <pre class="lang-java prettyprint-override"><code>public class DmDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
  {
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses()
      { return new Class[]{DmRoot.class};  }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses()
      { return null; }
  
      @Override
      protected String[] getServletMappings()
      { return new String[]{"/"}; }
  
      @Override
      protected String getServletName()
      {  return "dmDispatcherServlet";  }
  
      @Override
      protected void customizeRegistration(ServletRegistration.Dynamic registration)
      {
          super.customizeRegistration(registration);
          registration.setLoadOnStartup(1);
      }
  }
  </code></pre>
  
  <h3>WebConfigurer</h3>
  
  <pre class="lang-java prettyprint-override"><code>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);
      }
  }
  </code></pre>
  
  <h3>SecurityWebApplicationInitializer</h3>
  
  <pre class="lang-java prettyprint-override"><code>public class DmSecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer
  {
      public DmSecurityWebApplicationInitializer()
      {
          // some logging
      }
  
      @Override
      protected void beforeSpringSecurityFilterChain(ServletContext servletContext)
      {     // adding own filters   }
  
      @Override
      protected void afterSpringSecurityFilterChain(ServletContext servletContext)
      {     // adding own filters   }
  }
  </code></pre>
  
  <h3>SecurityConfigurer</h3>
  
  <pre class="lang-java prettyprint-override"><code>@EnableWebMvc
  @EnableWebSecurity
  @PropertySource("classpath:dmConfig.properties")
  public class DmSecurityConfigurer extends WebSecurityConfigurerAdapter
  {
  
      private static Logger logger = LogManager.getLogger(DmSecurityConfigurer.class.getName());
  
      @Autowired
      private Environment env;
  
      @Autowired
      private UserDetailsService dmUserDetailsService;
  
      @Override
      protected void configure(HttpSecurity httpSecurity) throws Exception
      {
          String urlPattern = env.getProperty("dm.springSecurityPattern");
          String realmName = env.getProperty("dm.springSecurityRealm");
  
          httpSecurity.httpBasic().realmName(realmName)
                  .and().userDetailsService(dmUserDetailsService)
                  .authorizeRequests()
                  .antMatchers(urlPattern).authenticated()
                  .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                  .and()
                  .csrf().disable();
      }
  
  }
  </code></pre>