Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 618
membersound Avatar asked Mar 20 '23 18:03

membersound


1 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

like image 142
M. Deinum Avatar answered Mar 31 '23 22:03

M. Deinum