我可以使用@Autowire注释继承的最终属性吗?
解决方案:不,我不再扩展原始父级。
原文:
有没有办法注释继承的 final
setter 方法? 我正在扩展一个具有 final
setter 的类,我希望使用 Spring 来 @Autowire
。 父类来自库,无法修改。
我发现的一个解决方法是编写一个代理方法,但这似乎比必要的工作更多。
public abstract class SqlMapClientDaoSupport ... {
public final void setSqlMapClient(SqlMapClient smc) {
...
}
}
@Component
public class AccountDao extends SqlMapClientDaoSupport {
// all this just to annotate an existing method?
@Autowire
public final void setSqlMapClientWorkaround(SqlMapClient smc) {
super.setSqlMapClient(smc);
}
}
编辑 1:修改以上示例以反映用例:
用例是为 Ibatis/Spring 实现 DAO 对象 它扩展了公共 基类。 每个 DAO 都需要相同的 属性集,并且每个属性都需要配置为一个bean。 我现在 通过在 applicationContext.xml
中单独配置每个内容来实现此目的。
<bean id="accountDAO"
class="com.example.proj.dao.h2.AccountDAOImpl"
p:sqlMapClient-ref="sqlMapClient" />
<bean id="companyDAO"
class="com.example.proj.dao.h2.CompanyDAOImpl"
p:sqlMapClient-ref="sqlMapClient" />
<!-- etc... -->
我更喜欢使用组件扫描来发现并自动连接 DAO 自动对象,我无法复制/粘贴错误。
<context:component-scan base-package="com.example.proj.dao.h2" />
我在注释指南中没有看到如何注释 除声明外的财产/成员。 我希望那是 但我缺少一些东西。
编辑 2: 我不再扩展 SqlMapClientDaoSupport 类,而是我的 AccountDao 是一个 POJO,它实现了 Support 类提供的少量功能。 这允许我随意使用@Autowire。
Resolution: No I'm no longer extending the original parent.
Original:
Is there a way to annotate an inherited final
setter method? I am extending a class which has a final
setter which I would like to @Autowire
with Spring. The parent class is from a library and cannot be modified.
A workaround I have found is to write a proxy method, but this seems like more work than necessary.
public abstract class SqlMapClientDaoSupport ... {
public final void setSqlMapClient(SqlMapClient smc) {
...
}
}
@Component
public class AccountDao extends SqlMapClientDaoSupport {
// all this just to annotate an existing method?
@Autowire
public final void setSqlMapClientWorkaround(SqlMapClient smc) {
super.setSqlMapClient(smc);
}
}
Edit 1: Above example modified to reflect use case:
The use case is implementing DAO objects for Ibatis/Spring
which extend a common base class. Each DAO needs the same
property set, and each needs to be configured as a bean. I currently
do this by configuring each individually in applicationContext.xml
.
<bean id="accountDAO"
class="com.example.proj.dao.h2.AccountDAOImpl"
p:sqlMapClient-ref="sqlMapClient" />
<bean id="companyDAO"
class="com.example.proj.dao.h2.CompanyDAOImpl"
p:sqlMapClient-ref="sqlMapClient" />
<!-- etc... -->
I would prefer to use component-scan to discover and autowire the DAO
objects automatically, which I can't copy/paste botch.
<context:component-scan base-package="com.example.proj.dao.h2" />
I do not see in the annotation guide how one would annotate a
property/member other than where declared. I'm hoping that is
something I'm missing though.
Edit 2: I am no longer extending the SqlMapClientDaoSupport class, instead my AccountDao is a POJO which implements what little functionality was being provided by the Support class. This allows me to use @Autowire at will.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你试过用xml配置吗? 因为它是一个现有的类,看起来您无法更改,所以它是使用 xml 配置它的明确候选者。 然后你可以将其指定为“autowire”,甚至可以在xml中配置该属性。
Have you tried configuring it with xml? Because it's an existing class which it looks like you can't change, it's a definite candidate for configuring it with xml. Then you can specify it as autowire", or even configure the property in the xml.
在我看来,您不应该尝试设置最终字段。
通常有一个很好的理由说明字段是最终的。
您是否设置了 SqlMapClientFactoryBean 对象?
请参阅此处获取帮助
It sounds to me like you shouldn't be trying to set a final field.
There is usually a good reason why fields are final.
Have you setup a SqlMapClientFactoryBean object ?
See here for help
不,没有办法注释继承的最终方法。
我没有扩展支持类 (
SqlMapClientDaoSupport
),而是在我的项目中重新实现它(它的行为很小),根据需要注释方法,并且我的 DAO 扩展了该支持类。No, there is no way to annotate an inherited final method.
Rather than extend the support class (
SqlMapClientDaoSupport
) I reimplemented it in my project (it's behavior is minimal) annotating the methods as needed, and my DAO extend that support class.您可以创建一个新的构造函数,其中包含所有最终 setter 的参数和构造函数的 @Autowired,然后在构造函数中调用 setter。
You could create a new constructor with params for all the setters that are final and
@Autowired
the constructor, then call the setters in the constructor.