Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining beans per multiple spring profiles

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.

like image 236
Luke Avatar asked Aug 11 '15 12:08

Luke


People also ask

How do you define beans for a specific profile?

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.

How do you inject multiple beans of the same type?

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.

How many ways can bean be defined in Spring?

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.

Can I configure a bean class multiple times?

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

How to assign a bean with multiple profiles in spring?

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.

What are profiles in Spring Framework?

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.

How to use @profile annotation in Spring Boot?

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

How do I set the default profile of a bean?

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


2 Answers

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.

like image 94
Jens Avatar answered Sep 30 '22 15:09

Jens


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>
like image 25
Łukasz Kotyński Avatar answered Sep 30 '22 15:09

Łukasz Kotyński