简体   繁体   English

Java Spring ApplicationContext配置

[英]Java Spring ApplicationContext Configuration

I'm in the process of moving all of my Spring Configurations to Java code. 我正在将所有Spring配置迁移到Java代码。 I've run into a problem where I now want to set which profile I am using based on a command line switch or maven profile, etc... I also want to avoid having to place all of the same annotations on each of my test classes. 我遇到了一个问题,现在我想根据命令行开关或Maven配置文件等来设置要使用的配置文件...我也想避免在每个测试中都放置所有相同的批注类。 This is not a web application, but rather a functional test suite. 这不是Web应用程序,而是功能测试套件。

Here is my attempt: 这是我的尝试:

public class CompanyApplicationContextInitializer 
  implements ApplicationContextInitializer<ConfigurableApplicationContext> {

  @Override
  public void initialize(final ConfigurableApplicationContext applicationContext) {
    final AnnotationConfigApplicationContext rootContext = new AnnotationConfigApplicationContext();
    rootContext.getEnvironment().setActiveProfiles(System.getProperty("spring.profile.active", "local"));
    rootContext.register(LocalConfiguration.class, SauceLabsConfiguration.class);
  }
}

Then I have my tests annotated with the following: 然后,我的测试将带有以下注释:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CompanyApplicationContextInitializer.class)

However when I attempt to run my tests, my autowired pieces are not being located. 但是,当我尝试运行测试时,找不到自动接线的零件。 Am I on the right track at all? 我完全走对了吗? How can I wire in this class to programatically set my ApplicationContext? 如何连接此类以编程方式设置ApplicationContext?

The problem with your example above is that you're passing an ApplicationContextInitializer class @ContextConfiguration#classes. 上面的示例的问题在于,您正在传递ApplicationContextInitializer类@ ContextConfiguration#classes。 The #classes attribute is intended to accept classes marked with Spring's @Configuration annotation. #classes属性旨在接受标有Spring的@Configuration批注的类。

ApplicationContextInitializer is intended for use primarily in web applications, where it is difficult to get programmatic access to the WebApplicationContext. ApplicationContextInitializer主要用于Web应用程序,因为在Web应用程序中很难以编程方式访问WebApplicationContext。 The "contextInitializerClasses" init-param can be passed to the Spring DispatcherServlet, and Spring will call your ACI implementation at the right time, allowing you to manipulate the application context prior to #refresh(). 可以将“ contextInitializerClasses” init参数传递给Spring DispatcherServlet,Spring会在正确的时间调用您的ACI实现,从而允许您在#refresh()之前操作应用程序上下文。

In your case, it appears you are concerned only with activating profiles for an integration test. 就您而言,您似乎只关心激活集成测试的概要文件。 So your ACI is unnecessary. 因此,您的ACI是不必要的。 Mark your integration test with Spring's @ActiveProfiles annotation to dictate which profiles are active. 使用Spring的@ActiveProfiles批注标记集成测试,以指示哪些配置文件处于活动状态。

Note that if spring.profiles.active has been set as a JVM system property or environment variable, the specified profile(s) will be activated automatically. 请注意,如果将spring.profiles.active设置为JVM系统属性或环境变量,则将自动激活指定的配置文件。 ie there is no need to call System#getProperty as you do in your ACI implementation. 即,无需像在ACI实现中那样调用System#getProperty。 One thing to note, however, is that based on the logic in your ACI implementation, it appears you want to fall back to a profile named "local" if spring.profiles.active is note supplied as a system property or environment variable. 但是要注意的一件事是,基于ACI实现中的逻辑,如果spring.profiles.active作为系统属性或环境变量提供,则您似乎希望退回到名为“ local”的配置文件。 You may be interested to know that there is a "reserved default profile" named literally "default". 您可能想知道有一个名为“默认”的“保留的默认配置文件”。 This probably has the same semantics you're looking for with your "local" profile. 这可能与您在“本地”配置文件中查找的语义相同。 Consider renaming your 'local' profile to 'default'. 考虑将您的“本地”个人资料重命名为“默认”。

Finally, note that there does exist an open improvement request for providing ApplicationContextInitializer support in @ContextConfiguration classes: https://jira.springsource.org/browse/SPR-9011 . 最后,请注意,确实存在一个开放的改进请求,用于在@ContextConfiguration类中提供ApplicationContextInitializer支持: https ://jira.springsource.org/browse/SPR-9011。 You might want to put a watch on that. 您可能想在上面放一块手表。 It would, for example, allow you a simple option for programmatically activating 'local' if no other profiles are active. 例如,如果没有其他配置文件处于活动状态,它将为您提供一个简单的选项,以编程方式激活“本地”。

Try adding the locations of your app context XML to the second annotation: 尝试将应用上下文XML的位置添加到第二个注释中:

@ContextConfiguration(locations = {
    "classpath:applicationContext.xml"
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM