令我惊讶的是,我很难找到这个问题的答案.我看过很多例子,您可以使用@PropertySource为类加载特定的属性文件.我还看到了一些示例,您可以在Spring Boot项目中轻松添加不同的属性文件.但我想要做的是为一个非春季启动的spring项目执行此操作并加载属性文件,以便可以在使用@Component注释的类中注入此文件的值,该类取决于服务器环境.因此,例如,如果我在开发服务器上,我希望加载特定的属性文件,并在生产中使用不同的属性文件.我这样做的原因是因为我的数据和服务层是他们自己的模块.这些模块包含自己的单元测试,可以在其他Spring引导项目中作为自己的模块导入.我需要加载属性文件来为这些使用spring而不是spring boot的模块提供服务.我尝试了以下,但这不起作用.
@Configuration
@Profile("test")
@EnableJpaRepositories("com.hi.repository")
@EnableTransactionManagement
@EnableScheduling
public class InfrastructureConfig {
...
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
Map<String, String> env = System.getenv();
String propertiesFile=null;
String e = env.get("SERVER_ENV");
if (e.equals("dev")) {
propertiesFile = "environment/development.properties";
} else if (e.equals("prod")) {
propertiesFile = "environment/production.properties";
}
configurer.setLocation(new ClassPathResource(propertiesFile));
return configurer;
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个看起来像这样的测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/DealServiceTest-context.xml"})
@ActiveProfiles("test")
public class LogTest {
private static final Logger log = LogManager.getLogger(LogTest.class);
@Autowired
PathsService pathsService;
@Autowired
Environment environment;
@Test
public void testBeans(){
System.out.println("********** WASSUP from LogTest");
System.out.println(environment.getProperty("imageBucket"));
}
Run Code Online (Sandbox Code Playgroud)
尽管测试打印出null,这表明属性文件尚未加载并准备好注入其值.我怎样才能做到这一点?
实际上,您可以在 @PropertySource 注释中使用占位符。请参阅文档:
@PropertySource 资源位置中存在的任何 ${...} 占位符将根据已针对环境注册的一组属性源进行解析。
假设占位符存在于已经注册的属性源之一中,例如系统属性或环境变量,占位符将被解析为相应的值。
我做了一个简单的例子,它接收一个“ property.environment ”值来选择,哪个 .properties 文件应该用作属性源。我的类路径中有两个资源文件——application-test.properties和 application-dev.properties,每个文件都包含一个“ test.property ”值(分别为“test-env”和“dev-env”)。
属性配置:
@Configuration
@PropertySource("classpath:/config/application-${property.environment}.properties")
public class PropertyConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
return propertySourcesPlaceholderConfigurer;
}
}
Run Code Online (Sandbox Code Playgroud)
带有@Value 的组件
@Component
public class TestService {
@Value("${test.property}")
String testProperty;
@PostConstruct
void init() {
System.out.println("---------------------------------------------------------");
System.out.println("Running in " + testProperty + " environment");
System.out.println("---------------------------------------------------------");
}
}
Run Code Online (Sandbox Code Playgroud)
构建命令行示例(它使用测试环境属性运行测试)
mvn clean install -DargLine="-Dproperty.environment=test"
Run Code Online (Sandbox Code Playgroud)
输出
---------------------------------------------------------
Running in test-env environment
---------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
运行命令行示例
java -jar -Dproperty.environment=dev PATH_TO_YOUR_JAR.jar
Run Code Online (Sandbox Code Playgroud)
输出
---------------------------------------------------------
Running in dev-env environment
---------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
您不需要自己设置属性,但可以使用spring配置执行此操作.查看文档:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties
如果您使用的是spring boot - 您需要做的就是为您的环境创建多个属性文件.并且仅适用于您需要覆盖的属性.
所以你的主要属性文件将是
src/main/resources/application.properties
Run Code Online (Sandbox Code Playgroud)
生产
src/main/resources/application-prod.properties
Run Code Online (Sandbox Code Playgroud)
发展
src/main/resources/application-dev.properties
Run Code Online (Sandbox Code Playgroud)
测试
src/main/resources/application-test.properties
Run Code Online (Sandbox Code Playgroud)
然后只使用配置文件名称作为环境变量
java -jar -Dspring.profiles.active=prod demo-0.0.1-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14265 次 |
| 最近记录: |