@Component with parent?

前端 未结 2 1073
长情又很酷
长情又很酷 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:16

    Following my comment, this piece of XML

    
        
    
    
    
    
    

    is more or less eqivalent to this code (note that I created artificial Base class since abstract beans in Spring don't need a class):

    public abstract class Base {
        @Autowired
        protected Foo foo;
    }
    
    @Component
    public class Wallace extends Base {}
    
    @Component
    public class Gromit extends Base {}
    

    Wallace and Gromit now have access to common Foo property. Also you can override it, e.g. in @PostConstruct.

    BTW I really liked parent feature in XML which allowed to keep beans DRY, but Java approach seems even cleaner.

提交回复
热议问题