As in the title above, I am confused about pros cons between injecting applicationContext by directly @Autowired annnotation or implementing ApplicationContextAware interfac
There is no need to use ApplicationContext at all.
If you need to use prototype scoped beans in a singleton bean, inject an org.springframework.beans.factory.ObjectFactory.
For example using constructor injection:
@Service
class MyClass {
private ObjectFactory myDependencyFactory;
public MyClass(ObjectFactory prototypeFactory) {
myDependencyFactory = prototypeFactory;
}
}
Now what's the benefit over using ApplicationContext ?
You can substitute this dependency (e.g. in a test) by simply passing a lambda (since ObjectFactory is a @FunctionalInterface) that returns a stubbed version of it.
While it is possible to stub the ApplicationContext, it is not clear in that case which beans will be looked up and need to be stubbed.