Spring获取当前的ApplicationContext [英] Spring get current ApplicationContext

查看:139
本文介绍了Spring获取当前的ApplicationContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring MVC作为我的Web应用程序。我的bean用 spring-servlet.xml 文件编写

I am using Spring MVC for my web application. My beans are written in "spring-servlet.xml" file

现在我有一个类 MyClass 我想使用spring bean访问这个类

Now I have a class MyClass and i want to access this class using spring bean

spring-servlet.xml中我写了以下内容

<bean id="myClass" class="com.lynas.MyClass" />

现在我需要使用 ApplicationContext 访问它

ApplicationContext context = ??

这样我可以做到

MyClass myClass = (MyClass) context.getBean("myClass");

怎么做?

推荐答案

只需注入它..

@Autowired
private ApplicationContext appContext;

或实现此界面:ApplicationContextAware

这篇关于Spring获取当前的ApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆