【问题标题】:Spring properites in multiple contexts using PropertySourcesPlaceholderConfigurer使用 PropertySourcesPlaceholderConfigurer 在多个上下文中的 Spring 属性
【发布时间】:2015-01-13 07:33:28
【问题描述】:

好吧,我已经打了太久了,是时候寻求帮助了。帮助?! 我无法使用 PropertySourcesPlaceholderConfigurer 让我的属性在我的一个上下文中工作。

我有两种情况: 一个根上下文:

AnnotationConfigWebApplicationContext rootContext = createContext(InfrastructureContextConfiguration.class);

还有一个dispatcherContext:

AnnotationConfigWebApplicationContext dispatcherContext = createContext(WebMvcContextConfiguration.class);

这些会像这样加载:

public class MyWebApplicationInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
   registerListener(servletContext);
   registerDispatcherServlet(servletContext);
}

private void registerDispatcherServlet(ServletContext servletContext) {
   AnnotationConfigWebApplicationContext dispatcherContext = createContext(WebMvcContextConfiguration.class, WebFlowContextConfiguration.class);
   ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(dispatcherContext));
   dispatcher.setLoadOnStartup(1);
   dispatcher.addMapping("/");
}

private void registerListener(ServletContext servletContext) {
  AnnotationConfigWebApplicationContext rootContext = createContext(InfrastructureContextConfiguration.class );
  servletContext.addListener(new ContextLoaderListener(rootContext));
  servletContext.addListener(new RequestContextListener());
}

我创建了 PropertySourcesPlaceholderConfigurer 并在 dispatcherContext WebMvcContextConfiguration 中设置了@PropertySource:

@Configuration
@EnableWebMvc
@PropertySource("classpath:my.properties")
@ComponentScan(basePackages = { "com.me.my.web" })
public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter {
@Bean
  public static PropertySourcesPlaceholderConfigurer  propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
   }
}

因此可以使用以下方法访问此 dispatcherContext 中的属性:

@Value( "${recaptcha.private.key}" )
private String recaptchaPrivateKey;   

问题是我无法从我的 rootContext 访问这些属性。我已经尝试过@value 注释。我已经尝试在 InfrastructureContextConfiguration 类中创建第二个 PropertySourcesPlaceHolderConfigurer,呃!

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:my.properties")
@ComponentScan(basePackages = { "com.me.my.service", "com.me.my.repository",
  "com.me.my.domain.support" })
public class InfrastructureContextConfiguration {

//load properties from my.properties file 
@Value( "${mysql.db.driver.class}" )  //this is always null!   
private String mysqlDriverClass ;
...
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
}  

@Bean
public DataSource dataSource() {
   BasicDataSource dataSource = new BasicDataSource();
   dataSource.setDriverClassName(mysqlDriverClass);
   ...
  }
 ...
}

更新:我已将 PropertySourcesPlaceHolderConfigurer 和 @PropertySource("classpath:my.properties") 添加到我的 InfrastructureContextConfiguration。我在上面更改了这个类以反映我的更改。使用调试器单步执行 InfrastructureContextConfiguration 我最初看到任何注入的属性都是空的,但最终它们都有值。例如,在 Datasource dataSource() 方法中,“mysqlDriverClass”为空,因此失败,但稍后在此类中,其他 bean 由其他注入的具有良好/非空值的属性构造。在什么时候我可以安全地尝试访问这些属性值?

回答我的更新我发现了我的问题。我在@Value 注入属性之前声明了@Autowired DataSource 成员变量,因此在解析注入属性之前,该类正在初始化数据源。

【问题讨论】:

  • 我对你的问题并不感到惊讶。您可以拥有多个 Web 上下文,但始终只有一个根上下文。 Web 上下文不会与另一个上下文共享它们的属性。例如,您可以为每个 Web 上下文定义 2 个属性 recaptcha.private.key。哪一个应该成为你的根源?我认为最适合您的是在您的InfrastructureContextConfiguration 中定义另一个@PropertySource 以正确注入。
  • 谢谢,这很有帮助。

标签: spring spring-mvc properties


【解决方案1】:

正如@Patouche 提到的,在根应用程序上下文中定义的 bean 可以在所有 Web 应用程序上下文中访问,但反过来就不行。

每个 DispatcherServlet 都有一个关联的 Web applicationContext,它继承自根 Web 应用程序上下文。如果您需要根 Web 应用程序上下文中的值或 bean,那么您必须在那里定义它。

有关详细信息,请参阅此讨论。它还具有相关 Spring 文档的链接。 Spring root WebApplicationContext for Servlet

【讨论】:

    猜你喜欢
    • 2013-03-08
    • 2012-05-30
    • 2021-02-12
    • 2014-12-03
    • 1970-01-01
    • 2015-07-23
    • 2014-06-30
    • 1970-01-01
    • 2011-12-24
    相关资源
    最近更新 更多