Victor1125
Victor1125

Reputation: 692

Create beans depends on spring profile

I have strange problem. Create beans depends on spring profile. Create annotation and use it on bean class. Anntoations:

@Profile("dev")
public @interface Dev {
}

@Profile("prod")
public @interface Prod {
}

using:

public interface Facade {
    String test();
}

@Dev
@Component("facade")
public class DevFacade implements Facade {
    public String test() {
        return "Dev";
    }
}

@Prod
@Component("facade")
public class ProdFacade implements Facade{
    public String test() {
        return "Prod";
    }
}

Until spring boot version 2.1.18.RELEASE it works fine. When I set spring.profiles.active=dev I will get only bean DevFacade. But above version 2.2.0.RELEASE It doesn't work and I get Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'facade' for bean class [com.example.demo.test.ProdFacade] conflicts with existing, non-compatible bean definition of same name and class [com.example.demo.test.DevFacade]. This means Spring create both beans for dev and prod profile, but why? When I modify classes and use @Profile("dev") instead of @Dev it works.

Have someone any idea why?

Upvotes: 1

Views: 1168

Answers (1)

Akif Hadziabdic
Akif Hadziabdic

Reputation: 2890

User Retention annotation. @Retention(RetentionPolicy.RUNTIME)

@Retention(RetentionPolicy.RUNTIME)
@Profile("dev")
  public @interface Dev {
}

@Retention(RetentionPolicy.RUNTIME)
@Profile("prod")
  public @interface Prod {
}

Upvotes: 1

Related Questions