@Component with parent?

前端 未结 2 1063
长情又很酷
长情又很酷 2021-02-14 03:04

Is there any way to use with @Component annotation (creating spring beans using annotation)?

I would like to create sprin

2条回答
  •  再見小時候
    2021-02-14 03:07

    Just came across the same Construct.. (a general abstract parent Class which used Beans - with context.xml-declaration for an potential Implementation that was injected by Component-scan)

    This is what I could have done in the @Component Class:

    @Autowired
    public void setBeanUsedInParent(BeanUsedInParent bean) {
       super.setBeanUsedInParent(bean);
    }
    

    ..what I ended up doing (better, since it makes clear that the injection works on the object not on classes) - and if you are able to modify the parent Class:

    // abstract getter in parent Class
    public abstract BeanUsedInParent getBeanUsedInParent();
    

    ..leave the actual Beans as well as their injection up to the actual implementation (the @Component Class):

    @Autowired
    private BeanUsedInParent beanUsedInParent;
    
    @Override
    public BeanUsedInParent getBeanUsedInParent() {
       return this.beanUsedInParent;
    }
    

提交回复
热议问题