Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create bean using @Bean in spring boot for abstract class

I have requirement to migrate old style spring project to Spring boot. Assume below code snippet I have to migrate to Spring boot style.

Here my ask , how to convert below abstract bean to @Bean ?

<bean id="sample" class="com.test.core.common.AbstractClass" abstract="true">
    <property name="sample1" ref="sample1" />
     <property name="sample2" ref="sample2" />
</bean>
like image 330
spandey Avatar asked Jan 25 '18 10:01

spandey


People also ask

Can we create bean of abstract class in Spring boot?

You don't. You only declare the beans which have a concrete subclass of that abstract class.

Can we use @bean on class?

We can also declare beans using the @Bean annotation in a configuration class. Finally, we can mark the class with one of the annotations from the org.

Can we Autowire bean in abstract class?

We can't use @Autowired on a constructor of an abstract class. Spring doesn't evaluate the @Autowired annotation on a constructor of an abstract class. The subclass should provide the necessary arguments to the super constructor.

What does @bean do in Spring boot?

One of the most important annotations in spring is the @Bean annotation which is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. This annotation is also a part of the spring core framework.


2 Answers

Write your abstract base class in plain Java (without any Spring coupling) :

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 :

@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.

2) Alternatively, declare the bean in a Configuration class.
Such as :

@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.

Each approach has its advantages and its drawbacks.
So to use according to the concrete requirement.

like image 103
davidxxx Avatar answered Oct 17 '22 23:10

davidxxx


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.

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 Nicoll who is Spring Core developer.

like image 3
Sasha Shpota Avatar answered Oct 17 '22 23:10

Sasha Shpota