[英]Spring boot : bean creation profile specific
I am working on a spring boot application.i am using spring boot version: 2.2.4-RELEASE我正在开发 spring 引导应用程序。我正在使用 spring 引导版本:2.2.4-RELEASE
I am trying to create a bean specific for a profile.but the bean is not creating as expected.我正在尝试创建一个特定于配置文件的 bean。但是 bean 没有按预期创建。
Below is my configuration file:下面是我的配置文件:
@Configuration
@Slf4j
public class TestConfig {
//below is the bean i need to be created for dev and test
@Bean
@Profile({“dev”, “test”})
TestObject getTestObject() {
//do something
}
//below is the bean i need to be created for staging and prod
@Bean
@Profile({“staging”, “prod”})
TestObject getTestObject() {
//do something
}
//someother beans common for all profiles
}
Service.java服务.java
@Service
public class Serviceclass {
@Autowired
private TestObject testObj;
//some methods
}
I tried the above way, but the bean is Not getting created for any of the profiles.我尝试了上述方法,但没有为任何配置文件创建 bean。 Any suggestions on how to achieve this would be helpful.
关于如何实现这一目标的任何建议都会有所帮助。
Update : the application is failing to start as one of the service class has dependency on the bean i am trying to create.更新:应用程序无法启动,因为 class 服务之一依赖于我要创建的 bean。
Thanks in advance.提前致谢。
In the note found in在找到的注释中
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Profile.html https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Profile.ZFC35FDC70D5FC69D2369883A822C7
Use distinct Java method names pointing to the same bean name
使用不同的 Java 方法名称指向相同的 bean 名称
You have to active profile for the application.您必须为应用程序激活配置文件。 You can pass runtime argument as as -Djava.profiles.active=dev .
您可以将运行时参数作为-Djava.profiles.active=dev传递。 It will solve your problem.
它会解决你的问题。 Also, need to create environment specific property files like application-{env}.properties
此外,需要创建特定于环境的属性文件,例如application-{env}.properties
I think the problem is the same method name.我认为问题是相同的方法名称。 For some reason if i give the same name (method overloading) we are running into issues.
出于某种原因,如果我给出相同的名称(方法重载),我们就会遇到问题。 Found a similar thread which explains the same.
找到了一个类似的线程,解释相同。
Thanks a lot everyone who tried to solve the issue.非常感谢所有试图解决这个问题的人。
Note: i will try to find the thread and will update the thread here which explains the same.注意:我将尝试找到该线程并将在此处更新该线程以解释相同的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.