[英]Spring @PostConstruct in @Repository dao and @Service that extends dao
My application is using Spring Boot (latest version) and has a dependency which uses Spring (latest version).我的应用程序正在使用 Spring Boot(最新版本)并且有一个使用 Spring(最新版本)的依赖项。 The dependency has a Dao
class annotated with @Repository
that extends a couple of unannotated abstract classes up to JdbcDaoSupport .依赖关系有一个Dao
与注解类@Repository
延伸一对夫妇未加抽象类的高达JdbcDaoSupport 。 Moreover, this class has a @PostConstruct
-annotated method, that sets the datasource.此外,这个类有一个@PostConstruct
-annotated 方法,用于设置数据源。
A secondary Service
class, annotated with @Service
, extends from Dao
.使用@Service
注释的辅助Service
类从Dao
扩展。
I'm finding that an autowired <Service instance>.getJdbcTemplate()
- which comes from the above-mentioned JdbcDaoSupport
superclass - gives null
.我发现自动装配的<Service instance>.getJdbcTemplate()
- 来自上述JdbcDaoSupport
超类 - 给出null
。
If I omit the @Repository
annotation on Dao
, the jdbcTtemplate is correctly set.如果我省略了@Repository
上注释Dao
,在jdbcTtemplate设置正确。 The same happens if I annotate that Dao
with @Service
.如果我用@Service
注释Dao
,也会发生同样的情况。
A part from the fact that I may have annotated them both for no specific purposes - the whole application code is experimental, though I can see a situation where the main service extends the repository (dao) while a more complex one can implement its logic delegating to several dao's - is this forbidden by Spring or am I missing something?部分原因是我可能没有出于特定目的对它们进行注释 - 整个应用程序代码是实验性的,尽管我可以看到主服务扩展存储库(dao)的情况,而更复杂的服务可以实现其逻辑委托到几个 dao 的 - 这是 Spring 禁止的还是我遗漏了什么?
I added a @PostConstruct
method to the service and it's not being called.我向服务添加了一个@PostConstruct
方法,但它没有被调用。
I tried autowiring the dao inside the service, but I get the same result;我尝试在服务内部自动装配 dao,但得到了相同的结果; it seems to me a matter of @Repository
and PersistenceExceptionTranslationPostProcessor
where the latter replaces my bean with a proxy-generated one ( GitHub issue ).在我看来, @Repository
和PersistenceExceptionTranslationPostProcessor
的问题是后者用代理生成的 bean 替换了我的 bean( GitHub 问题)。
您应该将 DAO 自动装配到服务中,而不是扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.