I know this is possible to exclude classes from a component-scan using exclude-filter .
The question is if it is possible to exclude the injection of a dependency in a class being injected.
For example, say I have the following class:
@Component
public class ServiceFactory
{
@Resource
private Service1 service1;
@Resource
private Service2 service2;
...
}
Now I would like to have ServiceFactory injected, service1 injected, but I would like to exclude service2 from the injection.
Of course I tried the following and it didn't work
<context:component-scan base-package="mypackage">
<context:exclude-filter type="assignable" expression="...Service2" />
</context:component-scan>
In your specific case (where you don't want to inject in unit test, but want to inject in prod, but also don't want to create a dummy bean) you can use the @MockBean annotation
I think this is a better solution than to just not inject the bean, becuase that way you avoid NullPointerException
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.