The "ApplicationContextAware" class accesses other Beans or container resources without dependency injection. However, in practice, it has been found that this class is often misused in interviews; Beans that should be obtained through injection are instead frequently accessed via a so-called utility class. This convenient access method provided by the class should be restricted as early as possible.

[Edited, screenshot deleted.]

Comment From: remeio

In my opinion, this usage is normal, but not elegant enough. Dependency Inject is better than Dependency Lookup.

// Dependency Inject 
@Resource
private XXXService service

// Dependency Lookup
SpringContextHolder.getBean(XXXService.class);

// NOT support
@Resource
private static XXXService service;

You can send you issue to Stack overflow to dicuss.

BTW, are you upload your company's code to Github? It's so dangerous.

Comment From: sbrannen

As @remeio pointed out, such questions are usually best asked on Stack Overflow.

However, I'll provide short answers here.

The "ApplicationContextAware" class accesses other Beans or container resources without dependency injection. However, in practice, it has been found that this class is often misused in interviews; Beans that should be obtained through injection are instead frequently accessed via a so-called utility class. This convenient access method provided by the class should be restricted as early as possible.

ApplicationContextAware has existed since Spring Framework was introduced. So, no, it cannot be deprecated or removed.

It serves a special purpose, typically only used within framework code.

Could you please tell me if these usages are all normal?

As you alluded to, dependency injection (for example, via @Autowired) is the preferable way to obtain a dependencies from the ApplicationContext.

Note that BeanFactoryAware is a similar Aware interface that exists for special purposes, also typically used within framework code.

If users have a concrete need to look up beans via means other than dependency injection, they can make use of ObjectProvider.

If you have further questions about these topics, please post them on Stack Overflow.

Cheers

Comment From: youngledo

BTW, are you upload your company's code to Github? It's so dangerous.

@remeio The screenshot was taken to facilitate explaining the issue with this kind of "junk code" and was not intended to offend; it has been edited and deleted.