Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the context utility class ApplicationContextUtil based on Spring?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the context tool class ApplicationContextUtil based on Spring? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Spring context tool class ApplicationContextUtilimport org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component; / * spring context tool class * @ author DAWN * / @ Componentpublic class ApplicationContextUtil implements ApplicationContextAware {/ * context object instance * / private static ApplicationContext applicationContext; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {ApplicationContextUtil.applicationContext = applicationContext } / * get applicationContext * * @ return * / public static ApplicationContext getApplicationContext () {return applicationContext;} / * get Bean through name. * * @ param name * @ return * / public static Object getBean (String name) {return getApplicationContext () .getBean (name);} / * obtain Bean through class. * * @ param clazz * @ param * @ return * / public static T getBean (Class clazz) {return getApplicationContext () .getBean (clazz) } / * through name, and Clazz returns the specified Bean * * @ param name * @ param clazz * @ param * @ return * / public static T getBean (String name, Class clazz) {return getApplicationContext (). GetBean (name, clazz);}} get the utility class of ApplicationContext

In a project, you often encounter such problems:

Some classes need to use new to create objects, but the class needs to use the bean defined in the spring container, so it is impossible to inject the bean we need through automatic injection of spring.

So you need to manually get the bean from the spring container. To get bean, you must first get the ApplicationContext object, which can be obtained in the following ways.

Mode one

Create the ApplicationContext object manually and save it.

Public class ApplicationContextUtil {private static ApplicationContext context; static {context = new ClassPathXmlApplicationContext ("applicationContext.xml");} public static ApplicationContext getApplicationContext () {return context;}} mode II

In the web environment, it is obtained through the utility class provided by spring, and the ServletContext object is required as a parameter. The bean is then obtained through the ApplicationContext object.

The difference between the following two tools is that the former returns null when the fetch fails, while the latter throws an exception.

In addition, because the spring is the object of the container in the ServletContext, you can fetch the WebApplicationContext object directly from the ServletContext.

ApplicationContext context1 = WebApplicationContextUtils.getWebApplicationContext (ServletContext sc); ApplicationContext context2 = WebApplicationContextUtils.getRequiredWebApplicationContext (ServletContext sc); WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute (WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)

The utility class inherits the abstract class ApplicationObjectSupport and uses @ Component on the utility class to be managed by spring.

This way, when the spring container starts, it sets the ApplicationContext object through the setApplicationContext () method in the parent class ApplicationObjectSupport.

You can get the ApplicationContext object through getApplicationContext ().

Mode 4

The utility class inherits the abstract class WebApplicationObjectSupport, and looking at the source code shows that WebApplicationObjectSupport inherits ApplicationObjectSupport, so the way to get the ApplicationContext object is the same as above, using the getApplicationContext () method.

Mode 5

The utility class implements the ApplicationContextAware interface, overrides the setApplicationContext (ApplicationContext applicationContext) method, and uses the @ Component annotation in the utility class to let spring manage it.

When the spring container starts, it calls the setApplicationContext () method to set the ApplicationContext object into it.

@ Componentpublic class ApplicationContextUtil implements ApplicationContextAware {private static ApplicationContext context; public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {context = applicationContext;} public static ApplicationContext getApplicationContext () {return context }} the answer to the question about the Spring-based context tool class ApplicationContextUtil is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report