simpletagsupport 怎么获取 application怎么读

怎样获取webapplicationcontext实例_百度知道
怎样获取webapplicationcontext实例
提问者采纳
方法四:类似上面方法:这种方式适合于采用Spring框架的B&#47。Spring初始化时,使系统尽可能的独立于当前运行环境.getBean(beanId);说明,因为这些类是知道它们是运行在Spring框架上的,会通过该方法将ApplicationContext 对象注入: 抽象类ApplicationObjectSupport提供getApplicationContext()方法.getBean(beanId),调用getWebApplicationContext()获取WebApplicationContext 方法五。这里值得提一点的是.context:继承自抽象类WebApplicationObjectSupport说明,可以方便的获取到ApplicationContext: import org。上面两个工具方式的区别是.getWebApplicationContext(ServletContext sc) ac1,然后在通过它获取需要的类实例.springframework,系统中用到上述方法的类实际上就于Spring框架紧密耦合在一起了,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入,后者返回null。Spring初始化时; ApplicationContext ac1 = WebApplicationContextUtils.web,并保存ApplicationContext 对象,请根据具体情况选用相应的方法:通过Spring提供的工具类获取ApplicationContext对象代码; ac2,通过ServletContext对象获取ApplicationContext对象.WebApplicationContextUtils,尽量通过DI的方式获取需要的服务提供者,系统中:实现接口ApplicationContextAware说明这种方式适用于采用Spring框架的独立应用程序,前者在获取失败时抛出异常;S系统。以上方法适合不同的情况:继承自抽象类ApplicationObjectSupport说明。方法二,因此,应该尽量的减少这类应用.getRequiredWebApplicationContext(ServletContext sc) ApplicationContext ac2 = WebApplicationContextUtils.support:实现该接口的setApplicationContext(ApplicationContext context)方法。方法三,需要程序通过配置文件手工初始化Spring的情况
网络工程师
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId"); 说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。
方法二:通过Spring提供的工具类获取ApplicationContext对象 代码:
import org.springframework.web.context.support.WebApplicationContextU
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId"); 说明: 这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。
其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 对象: WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
方法三:继承自抽象类ApplicationObjectSupport 说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。 Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
方法四:继承自抽象类WebApplicationObjectSupport 说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext
方法五:实现接口ApplicationContextAware 说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。 Spring初始化时,会通过该方法将ApplicationContext对象注入。在web应用中一般用ContextLoaderListener加载webapplication,如果需要在action之外或者control类之外获取webapplication思路之一是,单独写个类放在static变量中, 类似于:
public class AppContext {
private static AppC
private Abstract
ApplicationContext appC
public synchronized
static AppContext getInstance() {
if (instance ==null) {
instance =new AppContext(); }
private AppContext() {
this.appContext =new ClassPathXmlApplicationContext( "/applicationContext.xml");
public Abstract
ApplicationContext getAppContext() {
return appC
不过这样,还是加载了2次applicationcontext,servlet一次,路径加载一次;觉得不如直接用路径加载,舍掉servlet加载 在网上也找了些其他说法:实现ApplicationContextAware,,, 接口,或者servletcontextAware接口,还要写配置文件。有的竟然要把配置文件里的listener,换成自己的类,这样纯粹多此一举。不过有的应用不是替换,是在补一个listener, 我在一版的jpetstore(具体那一版不知道)里发现了这个: [web.xml]里 & &&&
&&& &listener& &&&&&&&
&&&&&&&&&&& &listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class& &&&
&&& &/listener& &&& &&&
&&& &listener& &&&&&&&
&&&&&&&&&& &listener-class&com.ibatis.jpetstore.util.SpringInit&/listener-class& &&&
&&& &/listener&
其中SpringInit实现接口ServletContextListener :
package com.ibatis.jpetstore.
import javax.servlet.ServletContextE
import javax.servlet.ServletContextL
import org.springframework.context.ApplicationC
import org.springframework.web.context.WebApplicationC
import org.springframework.web.context.support.WebApplicationContextU
public class SpringInit implements ServletContextListener {
private static WebApplicationContext springC
public SpringInit() {
public void contextInitialized(ServletContextEvent event) {
springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
public void contextDestroyed(ServletContextEvent event) {
public static ApplicationContext getApplicationContext() {
return springC
在其中的一个bean的构造里SpringInit获取applicationcontext,代码:
public OrderBean() {
(AccountService) SpringInit.getApplicationContext().getBean("accountService"),
(OrderService) SpringInit.getApplicationContext().getBean("orderService") );
恩,这种在action,servlet之外的bean里获取applicationcontext的方法值得参考,应该有用
阅读(...) 评论()ApplicationContext&获取的三种方法
spring为ApplicationContext提供的3种实现分别
为:ClassPathXmlApplicationContext,FileSystemXmlApplicationContext和
XmlWebApplicationContext,其中XmlWebApplicationContext是专为Web工程定制的。使用举例如下:
FileSystemXmlApplicationContext
&&&&&&&&&&&&&
eg1. ApplicationContext ctx = new
FileSystemXmlApplicationContext("bean.xml"); //加载单个配置文件
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&
String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
&&&&&&&&&&&&&&&&&&&&&
ApplicationContext ctx = new
FileSystemXmlApplicationContext(locations ); //加载单个配置文件
&&&&&&&&&&&&&
&&&&&&&&&&
ApplicationContext ctx =new
FileSystemXmlApplicationContext("D:/project/bean.xml");//根据具体路径加载文件
ClassPathXmlApplicationContext
&&&&&&&&&&&&&
eg1.&&ApplicationContext
ctx = new ClassPathXmlApplicationContext("bean.xml");
&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&
String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
&&&&&&&&&&&&&&&&&&&&&
ApplicationContext ctx = new
ClassPathXmlApplication(locations);
&&&&&&&&&&&&&
注:其中FileSystemXmlApplicationContext和ClassPathXmlApplicationContext与BeanFactory的xml文件定位方式一样是基于路径的。
3. XmlWebApplicationContext
eg1. ServletContext servletContext =
request.getSession().getServletContext();&&&&
&&&&&&&&&&&&&&
ApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
//web下得到路径一般如下:
 上文中说过,当前用户目录,即相对于System.getProperty("user.dir")返回的路径。
  对于JavaEE服务器,这可能是服务器的某个路径,这个并没有统一的规范!
  而不是我们发布的Web应用程序的根目录!
  这样,在Web应用程序中,我们绝对不能使用相对于当前用户目录的相对路径。
  在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。
  这样,我们只需要提供相对于Web应用程序根目录的路径,就可以构建出定位资源的绝对路径。
  这是我们开发Web应用程序时一般所采取的策略。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Spring中三种获取ApplicationContext的方法
package com.lc.
import org.springframework.context.ApplicationC
import org.springframework.context.support.FileSystemXmlApplicationC
public class App1 {
public static void main(String[] args) {
* 1.从ApplicationContext中取bean
//ApplicationContext ac=new ClassPathXmlApplicationContext("com/lc/ioc/beans.xml");
//当我们去实例化beans.xml,该文件中配置的bean被实例(该bean scope是 singleton)从bean中取出student
* 2.通过文件路径来获取ApplicationContext(用的并不多)
ApplicationContext ac=new FileSystemXmlApplicationContext("src\\com\\lc\\ioc\\beans.xml");
* 3.从XmlBeanFactory获得
//如果我们使用beanfactory去获取bean,当你只是实例化该容器, 那么容器的bean不被实例化,只有当你去使用getBean某个bean时,才会实时的创建.
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/lc/ioc/beans.xml"));
factory.getBean("student");
* 4.判断是不是单例的
//获取两个student
Student s1=(Student) ac.getBean("student");
Student s2=(Student) ac.getBean("student");
System.out.println(s1+" "+s2);
您对本文章有什么意见或着疑问吗?请到您的关注和建议是我们前行的参考和动力&&
您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。“/Library/Application Support/com.imangi.templerun在哪里 我的怎么没有我的是苹果4s怎么找不到
内容为广告/垃圾,我要举报!
特聘专家具有协助内容审核的特权
举报后内容将不能在前台展示
错乱举报会导致该权利被剥夺
选择举报原因&
快速简答--编辑问题发送微博@ZOL问答堂
新买的港版5s,怎么查询是否翻新机?型号:MF354ZP/A,序列号:DX3MXE78FRC6
没找到想要的答案?那就登录提问吧

我要回帖

更多关于 application怎么打开 的文章

 

随机推荐