javascript
Spring框架–应用程序上下文–到达应用程序上下文的三种方法
本文向您展示了三種不同的方式來獲取代碼中的Spring Framework Application Context。
摘要
(這是我在2010年撰寫的舊文章的轉(zhuǎn)貼)。 在Google中搜索“ Spring ApplicationContextAware ”時(shí),您會(huì)遇到很多建議,而且我也看到很多人繼續(xù)抱怨說他們的setApplicationContext方法沒有被調(diào)用。 因此,為澄清起見,我在博客中寫了一些說明,希望它有助于澄清上下文的工作方式。
方法1
在您的類中,可以實(shí)現(xiàn)ApplicationContextAware類,如下所示:
public class MyClass implements ApplicationContextAware {static final long serialVersionUID = 02L;ApplicationContext applicationContext = null;public void doSomething(){if (applicationContext != null && applicationContext.containsBean("accessKeys")){MyBean beanA = (MyBean) applicationContext.getBean("mybean");//Do something with this AccessBean}return null;}@Overridepublic void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {System.out.println("setting context");this.applicationContext = applicationContext;}}方法2
如果您使用的是Java Servlet,則可以執(zhí)行以下操作:
public class gzservlet extends HttpServlet {static final long serialVersionUID = 02L;ApplicationContext applicationContext = null;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {if (applicationContext == null){System.out.println("setting context in get");applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());}if (applicationContext != null && applicationContext.containsBean("accessKeys")){AccessBean thisAccessBean = (AccessBean) applicationContext.getBean("accessKeys");req.setAttribute("keys", thisAccessBean.toString());System.out.println("setting keys");}req.getRequestDispatcher("/index2.jsp").include(req,resp);}}因此,人們會(huì)問的問題是什么時(shí)候使用什么? 答案是。 取決于您如何調(diào)用Spring。
方法1的工作原理:調(diào)用Spring時(shí),您正在使用DispatcherServlet將此鏈接。 然后,方法1將解析ApplicationContextAware的實(shí)現(xiàn),并調(diào)用setApplicationContext()方法來設(shè)置上下文。
在web.xml中:
<servlet><servlet-name>dispatchservlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup> </servlet><servlet-mapping><servlet-name>dispatchservlet</servlet-name><url-pattern>/*</url-pattern> </servlet-mapping>如果您沒有使用DispatcherServlet,并且正在使用偵聽器初始化Spring,并且您有自己的Servlet正在驅(qū)動(dòng)Request \ Response范圍,則請(qǐng)使用方法#2。 下面是這種情況下web.xml外觀的示例。
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener><servlet><servlet-name>MyOwnServlet</servlet-name><servlet-class>com.something.myservlet</servlet-class><load-on-startup>2</load-on-startup> </servlet><servlet-mapping><servlet-name>MyOwnServlet</servlet-name><url-pattern>*.do</url-pattern> </servlet-mapping>我希望這可以闡明為什么有時(shí)即使實(shí)現(xiàn)了ApplicationContextAware接口,也不會(huì)調(diào)用您的setter。
[09/12/2010]這是獲取上下文的第三種方法:
使用靜態(tài)方法創(chuàng)建以下類以獲取上下文:
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware;public class ApplicationContextProvider implements ApplicationContextAware{private static ApplicationContext ctx = null;public static ApplicationContext getApplicationContext() {return ctx;}public void setApplicationContext(ApplicationContext ctx) throws BeansException {this.ctx = ctx;} }并在您的spring bean配置xml文件中添加以下內(nèi)容:
<bean id="applicationContextProvider" class="ApplicationContextProvider"></bean>現(xiàn)在,在您的課程中,您可以執(zhí)行以下操作:
ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();而已!!!
干杯。
如果您覺得這篇文章有用,請(qǐng)考慮注冊(cè)我的電子郵件或?qū)⑵渲匦掳l(fā)布到您喜歡的社交網(wǎng)站上。 請(qǐng)參閱右側(cè)導(dǎo)航欄上的鏈接。
現(xiàn)在為今天的靈感
為了創(chuàng)新,我們不能期待別人做了什么。 開辟一條道路的整個(gè)想法是,以前沒有道路。 今天要?jiǎng)?chuàng)新!
翻譯自: https://www.javacodegeeks.com/2017/11/spring-framework-application-context-three-ways-get-application-context.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Spring框架–应用程序上下文–到达应用程序上下文的三种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 显示器有拖影重影解决办法电脑显示重影如何
- 下一篇: mongodb副本集_设置MongoDB