【问题标题】:How to create bean using @Bean in spring boot for abstract class如何在Spring Boot中使用@Bean为抽象类创建bean
【发布时间】:2021-12-04 06:00:07
【问题描述】:

我需要将旧式 spring 项目迁移到 Spring boot。 假设下面的代码 sn -p 我必须迁移到 Spring 引导样式。

我问,如何将下面的抽象 bean 转换为 @Bean ?

<bean id="sample" class="com.test.core.common.AbstractClass" abstract="true">
    <property name="sample1" ref="sample1" />
     <property name="sample2" ref="sample2" />
</bean>

【问题讨论】:

标签: java spring spring-boot


【解决方案1】:

用纯 Java 编写你的抽象基类(没有任何 Spring 耦合):

public abstract class AbstractClass{   
    private Sample1 sample1;
    private Sample2 sample2;

    public AbstractClass(Sample1 sample1, Sample1 sample2){
       this.sample1 = sample1;
       this.sample2 = sample2;
   }
   ... 
}

请注意,添加带参数的构造函数(抽象类和具体类)使注入更容易,依赖关系更清晰。

那么你有两种方法:

1) 使用 @Component 注释具体类。
如:

@Component
public class MyClass extends AbstractClass{   
    public MyClass (Sample1 sample1, Sample1 sample2){
        super(sample1, sample2);
    }
}

第一种方法的优点是简短:只需添加一个注释。
但它实际上使子类成为可能由 Spring 上下文加载的 bean。

2) 或者,在 Configuration 类中声明 bean。
如:

@Configuration
public class MyConfig{
  @Bean
   public MyClass myClass(Sample1 sample1, Sample1 sample2){
      return new MyClass(sample1, sample1);
   }
}

第二种方式比较冗长,但优点是不修改子类代码,也让类的客户来决定类是否应该是一个bean。

每种方法都有其优点和缺点。
所以要根据具体要求使用。

【讨论】:

【解决方案2】:

无需转换此代码。您只需要将扩展​​ com.test.core.common.AbstractClass 的类声明为 Spring 托管 bean,方法是使用 @Component@Service 注释它们,或者在配置类中声明使用 @Bean 注释的方法。

一般Java配置中不需要“抽象bean”,甚至没有等价物。它在 xml 配置中需要用于参数继承,现在可以使用普通的 java 方法来实现。查找 example from Stephane Nicoll Spring Core 开发者。

【讨论】:

    【解决方案3】:

    由于Java有自己的抽象类和继承机制,所以你不需要在spring耦合中做以下代码的耦合。

    <bean id="sample" class="com.test.core.common.AbstractClass" abstract="true">
        <property name="sample1" ref="sample1" />
         <property name="sample2" ref="sample2" />
    </bean>
    

    在 XML 配置中,您需要这样做来指定子 bean 继承的模板。但是由于Springboot使用Java配置,所以这部分直接用Java继承处理。

    意思是你可以把这个抽象类声明为一个普通的Java抽象类,只把子类当作bean,而不用担心抽象的父类。

    【讨论】:

      【解决方案4】:

      当我们想要abstract classinterface 实例化为@Bean),我们通常可以:

      1. 实例化一个anonymous inner class

      2. 重写所有抽象方法! ;(

      所以对于Method injection,它看起来像:

      @Configuration
      class FreakyConfig {
        @Bean
        @RequestScope // !! (stateful)
        public MyFooBarDelegate delegate() {
          return MyFooBarDelegate.of(...);
        }
        @Bean // singleton, stateless, abstract (no spring deps.)! ;)
        public MyFooBarAbstractSingletonBean() {
          return new MyFooBarAbstractSingletonBean() { // anonymous inner class!
            @Override
            protected MyFooBarDelegate fooBarDelegate() { // ...and override the (injected)/prescribed methods.
              return delegate();
            }
          };
        }
      } // (stress) tested ;)
      

      另一个好(?)问题:最新(“当前”)"spring-javaconfig/docs"???

      另见:


      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-24
        • 2019-09-08
        • 1970-01-01
        • 2016-12-15
        • 1970-01-01
        相关资源
        最近更新 更多