mem*_*und 18 java spring dependency-injection
我可以以某种方式在抽象类上对一组注释进行分组,并且扩展此类的每个类都自动分配了这些注释吗?
至少以下不起作用:
@Service
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
class AbstractService
class PersonService extends AbstractService {
@Autowired //will not work due to missing qualifier annotation
private PersonDao dao;
}
Run Code Online (Sandbox Code Playgroud)
Phi*_*der 25
答案是不
除非注释类型具有@Inherited元注释,否则不会继承Java注释:https://docs.oracle.com/javase/7/docs/api/java/lang/annotation/Inherited.html.
Spring的@Component注释没有@Inherited,因此您需要在每个组件类上添加注释.@ Service,@ Controller和@Repository都没有.
Erw*_*idt 11
简短的回答是:使用您在示例中提到的注释,没有.
答案很长:有一个名为meta的注释java.lang.annotation.Inherited.如果注释本身使用此批注进行批注,那么当使用它对类进行批注时,其子类也会通过暗示自动对其进行批注.
但是,正如您在spring源代码中看到的那样,@Service和@Scope注释本身并没有注释@Inherited,因此类的存在@Service和@Scope它的子类不会继承.
也许这是可以在Spring中修复的东西.
| 归档时间: |
|
| 查看次数: |
10093 次 |
| 最近记录: |