首页 > 解决方案 > Is it a good practice to make one Spring bean inherit another one?

问题描述

In my Spring Boot web application there are two beans, one of them inherits other one and both implements an interface. Something line this:

@Component 
public class A implements SomeInterface {
    // ....
} 

@Component 
public class B extends A implements SomeInterface {
    // ....
} 

Is it a good idea to make one bean extending another one or it's better use a decorator? And why?

I don't mean any specific case but general one.

标签: javaspringspring-bootdesign-patternsspring-bean

解决方案


No because it will fail if you inject it by interface or the super class. If you inject A now it is ambiguous.


推荐阅读