Dark Star1
Dark Star1

Reputation: 7403

How to resolve bean dependencies conditionally in Spring using @Conditional annotation

I have a bean which also has a dependency on another bean, both of which are annotated with the same spring conditional:

@Component
@Conditional(ImportEnabledCondition.class)
public class ImportEntityResource {

@Autowired
ImportService importService;
....
}

The importService:

@Component
@Conditional(ImportEnabledCondition.class)
public class ImportService{...}

but I always get the org.springframework.beans.factory.NoSuchBeanDefinitionException exception. I'd appreciate any pointers as to how to resolve this issue.

Upvotes: 1

Views: 1228

Answers (1)

Cyril G.
Cyril G.

Reputation: 2017

@Conditional is used to register bean in Spring context depending of some conditions. In your case, you can just remove it as Spring will see the dependency as ImportEntityResource has ImportService as member. If the dependency is less explicit you can use @DependsOn.

Upvotes: 1

Related Questions