java - 使用 java config 在 Spring 应用程序中的 Web 项目之外单独存储属性

标签 java spring tomcat7 environment properties-file

我想保存 Tomcat 服务器中的属性。我想初始化一些用于开发的 bean 和一些用于生产的 bean。最好的解决方案是什么?

我尝试使用 Condition 接口(interface),但收到 NullPointerException。看来我没有从我的属性(property)中获得值(value)。我做错了什么?

我将我的属性文件放入 /home/user/apache-tomcat-home-dir/conf/my.properties 并添加

 <Environment name="my_config" value="file:///${catalina.home}/conf/my.properties" type="java.net.URI"/>

/home/user/apache-tomcat-home-dir/conf/context.xml

my.properties中只有一个属性:

profiler.default=dev

这就是我在项目中使用我的属性(property)的方式:

public class ProductionCondition implements Condition {

    @Value("${profiler.default}")
    private String prodProfiler;

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return prodProfiler.equals("production");
    }
}

public class DevCondition implements Condition {

    @Value("${profiler.default}")
    private String coreProfiler;

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//        return coreProfiler.equals("dev");
        return context.getEnvironment().getProperty("profiler.default").contains("dev");
    }
}

我应该配置 PropertySourcesPlaceholderConfigurer 来初始化文件中的属性:

@Configuration(value = "propertiesConfiguration")
@PropertySource(value="file:${catalina.home}/conf/my.properties")
public class PropertiesConfiguration {

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() throws MalformedURLException {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
//        configurer.setLocation(new FileSystemResource(propertyFile));
        return configurer;
    }
}

正确的形式是什么:${catalina.home}${catalina/home} 或者如何指定我的属性文件的相对路径?

在配置类中,我初始化条件bean并指定该配置类依赖于PropertiesConfiguration。

@Configuration
@Import({PropertiesConfiguration.class})
@DependsOn("propertiesConfiguration")
public class ApplicationConfig {

    @Bean
    @Conditional(ProductionCondition.class)
    public FooInterface productionVendorCore() {
        return new ProductionImpl();
    }

    @Bean
    @Conditional(DevCondition.class)
    public FooInterface devVendorCore() {
        return new DevImpl();
    }
//....
}

但无论如何我在 ProductionCondition 中收到 NPE:

return prodProfiler.equals("production");

感谢任何帮助!

最佳答案

不要试图发明自己的轮子,而是尝试使用已经提供的轮子。使用profiles相反。

在您的 tomcat 中,只需将 SPRING_PROFILES_ACTIVE 属性添加到 jndi 中,并使用您想要的值 productdev 即可。或者您可以将其设置为环境或 jvm 属性。

<Environment name="SPRING_PROFILES_ACTIVE" value="production" type="java.lang.String"/>

然后使用 @Profile 注释您的 Bean 定义,并为某些配置文件启用它们。

public class ApplicationConfig {

    @Bean
    @Profile("production")
    public FooInterface productionVendorCore() {
        return new ProductionImpl();
    }

    @Bean
    @Profile("dev")
    public FooInterface devVendorCore() {
        return new DevImpl();
    }
    //....
}

这应该只加载启用的配置文件中的 bean。您无需做任何额外的工作。

关于java - 使用 java config 在 Spring 应用程序中的 Web 项目之外单独存储属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29362786/

相关文章:

java - 从 Java 程序运行 Bash 以在 Raspberry Pi 上捕获网络摄像头图像

java - Java中的ArrayList只打印List中的最后一个对象

java - Hibernate 自引用父子

java - 创建名称为 'entityManagerFactoryBean' 的 bean 时出错

java - 如何在Spring WebClient/DataBuffer中拦截http响应流量?

java - 静态方法在类加载时是否创建对象

java - 什么是 NullPointerException,我该如何解决?

grails - Grails部署:类:Tomcat没有此类warName

tomcat - 网络应用程序。设置 servlet TrackingFilter 后不显示任何内容

Tomcat 不断覆盖 server.xml