-1

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.

JingJong
  • 207
  • 1
  • 3
  • 14
  • what do you mean `bean is Not getting created for any of the profiles` ? can you show some output or error ? – Ryuzaki L Aug 20 '20 at 04:00
  • @Deadpool application is failing to start as in one of the service has a dependency. Updating the question. – JingJong Aug 20 '20 at 04:02
  • Where are you passing the active profile? can you add your YAML file and the command which you are using to run the application – Deepak Kumar Aug 20 '20 at 04:15
  • @DeepakKumar from application logs i can see below: The following profiles are active: dev – JingJong Aug 20 '20 at 04:16
  • @DeepakKumar how does application yml play a role here as i am trying to create a bean which doesnt take anything from properties file? – JingJong Aug 20 '20 at 04:19
  • @JingJong I suspected that your profile configuration is not current and that is causing the issue while creating a bean – Deepak Kumar Aug 20 '20 at 04:20
  • @DeepakKumar okay got it ..yeah but here i am not even using any properties – JingJong Aug 20 '20 at 04:21

3 Answers3

1

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

rene
  • 41,474
  • 78
  • 114
  • 152
Vlad Ulshin
  • 482
  • 2
  • 5
0

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

Soban Imam
  • 131
  • 1
  • 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.

JingJong
  • 207
  • 1
  • 3
  • 14