Die knock interview series.
【 Primary 】【Spring】
In our normal coding, if you use Spring, you’ll find a lot of classes that end in Aware. Are there any patterns? What is the interviewer trying to gain from this?
# 1. Q
What are the classes that end in Aware used for in Spring? Can you give an example?
# 2. Parsing
Questions are used to test the interviewer’s basic coding ability and summarizing ability. This topic is a primer, because ApplicationContextAware is used frequently and will be encountered when using Spring.
# 3. The answer
Aware means Aware, and classes that end in that word generally implement the Aware interface, so that beans can access the services of the Spring container. The specific callback methods are implemented by subclasses, such as ApplicationContextAware.
# 4. Extension
To understand this, we need only understand Spring’s Bean life cycle.
The Bean life cycle is divided into initialization and destruction. In each part, of course, can do some hooks, such as usual InitializingBean DisposableBean, etc. Of course, Aware is no exception.
Spring provides the interface to Aware:
1) BeanNameAware: You can get the name of the bean in the container
void setBeanName(String name);
Copy the code
2) BeanFactoryAware: Get the current bean Factory. This can also invoke the container’s services
void setBeanFactory(BeanFactory beanFactory)
throws BeansException;
Copy the code
ApplicationContextAware: The current applicationContext, which can also call the container’s services
void setApplicationContext(ApplicationContext applicationContext)
throws BeansException;
Copy the code
4) MessageSourceAware: Get message source, which can also get text information
void setMessageSource(MessageSource messageSource);
Copy the code
5) ResourceLoaderAware: Get the resource loader, you can get the content of external resource files
void setResourceLoader(ResourceLoader resourceLoader);
Copy the code
The list goes on. The most commonly used is ApplicationContextAware, and almost every project has one.
ApplicationContextAware can get the spring context and then the associated bean, which is also a way to get the injected bean, in some cases where autoWired is not available.
This disrupts Spring’s injection architecture and should be used sparingly.
Typical code is as follows:
@Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { applicationContext = ctx; } public static ApplicationContext getApplicationContext() { return applicationContext; } public static <T> T getBean(Class<T> cls) { return applicationContext.getBean(cls); }}Copy the code
Static field injection is a pain in the neck, like a lump in the throat.
With such a common utility class, why doesn’t Spring provide one by default? And developers are going to make a class like this dirty and smelly?
Spring does provide one by default, but it has to be a Web project, so use the following code to get the Context.
org.springframework.web.context.WebApplicationContext
.getCurrentWebApplicationContext();
Copy the code
Digress. In ordinary coding, we should pay attention to these naming rules.