开发者

Spring, abstract class and annotations

开发者 https://www.devze.com 2023-01-01 20:20 出处:网络
I\'ve got a pretty simple abstract class public abstract class AbstractServiceAction开发者_StackOverflows {

I've got a pretty simple abstract class

public abstract class AbstractServiceAction开发者_StackOverflows {

    @Autowired
    protected DatabaseModel dbModel;

    protected User user;
    protected boolean complete;
    protected String serviceResult;

    public AbstractServiceActions(User user) {
        this.user = user;
        this.serviceResult = "";
    }

    public abstract String doAction();
    }

Now you can see, I'm trying to autowire the DatabaseModel. But in my extended class I only recieve null for the dbModel.

@Component
public class CreateDatabaseAction extends AbstractServiceActions {
....
}

Question: Am I trying something impossible here?


Your setup seems fine. The reason perhaps lies elsewhere. Maybe you are instantiating the class with new CreateDatabaseAction(), rather than letting spring do this.


Use @Autowired and not @Inject from javax.inject.

Dependency injection in abstract class only works for spring's @Autowired.

FYI, I'm using Spring 4.0; Java 6

0

精彩评论

暂无评论...
验证码 换一张
取 消