Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring can you autowire inside an abstract class?

Spring is failing to autowire my object? Is it possible to autowire an object within an abstract class. Assume all schemas are supplied in application-context.xml

Question: What annotation should be on the base and extending classes (if any) @Service @Component?

Example

abstract class SuperMan {

    @Autowire
    private DatabaseService databaseService;

    abstract void Fly();

    protected void doSuperPowerAction(Thing thing) {

        //busy code

        databaseService.save(thing);

    }
}

Extending class

public class SuperGirl extends SuperMan {

    @Override
    public void Fly() {
        //busy code
    }

    public doSomethingSuperGirlDoes() {

        //busy code

        doSuperPowerAction(thing)

    }

application-context.xml

<context:component-scan base-package="com.baseLocation" />
<context:annotation-config/>
like image 492
stackoverflow Avatar asked Nov 13 '13 22:11

stackoverflow


People also ask

Can we use @autowired in a class?

A class that uses the repository class: public class MyClass extends AbstractClass { @Autowired private MyRepository myRepository; //... } I know that if I annotate my MyClass with @Component and use it with an @Autowired , then the @Autowired MyRepository is resolved just fine.

Which classes can be Autowired in Spring?

In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file.

Can abstract class have bean?

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

Can we Autowire inside a method?

@Autowired on Setter MethodsYou can use @Autowired annotation on setter methods to get rid of the <property> element in XML configuration file. When Spring finds an @Autowired annotation used with setter methods, it tries to perform byType autowiring on the method.


Video Answer


3 Answers

I have that kind of spring setup working

an abstract class with an autowired field

public abstract class AbstractJobRoute extends RouteBuilder {

    @Autowired
    private GlobalSettingsService settingsService;

and several children defined with @Component annotation.

like image 90
Frederic Close Avatar answered Oct 13 '22 03:10

Frederic Close


Normally, Spring should do the autowiring, as long as your abstract class is in the base-package provided for component scan.

See this and this for further reference.

@Service and @Component are both stereotypes that creates beans of the annotated type inside the Spring container. As Spring Docs state,

This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.

like image 36
Andrei Nicusan Avatar answered Oct 13 '22 02:10

Andrei Nicusan


What if you need any database operation in SuperGirl you would inject it again into SuperGirl.

I think the main idea is using the same object reference in different classes. So what about this:

//There is no annotation about Spring in the abstract part.
abstract class SuperMan {


    private final DatabaseService databaseService;

    public SuperMan(DatabaseService databaseService) {
     this.databaseService = databaseService;
    }

    abstract void Fly();

    protected void doSuperPowerAction(Thing thing) {

        //busy code

        databaseService.save(thing);

    }
}

@Component
public class SuperGirl extends SuperMan {

private final DatabaseService databaseService;

@Autowired
public SuperGirl (DatabaseService databaseService) {
     super(databaseService);
     this.databaseService = databaseService;
    }

@Override
public void Fly() {
    //busy code
}

public doSomethingSuperGirlDoes() {

    //busy code

    doSuperPowerAction(thing)

}

In my opinion, inject once run everywhere :)

like image 6
Muhammed Ozdogan Avatar answered Oct 13 '22 02:10

Muhammed Ozdogan