简体   繁体   English

如何将环境自动连接到PropertySourcesPlaceholderConfigurer?

[英]How to autowire Enviroment into a PropertySourcesPlaceholderConfigurer?

I'm trying to set up my Spring app such that a different .properties files is read depending on the configuration profile. 我试图设置我的Spring应用程序,以便根据配置配置文件读取不同的.properties文件。 I'm using java config and so what I'm trying to do is this: 我正在使用Java配置,所以我想做的是这样的:

@Autowired
private static Environment env;

@Bean
public static PropertySourcesPlaceholderConfigurer properties(){
    PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
    String[] profiles = env.getActiveProfiles();
    String filestring = "environment."+profiles[0]+".properties";
    ClassPathResource properties = new ClassPathResource( filestring );
    Resource[] resources = new ClassPathResource[] { properties };
    pspc.setLocations( resources );
    return pspc;
}

However the env.getActiveProfiles() is giving me a NullPointerException , which I assume means that the environment hasn't been injected. 但是env.getActiveProfiles()给了我NullPointerException ,我认为这意味着尚未注入环境。 Any one got any ideas how I can fix this? 任何人都知道如何解决此问题? Or alternatively if this is dumb/impossible how I could go about this better? 或者,如果这是愚蠢的/不可能的,我该如何做得更好?

Just to give you an alternate perspective on your approach (clearly everyone's business case may differ from project to project,) but the type of configuration you are pursuing might lead to other headaches down the road. 只是为您提供一种替代方法的观点(显然每个项目的业务案例可能因项目而异),但是您追求的配置类型可能会导致其他麻烦。 Security comes to mind. 想到安全性。 Usually multiple environments means you are dealing with usernames and passwords for various connections to databases and such. 通常,多个环境意味着您要处理与数据库等的各种连接的用户名和密码。 Storing those values for a production environment in line with your other configurations could expose sensitive data to developers who need have no knowledge of such things. 根据您的其他配置为生产环境存储这些值可能会将敏感数据暴露给不需要此类知识的开发人员。 Rather, if you switch using SPeL expressions and referencing the environment directly, then you can still achieve your runtime configuration but move your settings for each environment to the server (or what-have-you) where those specific configs apply. 相反,如果您使用SPeL表达式进行切换并直接引用环境,那么您仍然可以实现运行时配置,但是将每个环境的设置移动到应用这些特定配置的服务器(或您拥有的)。 Example: 例:

<bean id="myDatabase" class="mypackage.MyDatabase" p:username="#{environment['DB_USERNAME']}" p:password="#{environment['DB_PASSWORD']}" .../>

Then on your server, you can pass in system properties OR set environment variables with your desired username and password, and they will be configured at runtime. 然后在服务器上,您可以传入系统属性或使用所需的用户名和密码设置环境变量,它们将在运行时进行配置。 (The environment expression resolves directly to your Environment instance.) environment表达式直接解析为您的Environment实例。)

Just a thought. 只是一个想法。 =) =)

As @ kungfuters rightly suggested, the business case may differ from application to application. 正如@ kungfuters正确建议的,该业务案例可能因应用程序而异。 Here is another alternative that worked for my application. 这是适用于我的应用程序的另一种选择。

Provide an implementation of following interface: 提供以下接口的实现:

ApplicationContextInitializer<ConfigurableApplicationContext>

Provide implementation of the following method. 提供以下方法的实现。 The logic to identify the profile goes in this method. 识别配置文件的逻辑在此方法中。

initialize(ConfigurableApplicationContext ctx) 

Based on the identification, set the active profile: 根据标识,设置活动配置文件:

this.applicationContext.getEnvironment().setActiveProfiles(<<yourProfileName>>)

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

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