简体   繁体   English

Spring @profile如何与继承一起使用?

[英]How does Spring @profile work with inheritance?

Currently in a project, I have parent abstract classes called page object classes which are subclassed typically by 2 or 3 child classes, each being used based on a specific configuration (For example: platform as Android, IOS, Web). 当前在一个项目中,我有一个称为页面对象类的父抽象类,通常由2或3个子类子类化,每个子类都基于特定的配置使用(例如:Android平台,IOS,Web平台)。

@Component
public abstract class MePage {}


@Profile("android")
@Component
public class AndroidMePage extends MePage {}


@Profile("ios")
@Component
public class IOSMePage extends MePage {}

Whenever an instance of one of the subclasses is needed, it's retrieved using 每当需要其中一个子类的实例时,都可以使用进行检索

@Autowired
MePage mePage;

Question
How does Spring work in such cases where subclasses are annotated with @profile and the parent class is an abstract class and also a component? 在子类使用@profile进行注释并且父类既是抽象类又是组件的情况下,Spring如何工作?

Does Spring automatically assign instance of one of the subclasses to the @Autowired abstract class variable based on the profile configured? Spring是否根据配置的配置文件自动将子类之一的实例分配给@Autowired抽象类变量?

You should not have @Component on top of an abstract class, since abstract classes are not meant to be instantiated (not event by Spring). 您不应在抽象类之上使用@Component ,因为抽象类无意被实例化(Spring不会发生事件)。

On top of that, Spring will inject the right bean based on your profile. 最重要的是,Spring将根据您的个人资料注入正确的bean。

@Autowired
private MePage mePage; // AndroidMePage if android profile is active

@Autowired
private MePage mePage; // IOSMePage if ios profile is active

If your parent class is not abstract, you have to deal with multiple bean definitions as usual. 如果您的父类不是抽象类,则必须照常处理多个bean定义。

I think you have at least three options here. 我认为您至少有三个选择。

1) Declare one of the beans as @Primary 1)将其中一个bean声明为@Primary

@Component
public class MePage {}

@Profile("android")
@Component
@Primary
public class AndroidMePage extends MePage {}

@Profile("ios")
@Component
@Primary
public class IOSMePage extends MePage {}

@Autowired
private MePage mePage; // AndroidMePage if android profile is active

@Autowired
private MePage mePage; // IOSMePage if ios profile is active

2) Autowire a List of beans 2)自动连线一个bean List

@Component
public class MePage {}

@Profile("android")
@Component
public class AndroidMePage extends MePage {}

@Profile("ios")
@Component
public class IOSMePage extends MePage {}

@Autowired
private List<MePage> pages; // MePage and one of AndroidMePage or IOSMePage , based on active profile

3) Add @Qualifier to your bean definitions and use that when autowiring 3)将@Qualifier添加到您的bean定义中,并在自动装配时使用

@Component
@Qualifier("default")
public class MePage {}

@Profile("android")
@Component
@Qualifier("android")
public class AndroidMePage extends MePage {}

@Profile("ios")
@Component
@Qualifier("ios")
public class IOSMePage extends MePage {}

@Autowired
@Qualifier("default")
private MePage mePage; // MePage is injected, regardless of active profile

@Autowired
@Qualifier("ios")
private MePage mePage; // IOSMePage if ios profile is active

@Autowired
@Qualifier("android")
private MePage mePage; // AndroidMePage if android profile is active

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM