Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring@profile如何与继承一起工作?_Java_Spring_Inheritance_Dependency Injection - Fatal编程技术网

Java Spring@profile如何与继承一起工作?

Java Spring@profile如何与继承一起工作?,java,spring,inheritance,dependency-injection,Java,Spring,Inheritance,Dependency Injection,目前在一个项目中,我有一个称为页面对象类的父抽象类,通常由2或3个子类进行子类化,每个子类根据特定的配置使用(例如:Android、IOS、Web等平台) 每当需要其中一个子类的实例时,就会使用 @Autowired MePage mePage; 问题 在子类用@profile注释并且父类既是抽象类又是组件的情况下,Spring是如何工作的 Spring是否会根据配置的概要文件自动将其中一个子类的实例分配给@Autowired抽象类变量?您不应该将@Component放在抽象类的顶部,因为抽象

目前在一个项目中,我有一个称为页面对象类的父抽象类,通常由2或3个子类进行子类化,每个子类根据特定的配置使用(例如:Android、IOS、Web等平台)

每当需要其中一个子类的实例时,就会使用

@Autowired
MePage mePage;
问题
在子类用
@profile
注释并且父类既是抽象类又是组件的情况下,Spring是如何工作的


Spring是否会根据配置的概要文件自动将其中一个子类的实例分配给
@Autowired
抽象类变量?

您不应该将
@Component
放在抽象类的顶部,因为抽象类不是要实例化的(不是Spring的事件)

除此之外,Spring将根据您的配置文件注入正确的bean

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

@Autowired
private MePage mePage; // IOSMePage if ios profile is active
如果父类不是抽象类,则必须像往常一样处理多个bean定义

我想你至少有三个选择

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)自动连接豆的
列表

@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

我需要将父类作为抽象类,因为有些抽象方法需要在子类中定义。父类本身定义的一些非抽象方法。@AbhijeetVaikar您可以让父类抽象,我建议删除它顶部的
@Component
,让类本身抽象。因此,如果我从父类中删除
@Component
注释,Spring仍然会根据配置文件自动将子类的实例分配给
@Autowired
父类引用?如果在自动连接时,给定引用类型有一个且只有一个bean(即Spring托管组件),Spring会选择正确的bean并为您注入它。
@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
@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