java 如何在spring boot中使用@Bean为抽象类创建bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48440669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create bean using @Bean in spring boot for abstract class
提问by begin
I have requirement to migrate old style spring project to Spring boot. Assume below code snippet I have to migrate to Spring boot style.
我需要将旧式 spring 项目迁移到 Spring boot。假设下面的代码片段我必须迁移到 Spring 引导样式。
Here my ask , how to convert below abstract bean to @Bean ?
在这里我问,如何将下面的抽象 bean 转换为 @Bean ?
<bean id="sample" class="com.test.core.common.AbstractClass" abstract="true">
<property name="sample1" ref="sample1" />
<property name="sample2" ref="sample2" />
</bean>
回答by davidxxx
Write your abstract base class in plain Java (without any Spring coupling) :
用纯 Java 编写抽象基类(没有任何 Spring 耦合):
public abstract class AbstractClass{
private Sample1 sample1;
private Sample2 sample2;
public AbstractClass(Sample1 sample1, Sample1 sample2){
this.sample1 = sample1;
this.sample2 = sample2;
}
...
}
Note that adding a constructor with parameters (both for the abstract class and the concrete class) makes injection easier and dependencies clearer.
请注意,添加带参数的构造函数(抽象类和具体类)使注入更容易,依赖关系更清晰。
Then you have two ways :
那么你有两种方法:
1) Annotate the concrete class(es) with @Component
.
Such as :
1) 用 注释具体的类@Component
。
如 :
@Component
public class MyClass extends AbstractClass{
public MyClass (Sample1 sample1, Sample1 sample2){
super(sample1, sample2);
}
}
This first way has the advantage to be short : just an annotation to add.
But it makes de facto the subclass as a bean that may potentially be loaded by the Spring context.
第一种方式的优点是简短:只需添加一个注释。
但它实际上使子类成为可能由 Spring 上下文加载的 bean。
2) Alternatively, declare the bean in a Configuration
class.
Such as :
2) 或者,在Configuration
类中声明 bean 。
如 :
@Configuration
public class MyConfig{
@Bean
public MyClass myClass(Sample1 sample1, Sample1 sample2){
return new MyClass(sample1, sample1);
}
}
This second way is more verbose but has the advantage to not modify the subclass code and also let clients of the class to decide whether the class should be a bean.
第二种方式更冗长,但优点是不修改子类代码,也让类的客户决定类是否应该是 bean。
Each approach has its advantages and its drawbacks.
So to use according to the concrete requirement.
每种方法都有其优点和缺点。
所以要根据具体要求使用。
回答by Sasha Shpota
There is no need in converting this code. You only need to make the classes that extend com.test.core.common.AbstractClass
declared as spring managed beans by either annotating them with @Component
or @Service
or declaring a method annotated with @Bean
in your configuration class.
无需转换此代码。您只需要通过在配置类中com.test.core.common.AbstractClass
使用@Component
或@Service
或声明一个带有注释的方法来将扩展的类声明为 spring 管理的 bean @Bean
。
Generally "abstract bean" is not needed in Java Configuration, there is even no equivalent. It was needed in xml configuration for parameter inheritance which is now achievable with plain java methods. Find example from Stephane Nicollwho is Spring Core developer.
Java Configuration 中通常不需要“抽象bean”,甚至没有等价物。它在 xml 配置中需要用于参数继承,现在可以使用普通的 java 方法实现。查找Spring Core 开发人员Stephane Nicoll 的示例。
回答by tryingToLearn
Since Java has it's own mechanism of abstract classes and inheritance in place, you don't need to do the coupling of following code in your spring coupling.
由于Java 有自己的抽象类和继承机制,所以你不需要在你的spring 耦合中做以下代码的耦合。
<bean id="sample" class="com.test.core.common.AbstractClass" abstract="true">
<property name="sample1" ref="sample1" />
<property name="sample2" ref="sample2" />
</bean>
In XML config, you needed to do this to specify the template for inheritance of child beans. But since Springboot uses Java configuration, this part is handled directly with Java inheritance.
在 XML config 中,您需要这样做来指定子 bean 继承的模板。但是由于Springboot使用的是Java配置,所以这部分直接用Java继承来处理。
What it means is that you can declare this abstract class as a normal Java abstract class and treat only the child classes as beans without worrying about the abstract parent class.
这意味着您可以将这个抽象类声明为一个普通的 Java 抽象类,并且只将子类视为 bean,而不必担心抽象父类。