0
votes

For my Spring Boot App, I am using Profile. Following is my Bean Class:

@Profile("container")
@Bean
public class ConfigService {
public RestTemplate restTemplate() {
    return new RestTemplate();
}

} Now, I want to use this bean in another class and do so via with auto wired:

@Autowired private ConfigSevice config;

This work as long as the profile "container" active is, but the application throws an unsatisfied dependeny error referring the autowired bean when the profile is not active in the class where the bean is used. How can I solve this problem?

1

1 Answers

1
votes

Use

@Autowired(required = false)

But be careful. If you call a method on this dependency when profile is not active, you will get a nullpointer exception.