[英]How to create multiple beans at runtime for different profiles in Spring Boot?
In my project I have following properties files在我的项目中,我有以下属性文件
application.properties
spring.application.name=Profiles
spring.profiles.active=dev,qa
spring.message=Hello world from localhost environment
application-dev.yml
server.port=9091
application-qa.yml
server.port=9091
And below Java classes在 Java 类下面
ProfileConfig.java
@Profile({"dev", "qa"})
@Configuration
public class ProfileConfig {
@Autowired
private Environment environment;
private static Logger LOGGER = LoggerFactory.getLogger(ProfileConfig.class);
@Bean
public void config() {
System.out.println("****************SERVER.PORT " + environment.getProperty("server.port"));
LOGGER.info("Succesfully loaded the environment.");
}
}
SpringbootProfiles.java
@SpringBootApplication
public class Springbootprofiles {
private static Logger LOGGER = LoggerFactory.getLogger(Springbootprofiles.class);
public static void main(String[] args) {
SpringApplication.run(Springbootprofiles.class, args);
LOGGER.info("Springboot profiles application is running successfully.");
}
}
OUTPUT is ****************SERVER.PORT 10092 Succesfully loaded theenvironment. OUTPUT is ****************SERVER.PORT 10092 成功加载环境。
Question - 1) Why is port only from qa profile printed.问题 - 1)为什么只打印 qa 配置文件中的端口。 As dev is also an active profile, I expected port printed for dev and qa as below
由于 dev 也是一个活动配置文件,我预计为 dev 和 qa 打印的端口如下
****************SERVER.PORT 10091 Succesfully loaded theenvironment. ****************SERVER.PORT 10091 成功加载环境。
****************SERVER.PORT 10092 Succesfully loaded theenvironment. ****************SERVER.PORT 10092 成功加载环境。
2)
Is it possible to have two bean created at runtime, one for dev and other for qa ? 2)
是否可以在运行时创建两个 bean,一个用于 dev 另一个用于 qa ? If so, how can I read bean for qa and for dev?如果是这样,我如何为 qa 和 dev 读取 bean?
you could instance your application twice and choose a specific profile for each one ( I think there is no way to start the same application starting in two ports )您可以将应用程序实例化两次并为每个应用程序选择一个特定的配置文件(我认为无法从两个端口启动相同的应用程序)
You can tell spring to use an specific properties passing the environment in one of ways below:您可以通过以下方式之一告诉 spring 使用传递环境的特定属性:
spring.profiles.active=prd
inside application.properties filespring.profiles.active=prd
放在 application.properties 文件中--spring.profiles.active=dev
( try config in your IDE for development using )--spring.profiles.active=dev
(在您的 IDE 中尝试使用配置进行开发)java -jar myjar.jar --spring.profiles.active=dev
java -jar myjar.jar --spring.profiles.active=dev
SET SPRING_ACTIVES_PROFILE=dev
SET SPRING_ACTIVES_PROFILE=dev
/container SET SPRING_ACTIVES_PROFILE=dev
中SET SPRING_ACTIVES_PROFILE=dev
If you need run your tests in a specific configuration ( vars, database, settings ), It's possible to pass which .properties
will be used如果您需要在特定配置(vars、database、settings)中运行测试,则可以传递将使用哪个
.properties
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
@TestPropertySource(locations = "classpath:application-qa.properties")
public class RunTest_from_onlytests_properties {
@Autowired
private MockMvc mockMvc;
@Test // org.junit.jupiter.api.Test
public void test() throws Exception{
// ...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.