在超类中注入没有 @Autowired 的 @Repository bean 的派生属性
我想使用@Repository spring注释来避免在context.xml中添加bean。 我使用 ibatis 集成,所以我的存储库类看起来像这个
@Repository("userDao")
public class UserDaoMybatis extends SqlMapClientDaoSupport implements UserDao {
// ...
}
SqlMapClientDaoSupport (spring 库类)有用于设置所需属性的最终方法,该属性未使用 @Autowired 或 @Resourse
public final void setSqlMapClient(SqlMapClient sqlMapClient) {
if (!this.externalTemplate) {
this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
}
}
SqlMapClient bean 注释,在 spring context.xml 中定义。 如果 userDao bean 在 XML 中定义,它工作正常,但是当我放置 @Repository 注释并删除 bean 声明时,我得到以下异常
java.lang.IllegalArgumentException: Property 'sqlMapClient' is required
解决方法可以是添加新方法,
@Aitowired
injectSqlMapClient(SqlMapClient sqlMapClient) {
setSqlMapClient(sqlMapClient);
}
但它看起来很难看
有没有其他方法可以注入属性而无需已定义?
I would like to use @Repository spring annotation to avoid adding bean in context.xml.
I use ibatis integration, so my repository class looks like this
@Repository("userDao")
public class UserDaoMybatis extends SqlMapClientDaoSupport implements UserDao {
// ...
}
SqlMapClientDaoSupport (spring library class) has final method for setting required property which is not annotated with @Autowired or @Resourse
public final void setSqlMapClient(SqlMapClient sqlMapClient) {
if (!this.externalTemplate) {
this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
}
}
SqlMapClient bean is defined in spring context.xml.
If userDao bean is defined in XML it works fine, but when I put @Repository annotation and remove bean declaration I get the following exception
java.lang.IllegalArgumentException: Property 'sqlMapClient' is required
A workaround can be to add new method like
@Aitowired
injectSqlMapClient(SqlMapClient sqlMapClient) {
setSqlMapClient(sqlMapClient);
}
but it looks ugly
Is there any other way yo inject the property without having defined?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引入一个中间超类怎么样?
然后
是的,这是滥用继承,但并不比现有的
SqlMapClientDaoSupport
更糟糕,如果你迫切希望避免 DAO 类本身的注入钩子,我想不出更好的方法。How about introducing an intermediary superclass?
and then
Yes, it's abuse of inheritance, but no worse than the existing
SqlMapClientDaoSupport
, and if you're desperate to avoid the injection hook in the DAO class itself, I can't think of a better way.