在超类中注入没有 @Autowired 的 @Repository bean 的派生属性

发布于 2024-09-25 00:13:24 字数 926 浏览 2 评论 0原文

我想使用@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2024-10-02 00:13:24

引入一个中间超类怎么样?

public class AutowiringSqlMapClientDaoSupport extends SqlMapClientDaoSupport {

   @Autowired
   injectSqlMapClient(SqlMapClient sqlMapClient) {
      setSqlMapClient(sqlMapClient);
   }
}

然后

@Repository("userDao")
public class UserDaoMybatis extends AutoringSqlMapClientDaoSupport implements UserDao {
    // ...
}

是的,这是滥用继承,但并不比现有的 SqlMapClientDaoSupport 更糟糕,如果你迫切希望避免 DAO 类本身的注入钩子,我想不出更好的方法。

How about introducing an intermediary superclass?

public class AutowiringSqlMapClientDaoSupport extends SqlMapClientDaoSupport {

   @Autowired
   injectSqlMapClient(SqlMapClient sqlMapClient) {
      setSqlMapClient(sqlMapClient);
   }
}

and then

@Repository("userDao")
public class UserDaoMybatis extends AutoringSqlMapClientDaoSupport implements UserDao {
    // ...
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文