'No qualifying bean of type when using @ConditionalOnProperty

I have a component that I what to be able to enable/disable from the applikation.properties in my spring boot application..

In my application.properties I have

wiki.enabled=false

And in component looks like this

@Component
@ConditionalOnProperty("wiki.enabled")
public class WikiClient {
...
}

And finally in my other class where I use the wikiclient I have autowired it like this in my constructor.

    @Autowired(required = false)
    public MigrationManager(UserService userService, WikiClient wikiClient) {
    ...
    }

Still I get the exception

No qualifying bean of type com.test.WikiClient

If I enable the property it works like if I enabled the component.



Solution 1:[1]

I solved it by moving the the wikiClient argument to a property and used @Autowired(required = false) instead of having it in the constructor. Like M. Deinum said in the comments. Optional dependencies should not be in the constructor.

Solution 2:[2]

In your main springboot application class add Annotation @ComponentScan and make sure the package in which WikiClient is defined is included in the ComponentScan.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 heldt
Solution 2 Vaibhav Ranglani