【发布时间】:2013-11-13 22:32:34
【问题描述】:
Spring 无法自动装配我的对象?是否可以在抽象类中自动装配对象。假设所有模式都在 application-context.xml 中提供
问题:基类和扩展类(如果有)@Service @Component 上应该有什么注释?
示例
abstract class SuperMan {
@Autowire
private DatabaseService databaseService;
abstract void Fly();
protected void doSuperPowerAction(Thing thing) {
//busy code
databaseService.save(thing);
}
}
扩展类
public class SuperGirl extends SuperMan {
@Override
public void Fly() {
//busy code
}
public doSomethingSuperGirlDoes() {
//busy code
doSuperPowerAction(thing)
}
application-context.xml
<context:component-scan base-package="com.baseLocation" />
<context:annotation-config/>
【问题讨论】:
-
虽然帖子好像很老了,只是想指出自动装配有一个错字,应该是@Autowired而不是抽象类超人。
标签: spring abstract-class autowired