Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

java - Spring autowire dependency defined in an abstract class

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 {
...
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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>

cheers!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...