Servlet-Spring-属性占位符

时间:2018-12-11 12:05:22

标签: spring servlets property-placeholder

我有一个名为“ registrows.properties”的属性文件。

我在applicationContext.xml中配置了一个属性占位符:

<context:property-placeholder location="classpath:registrows.properties"  />

我在web.xml中配置了一个“ ContextLoaderListener”:

<context-param>
    <param-name>contextConfigLocation </param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

我想在init servlet方法中访问此属性值。我尝试使用@Value批注,但不起作用:

public class MyServlet extends HttpServlet
{
   @Value( "${app1.myproperty}" )
   private String myProperty;

   public MyServlet ()
   {
      super();
   }

   @Override
   public void init(ServletConfig config) throws ServletException
   {
       super.init(config);

       System.out.println(myProperty);
   }

   public void setMyProperty (String value)
   {
      this.myProperty = value;
   }

   public String getMyProperty ()
   {
      return this.myProperty;
   }

}

我该怎么办?

我正在使用spring-mvc 5.0.4。

谢谢

0 个答案:

没有答案