I have a @Service that's injected into a conditional bean. If the bean conditions is false, would the @Service be instantiated anyway?
Yes, if you mark it as @Service and spring IOC container finds it during the startup (because it's a subject to component scanning) then it will load it, create the bean definition out of it, initialize the object, inject it's own dependencies and put onto the application context. From this standpoint it doesn't matter whether the bean is injected into other beans or not.
Do I need to mark it @Lazy?
Usually lazy is used to "postpone" the initialization of bean till the point when its called first (read, its methods are invoked).
If you do not call this bean's method - you basically do not need to create.
@Michiel already provided one way to avoid loading this bean with annotation, another method is "unify" all the relevant beans in java configuration:
@Configuration
@ConditionalOnProperty(...)
public class MyConfig {
@Bean
public MyService myService() {
return new MyService();
}
@Bean
public MyDefaultComponent myDefaultComponent(MyService myService) {
return new MyDefaultComponent(myService);
}
}
This method allows specifying the conditional only once, so that if "tomorrow" you'll have even more beans to load - you'll know where to add them so that they will be loaded only upon certain value of your condition.
Update 1
Roughly the same effect can be achieved by using a custom stereotype annotation See this tutorial for example
You can create your own annotation, say, @FeatureXService and annotated it with both @ConditionalOnProperty and regular @Service
Then you'll have to mark all the relevant beans with this annotation instead of regular @Service or @Component. The annotation looks like this:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Service
@ConditionalOnProperty(name = "feature.x.enabled", havingValue = "true")
public @interface FeatureXService {
}
And the use it:
@FeatureXService
class MyService {...}
@FeatureXService
class MyDefaultComponent implements MyComponent {...}
This also allows specifying the @ConditionalOnProperty only once.