在启动时填充SpringServlet上下文

在启动时填充SpringServlet上下文,spring,servlets,Spring,Servlets,这可能是一个重复的问题 我的问题是:我想从属性文件中读取一个属性,并在启动应用程序时将其放入servlet上下文中 谁能帮我一下吗 提前感谢。实现Spring的ApplicationListener: @Component public class MyApplicationListener implements ApplicationListener { /* if you want to set predefined properties you even don't have to

这可能是一个重复的问题

我的问题是:我想从属性文件中读取一个属性,并在启动应用程序时将其放入servlet上下文中

谁能帮我一下吗


提前感谢。

实现Spring的ApplicationListener:

@Component
public class MyApplicationListener implements ApplicationListener  {

  /* if you want to set predefined properties you even don't have to load properties filed - you can directly inject properties values ... you can configure it in applicationContext.xml
   <util:list id="locations">
      <value>classpath:appconfig1.properties</value>
      <value>classpath:appconfig2.properties</value>
   </util:list>
   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
              p:locations-ref="locations" />
  */         

  @Value("${myproperty1}") private String myProperty1;
  @Value("${myproperty2}") private String myProperty2;
  @Value("${myproperty3}") private String myProperty3;

  public void onApplicationEvent(ApplicationEvent event) {

    if (event instanceof ContextClosedEvent) {
        applicationClosed();
        return;
    }

    if (!(event instanceof ContextRefreshedEvent)) return;
    ContextRefreshedEvent e = (ContextRefreshedEvent) event;
    ApplicationContext appContext = e.getApplicationContext();
    if (!(appContext instanceof WebApplicationContext)) return;
    WebApplicationContext ctx = (WebApplicationContext) e.getApplicationContext();
    ServletContext context = ctx.getServletContext();


    context.setAttribute("myProperty1", myProperty1);
    context.setAttribute("myProperty2", myProperty2);
    context.setAttribute("myProperty3", myProperty3);

  }
}
@组件
公共类MyApplicationListener实现ApplicationListener{
/*如果您想设置预定义属性,甚至不必加载属性字段-您可以直接插入属性值…您可以在applicationContext.xml中配置它
类路径:appconfig1.properties
类路径:appconfig2.properties
*/         
@值(“${myproperty1}”)私有字符串myproperty1;
@值(“${myproperty2}”)私有字符串myproperty2;
@值(“${myproperty3}”)私有字符串myproperty3;
ApplicationEvent上的公共无效(ApplicationEvent事件){
if(ContextClosedEvent的事件实例){
应用程序关闭();
返回;
}
if(!(ContextRefreshedEvent的事件实例))返回;
ContextRefreshedEvent e=(ContextRefreshedEvent)事件;
ApplicationContext appContext=e.getApplicationContext();
if(!(WebApplicationContext的appContext实例))返回;
WebApplicationContext ctx=(WebApplicationContext)e.getApplicationContext();
ServletContext=ctx.getServletContext();
setAttribute(“myProperty1”,myProperty1);
setAttribute(“myProperty2”,myProperty2);
setAttribute(“myProperty3”,myProperty3);
}
}

实现Spring的应用程序侦听器:

@Component
public class MyApplicationListener implements ApplicationListener  {

  /* if you want to set predefined properties you even don't have to load properties filed - you can directly inject properties values ... you can configure it in applicationContext.xml
   <util:list id="locations">
      <value>classpath:appconfig1.properties</value>
      <value>classpath:appconfig2.properties</value>
   </util:list>
   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
              p:locations-ref="locations" />
  */         

  @Value("${myproperty1}") private String myProperty1;
  @Value("${myproperty2}") private String myProperty2;
  @Value("${myproperty3}") private String myProperty3;

  public void onApplicationEvent(ApplicationEvent event) {

    if (event instanceof ContextClosedEvent) {
        applicationClosed();
        return;
    }

    if (!(event instanceof ContextRefreshedEvent)) return;
    ContextRefreshedEvent e = (ContextRefreshedEvent) event;
    ApplicationContext appContext = e.getApplicationContext();
    if (!(appContext instanceof WebApplicationContext)) return;
    WebApplicationContext ctx = (WebApplicationContext) e.getApplicationContext();
    ServletContext context = ctx.getServletContext();


    context.setAttribute("myProperty1", myProperty1);
    context.setAttribute("myProperty2", myProperty2);
    context.setAttribute("myProperty3", myProperty3);

  }
}
@组件
公共类MyApplicationListener实现ApplicationListener{
/*如果您想设置预定义属性,甚至不必加载属性字段-您可以直接插入属性值…您可以在applicationContext.xml中配置它
类路径:appconfig1.properties
类路径:appconfig2.properties
*/         
@值(“${myproperty1}”)私有字符串myproperty1;
@值(“${myproperty2}”)私有字符串myproperty2;
@值(“${myproperty3}”)私有字符串myproperty3;
ApplicationEvent上的公共无效(ApplicationEvent事件){
if(ContextClosedEvent的事件实例){
应用程序关闭();
返回;
}
if(!(ContextRefreshedEvent的事件实例))返回;
ContextRefreshedEvent e=(ContextRefreshedEvent)事件;
ApplicationContext appContext=e.getApplicationContext();
if(!(WebApplicationContext的appContext实例))返回;
WebApplicationContext ctx=(WebApplicationContext)e.getApplicationContext();
ServletContext=ctx.getServletContext();
setAttribute(“myProperty1”,myProperty1);
setAttribute(“myProperty2”,myProperty2);
setAttribute(“myProperty3”,myProperty3);
}
}

为什么在servlet上下文中?到目前为止您尝试了什么?为什么在servlet上下文中?到目前为止您尝试了什么?对我来说很有效,但一个小问题是:如何知道该事件是servlet上下文启动事件。我在Spring文档中没有发现这一点。仅当事件为
ContextRefreshedEvent
类型(可以简单地从我提供的代码中理解)时才应用该操作,并且今天可以更容易地完成:
公共类MyApplicationListener实现ApplicationListener
(Spring将为您筛选事件类型).为我工作,但有一个小问题:如何知道该事件是servlet上下文启动事件。我在Spring文档中没有发现这一点。仅当事件为
ContextRefreshedEvent
类型(可以简单地从我提供的代码中理解)时才应用该操作,并且今天可以更容易地完成:
公共类MyApplicationListener实现ApplicationListener
(Spring将为您过滤事件类型)。