I have 3 profiles in my app called: dev, test, prod. I want to use spring profiles to customize bean injection so that for profile dev and test I will have one bean implementation and for profile prod I will have another. The question is how to achieve that. How I can setup one bean to be active within two different profiles. I tried something like this:
@Component
@Profile("dev, test")
class DevTestBean{}
but unfortunatelly spring sees it as a single profile called dev comma space test.
Use @Profile on a Bean Let's start simple and look at how we can make a bean belong to a particular profile. We use the @Profile annotation — we are mapping the bean to that particular profile; the annotation simply takes the names of one (or multiple) profiles.
The default autowiring is by type, not by name, so when there is more than one bean of the same type, you have to use the @Qualifier annotation.
There are three different ways in which you can define a Spring bean: annotating your class with the stereotype @Component annotation (or its derivatives) writing a bean factory method annotated with the @Bean annotation in a custom Java configuration class. declaring a bean definition in an XML configuration file.
The class is created as a bean in the "WebAppConfig extends WebMvcConfigurerAdapter" class, which is where the constructor is called. I did some testing and found out that the WebMvcConfigurerAdapter is loaded two or more times(By adding a println to its constructor).
It is possible to assign a bean with multiple Profiles, separated with logical operations like not (!), and ( &) and or ( |) etc. Profiles in Spring are specified with the @Profile annotation in Spring. It is possible to use and activate @Profile annotations in several ways, we will explore each of them in detail.
In this tutorial, we'll focus on introducing Profiles in Spring. Profiles are a core feature of the framework — allowing us to map our beans to different profiles — for example, dev, test, and prod. We can then activate different profiles in different environments to bootstrap only the beans we need.
Using @Profile annotation we can indicate the active profile in which a specific bean should be created. To test this let’s further enhance SpringBootTutorialBasicsConfigurationApplication. Let’s print the name of all the beans that are loaded. When you reload the application, you would see the following in the log
The Default Profile Any bean that does not specify a profile belongs to the default profile. Spring also provides a way to set the default profile when no other profile is active — by using the spring.profiles.default property. 6. Get Active Profiles
You have to change to @Profile({"dev", "test"})
The value must be declared as Set. See the documentation
If a @Configuration class is marked with @Profile, all of the @Bean methods and @Import annotations associated with that class will be bypassed unless one or more of the specified profiles are active. This is analogous to the behavior in Spring XML: if the profile attribute of the beans element is supplied e.g., , the beans element will not be parsed unless at least profile 'p1' or 'p2' has been activated. Likewise, if a @Component or @Configuration class is marked with @Profile({"p1", "p2"}), that class will not be registered or processed unless at least profile 'p1' or 'p2' has been activated.
XML solution has not been placed in official documentation:
https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/#beans-definition-profiles
so for the record I will put it here:
<beans profile="dev,foo,bar">
<!-- (...) -->
</beans>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With