Define default implementation for injection in Spring?

I have a base class that is extended by some other classes. Therefore I have to provide qualifiers for being able to inject a specific instance.

I wonder if I could mark any of these classes (eg the most upper class) as default class, which would be picked up if no qualifier is provided on @Autowired?

@Service
//@Qualifier("Parent")
class ParentRunner;

@Service
@Qualifier("Child")
class ChildRunner extends ParentRunner;

The following does at least not work:

@Autowired
//@Qualifier("Parent")
private ParentRunner runner;

Answers


You can mark one implementation with @Primary which will then be used as default. When using xml you can use the primary element inside the bean element to set an instance to the primary bean instance to use.

See http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Primary.html


Need Your Help

generating log timestamps using Stored procedures in SQL server 2005

sql-server-2005 performance stored-procedures logging timestamp

I have a Stored Procedure, Which has some select Statements and insert statements.

Should SVG contents be visible outside an embedded <svg> by default?

html css svg w3c html5boilerplate

In the past I answered a question about "who was wrong" in regards to SVG images not being "cropped" (overflow: hidden) in IE9, where all other supporting browsers did so. The answer is here.