Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
860 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 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...