alex_java_kotlin
alex_java_kotlin

Reputation: 439

How can i exclude spring bean from autowiring candidates for multiple profiles?

I want to exclude bean from autowiring candidates if current profile is NOT one of array, like this:

@Component
@ActiveProfiles(value = {"!profile_1", "!profile_2"})
public class MyComponentImpl implements MyComponent

@Component
public class MyComponentDummy implements MyComponent

But when i'm trying to do it via @ActiveProfiles annotation that bean doesn't seem to be excluded and i'm getting expected single matching bean but found 2.

Am i missing something or it is impossible that way and must be done with something like

@ActiveProfiles(value = {"profile_3", "!profile_4", ... }) 

My spring version is 4.2.2

Upvotes: 0

Views: 3620

Answers (1)

user2254601
user2254601

Reputation: 174

This could be similar to query here How to conditionally declare Bean when multiple profiles are not active?

On other hand I would recommend to do positive selection rather than negative elimination. The "default" profile could be of helpful here. Lets say you have multiple profiles - Profile1.. Profile5

And you do not want to create instance of bean if its Profile1 or Profile2

So you can annotate it with @ActiveProfiles(value={"Profile3","Profile4","Profile5","default")

Upvotes: 2

Related Questions