Spring Boot:基于配置文件的Bean创建

huangapple go评论58阅读模式
英文:

Spring boot : bean creation profile specific

问题

我正在开发一个Spring Boot应用程序。我正在使用Spring Boot版本:2.2.4-RELEASE

我试图为特定配置创建一个Bean,但是这个Bean没有按预期创建。

以下是我的配置文件:

@Configuration
@Slf4j
public class TestConfig {

  // 下面是我想要在开发和测试环境下创建的Bean
  @Bean
  @Profile({"dev", "test"})
  TestObject getTestObject() {

        // 做一些操作
  }

  // 下面是我想要在暂存和生产环境下创建的Bean
  @Bean
  @Profile({"staging", "prod"})
  TestObject getTestObject() {

        // 做一些操作
  }

  // 其他一些对所有配置都通用的Bean

}

Service.java

@Service
public class Serviceclass {

  @Autowired
   private TestObject testObj;

   // 一些方法

}

我尝试了上述方法,但是这个Bean在任何配置下都没有被创建。关于如何实现这一点的任何建议将会很有帮助。

更新:应用程序无法启动,因为其中一个服务类依赖于我正在尝试创建的Bean。

提前致谢。

英文:

I am working on a spring boot application.i am using spring boot version: 2.2.4-RELEASE

I am trying to create a bean specific for a profile.but the bean is not creating as expected.

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

@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. 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.

Thanks in advance.

答案1

得分: 1

在找到的注释中:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Profile.html

使用不同的Java方法名称指向相同的bean名称

英文:

In the note found in

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Profile.html

> Use distinct Java method names pointing to the same bean name

答案2

得分: 0

你必须为应用程序激活配置文件。您可以通过运行时参数传递 -Djava.profiles.active=dev。这将解决您的问题。
此外,还需要创建环境特定的属性文件,比如 application-{env}.properties

英文:

You have to active profile for the application. You can pass runtime argument as as -Djava.profiles.active=dev. It will solve your problem.
Also, need to create environment specific property files like application-{env}.properties

答案3

得分: -1

我认为问题出在方法名相同上。因为某种原因,如果我给出相同的名字(方法重载),我们会遇到问题。找到了一个类似的帖子,解释了相同的问题。

非常感谢所有试图解决这个问题的人。

注意:我会尝试找到那个帖子,并会在这里更新那个帖子,解释了相同的问题。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月20日 11:45:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63497990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定