java 使用 Spring 注释注入父类依赖项的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16733509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper way to inject parent class dependencies with Spring annotations
提问by Kshitiz Sharma
I have following code -
我有以下代码 -
Dao.java
道.java
@Component
public class Dao extends NamedParameterJdbcDaoSupport {
}
dbContext.xml
数据库上下文.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.jdbc.url}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.password}" />
</bean>
applicationContext.xml
应用上下文.xml
<context:component-scan base-package="com.kshitiz" />
The problem is that NamedParameterJdbcDaoSupport
needs data source in order to work.
Since this is a property of the super class and not my own class the only way I could think of to make it work is -
问题是NamedParameterJdbcDaoSupport
需要数据源才能工作。由于这是超类的属性而不是我自己的类,因此我能想到的使其工作的唯一方法是-
@Component
public class Dao extends NamedParameterJdbcDaoSupport {
@Autowired
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
}
This is pretty ugly. Can I specify that I want to autowire all the properties of my bean? Something like -
这很丑陋。我可以指定我要自动装配 bean 的所有属性吗?就像是 -
@Component(default-autowire="byType")
public class Dao extends NamedParameterJdbcDaoSupport {
}
Is this possible in Spring? Alternatively what is the most elegant way to inject super class dependencies?
这在春天可能吗?或者,注入超类依赖项的最优雅的方法是什么?
Edit:I already know this can be done using XML which I am presently using. I'd like to know the best that can be done using annotations only.
编辑:我已经知道这可以使用我目前使用的 XML 来完成。我想知道仅使用注释可以完成的最佳工作。
回答by Zutty
Not necessarily the answer you were looking for, but I would do this with an intermediary super class.
不一定是你要找的答案,但我会用一个中间超级类来做到这一点。
public abstract class AbstractDao extends NamedParameterJdbcDaoSupport {
@Autowired
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
}
@Component
public class Dao extends AbstractDao {
}
回答by BrokenMill
I've searched for something similar when using Spring's Hibernate support. There's no way of adding (or changing) the wiring in a superclass without subclassing and overriding the required method. Or the declarative approach of subclassing and providing a refvalue for the desired properties via XML.
Anything less "ugly" would probably be less transparent. So Zutty's proposed solution fits best here as it eliminates the need of overriding in each Dao implementation.
我在使用 Spring 的 Hibernate 支持时搜索过类似的东西。没有子类化和覆盖所需的方法,就无法在超类中添加(或更改)接线。或者通过 XML 对所需属性进行子类化和提供ref值的声明性方法。
任何不那么“丑陋”的东西可能都不那么透明。所以 Zutty 提出的解决方案最适合这里,因为它消除了在每个 Dao 实现中覆盖的需要。
回答by fpmoles
If it is required for your class to work (and it probably is in a DAO), it should be a constructor argument not a property. Since you are autowiring, you need neither. Make it protected in the parent and autowire it. Your child will have a reference to it.
如果你的类需要它工作(它可能在 DAO 中),它应该是一个构造函数参数而不是一个属性。由于您是自动装配,因此您都不需要。使其在父级中受到保护并自动装配。您的孩子将参考它。
回答by Tom McIntyre
This can be done transparently using xml configuration. If you want to use annotations, calling super like you are now is probably the best way to go.
这可以使用 xml 配置透明地完成。如果你想使用注解,像现在这样调用 super 可能是最好的方法。