我想使用@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);
}
但它看起来很难看
有没有其他方式哟注入财产没有定义?
最佳答案
如何引入中间超类?
原文链接:https://www.f2er.com/spring/432428.htmlpublic class AutowiringsqlMapClientDaoSupport extends sqlMapClientDaoSupport {
@Autowired
injectsqlMapClient(sqlMapClient sqlMapClient) {
setsqlMapClient(sqlMapClient);
}
}
然后
@Repository("userDao")
public class UserDaoMybatis extends AutoringsqlMapClientDaoSupport implements UserDao {
// ...
}
是的,这是滥用继承,但并不比现有的sqlMapClientDaoSupport更糟糕,如果你急于避免DAO类本身的注入钩子,我想不出更好的方法.