前几天写web项目的时候,用到了spring mvc。

但是又写bean。我要在代码里面生成,而这个bean里面,又有一些属性是通过spring注入的。

所以,只能通过ApplicationContext来获取。

在servlet里面获取ApplicationContext其实可以通过spring提供的方法:

1
WebApplicationContextUtils.getWebApplicationContext(ServletContext)

来获取。

这个方法前提是要在web.xml里面即一个listener:

1
2
3
4
5
6
7
8
9
10
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-servlet.xml,
            classpath:applicationContext.xml
        </param-value>
    </context-param>
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

不过这样子只能在servlet里面获取。

如果要在servlet外获取ApplicationContext呢?

其实可以封装一下的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package me.idashu.code.util;
 
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
 
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
 
/**
 * 描述:ApplicationContext容器
 *
 * @author: dashu
 * @since: 13-5-11
 */
public class AppContext implements ServletContextListener {
 
    private static WebApplicationContext springContext;
 
    public AppContext() {
        super();
    }
 
    public void contextInitialized(ServletContextEvent event) {
        springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
 
    }
 
 
    public void contextDestroyed(ServletContextEvent event) {
    }
 
    public static ApplicationContext getApplicationContext() {
        return springContext;
    }
 
}

还需要在web.xml里面再加条记录:

1
2
3
<listener>
        <listener-class>me.idashu.code.util.AppContext</listener-class>
    </listener>

其实就是在content初始化的时候,把ApplicationContext保存起来,下次方便调用。

spring中获取applicationContext(2)的更多相关文章

  1. spring中获取applicationContext

    常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...

  2. spring中获取ApplicationContext对象的技巧,含源码说明

    第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...

  3. spring mvc在Controller中获取ApplicationContext

    spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...

  4. spring中获取dao或对象中方法的实例化对象

    spring中获取dao的中方法的实例化对象: //获取应用上下文对象 ApplicationContext ctx = new ClassPathXmlApplicationContext(&quo ...

  5. Spring中获取被代理的对象

    目录 Spring中获取被代理的对象 获取Spring被代理对象什么时候可能会用到? Spring中获取被代理的对象 Spring中获取被代理的对象 ### 获取Spring被代理对象的JAVA工具类 ...

  6. spring中获取当前项目的真实路径

    总结: 方法1: WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext ...

  7. spring 代码中获取ApplicationContext(@AutoWired,ApplicationListener)

    2017年度全网原创IT博主评选活动投票:http://www.itbang.me/goVote/234    学习spring框架时间不长,一点一滴都得亲力亲为.今天忽然觉得老是通过@Autowir ...

  8. Spring中获取对象

    Spring是一个非常主流,而且是好用的框架.提供管理对象的容器,提供事务的支持,缓存,权限认证(往往不用).用来集成hibernate等.而管理对象的生命周期是其中一个非常重要的功能.在Spring ...

  9. Spring +quartz获取ApplicationContext上下文

    job存在数据库中,能够进行动态的增增删改查,近期遇到了怎样获取ApplicationContext上下文的问题.解决的方法例如以下 applicationContext-quartz.xml < ...

随机推荐

  1. uva 579 ClockHands 几何初接触 求时针与分针的夹角

    貌似是第一次接触几何题... 求时针与分针的夹角,这不是小学生的奥数题么.我小时候也想过这问题的. 每过一小时时针走1/12*360=30度,每过一分钟时针走1/60*30=0.5度,分针走1/60* ...

  2. django drf 动态权限配置和动态seriaizer_class配置

    可以通过重写get_permissions方法和get_serializers_class方法来实现动态的配置权限和序列化 VIewDemo class RegUserSet(mixins.Creat ...

  3. 给IDistributedCache新增了扩展方法GetOrCreate、GetOrCreateAsync

    public static class DistributedCacheExtensions { public static TItem GetOrCreate<TItem>(this I ...

  4. 使用Git上传代码到远程仓库

    1.进入文件夹,cd f:/test/ 2.初始化远程仓库,git init 3.添加代码,git add . (注意add后面的点前面有一个空格) 4.提交代码到远程仓库,git commit -m ...

  5. JSOI2010 满汉全席

    题目链接:戳我 一个2-SAT的模板题. (什么是2-SAT呢?就是解决一个情况两种决策的问题,我们根据"选了其中一个点A就必须选一个点B的原则,从A向B连边.最后判断如果在一个强连通分量里 ...

  6. jzoj5879. 【NOIP2018提高组模拟9.22】电路图 B

    tj:一道好題 看區間操作可以想到線段樹 並聯操作公式:a1∗a2/(a1+a2)a1*a2/(a1+a2)a1∗a2/(a1+a2) 串聯操作公式:a1+a2a1+a2a1+a2 我們發現,一個區間 ...

  7. Flask从入门到精通之flask程序入门

    初始化 所有Flask程序都必须创建一个程序实例,Web服务器使用一种名为Web服务器网关接口的的协议(WSGI),把接收自客户端的所有请求转发给这个对象处理.程序实例是Flask类的对象,使用下面代 ...

  8. vector.clear()不能用来清零

    vector.clear()函数并不会把所有元素清零,笔者就曾经这样幻想过这个函数的作用,然而事实证明并不是. vector有两个参数,一个是size,表示当前vector容器内存储的元素个数,一个是 ...

  9. 无apk,怎么获取app的activity

    在做app自动化测试之前,有个前提条件,就是要获取当前app的包名和当前活动的activity.如果有提供了.apk,就可以直接通过adb命令获取到包名和欢迎页面:有种软件是手机自带的的,我们不知道a ...

  10. word转html 压缩图片网站

    word转html https://docs.google.com/document/d/1MS-os1NcEPSEe2OWRenGR_6CsEmEQUchoQoh-abmL1Y/edit 压缩图片 ...