在抽象类中定义的Spring autowire依赖项 [英] Spring autowire dependency defined in an abstract class

查看:540
本文介绍了在抽象类中定义的Spring autowire依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有@Autowired依赖项的抽象类Command和扩展抽象类的类。依赖性没有被注入。抽象和具体类使用@Component注释并正在扫描。似乎基础(抽象)类不是弹簧管理的。需要做些什么呢?是否有注释将其定义为抽象?我不想用XML定义bean。

I have an abstract class "Command" with an @Autowired dependency and classes extending the abstract class. The dependency is not being injected. The abstract and concrete classes are annotated with @Component and are being scanned. It seems that the base(abstract) class is not spring managed. What needs to be done for it to be? Is there an annotation to define it as abstract? I don't want to define the bean in XML.

public abstract class Command {
  @Autowired
  private SecurityUtils securityUtils;
....

@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Component
public class NoteCommand extends Command {
...
}

我的错误我道歉。命令类在我的控制器中注入,其中一个(NoteCommand)中有许多是通过new手动实例化的。一切都很好。

My mistake I apologize. The command classes are injected in my controllers and one of them (NoteCommand) out many was instantiated manually via "new". All is good.

推荐答案

这可以通过XML配置实现(不确定注释)。阅读 http://docs.spring .io / spring-framework / docs / 3.0.0.RC3 / reference / html / ch03s07.html

This can be achieved with XML configuration(not sure about annotations). Read this http://docs.spring.io/spring-framework/docs/3.0.0.RC3/reference/html/ch03s07.html

试试这个(将其他配置添加到子bean) ?)

Try this(add other config to child bean?)

<bean id = "command" class = "some.package.name.Command" abstract = "true">
  <property name = "securityUtils" ref = "securityUtils"/>
</bean>

<bean id ="noteCommand" class = "some.package.name.NoteCommand" parent="commadn">

</bean>

干杯!

这篇关于在抽象类中定义的Spring autowire依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆