Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowire a default implementation if implemenation bean is not found

Tags:

java

spring

I have an interface with 2 default methods. If the implementation is not found, I wish autowire a proxy bean with these 2 default methods. Is this possible in spring?

like image 921
Maclean Pinto Avatar asked Mar 26 '19 09:03

Maclean Pinto


People also ask

What is autowiring in Spring Boot beanfactory?

This can be done by declaring all the bean dependencies in Spring configuration file. So, Spring is able to utilize the BeanFactory to know the dependencies across all the used beans. . The default mode is . Spring supports the following autowiring modes: no: It’s the default autowiring mode. It means autowiring.

How to exclude a bean from autowiring in Java?

If you want to make specific bean autowiring non-mandatory for a specific bean property, use required=”false” attribute in @Autowired annotation. If you want to apply optional autowiring at global level i.e. for all properties in all beans; use below configuration setting. 5. Excluding a bean from autowiring

How to use @AutoWired annotation in Bean classes in spring?

Apart from the autowiring modes provided in the bean configuration file, autowiring can be specified in bean classes also using @Autowired annotation. To use @Autowired annotation in bean classes, you must first enable the annotation in the spring application using the below configuration.

How to apply optional autowiring for all properties in a bean?

If you want to apply optional autowiring at global level i.e. for all properties in all beans; use below configuration setting. 5. Excluding a bean from autowiring By default, autowiring scans, and matches all bean definitions in scope.


1 Answers

You need to define bean in some configuration

@Bean
@ConditionalOnMissingBean
public Foo foo(){
    return new Foo() {};
}

where Foo is your interface.

like image 92
talex Avatar answered Sep 18 '22 14:09

talex