applicationContext说白了就是对beanFactory的扩展,也就是一个spring容器,而且applicationContext是单例的,项目中主要包含一个webApplicationContext和spring的DispatchServlet的一个容器。

一.系统提供数据初始化即build的时候怎么把这些bean注入到spring容器中

   因为运行build,这不是一个web工程,不会解析web.xml,所以通过一个ApplicationContextFactory这个工厂类来启动一个spring容器:

  

public final class ApplicationContextFactory {
private static ApplicationContextFactory instance; private Map<String,ApplicationContext> contexts=new HashMap<String,ApplicationContext>(); private ApplicationContextFactory(){ } public static ApplicationContextFactory getInstance(){
if (instance==null){
instance=new ApplicationContextFactory();
}
return instance;
} public ApplicationContext getApplicationContext(){
String type=System.getProperty("spring.profiles.active");
ApplicationContext result=contexts.get(type);
if (result==null){
result=new ClassPathXmlApplicationContext(
"classpath*:/applicationContext.xml");
contexts.put(type, result);
}
return result;
} public Object getBean(String beanName){
return getApplicationContext().getBean(beanName);
}
}

  注入各个applicationContext.xml的bean,然后在build中执行这些相关bean的init方法。

@Component
@AppInitRunner
public class SystemBuild extends InitializationsRunner { @Autowired
private DatabaseInitManager databaseInitManager;
@Autowired
private SessionInitManager sessionInitManager;
@Autowired
private PropertiesInitManager propertiesInitManager;
@Autowired
private PermissionInitManager permissionInitManager;
@Autowired
private OrganizationInitManager organizationInitManager; @Autowired
private RoleInitManager roleInitManager;
@Autowired
private JobInitManager jobInitManager;
@Autowired
private ProcessInitManager processInitManager; @Autowired
private MemcachedClient memcachedClient; @Override
public void executeInitialization() throws Exception { memcachedClient.flushAll(); databaseInitManager.init();
sessionInitManager.init();
propertiesInitManager.init();
permissionInitManager.init();
organizationInitManager.init();
roleInitManager.init();
jobInitManager.init();
processInitManager.init(); memcachedClient.shutdown();
System.exit(0);
} public static void building(String[] args) {
try {
Map<String, Object> map = ApplicationContextFactory.getInstance()
.getApplicationContext()
.getBeansWithAnnotation(AppInitRunner.class);
for (Entry<String, Object> entry : map.entrySet()) {
((InitializationsRunner) entry.getValue())
.executeInitialization();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

  

二.web工程加载ApplicationContext

  1.在web.xml中通过listener启动容器,这个webApplicationContext即rootContext,这里面主要是管理各个applicationContext.xml中注入的bean主要是一些工具类,DAO等

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  2.spring 本身的DispatchServlet就有一个容器,这个主要用来管理spring-mvc.xml中的bean,还有包括系统中打上@component/controller/service等的bean(主要是service&&controller)。这个Applicationcontext的parentContext就是上面提到的rootContext ,当子applicationContext中找不到相关bean时,会自动去父applicationContext中去寻找。

 

项目中ApplicationContext的更多相关文章

  1. 项目中运行报错: Loading XML bean definitions from class path resource [applicationContext.xml]

    记录一下: org.springframework.context.support.AbstractApplicationContext prepareRefresh Refreshing org.s ...

  2. Spring Scope:Web项目中如何安全使用有状态的Bean对象?

    Web系统是最常见的Java应用系统之一,现在流行的Web项目多使用ssm或ssh框架,使用spring进行bean的管理,这为我们编写web项目带来了很多方便,通常,我们的controler层使用注 ...

  3. Atitit.mybatis的测试  以及spring与mybatis在本项目中的集成配置说明

    Atitit.mybatis的测试  以及spring与mybatis在本项目中的集成配置说明 1.1. Mybatis invoke1 1.2. Spring的数据源配置2 1.3. Mybatis ...

  4. web项目中加入struts2、spring的支持,并整合两者

    Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...

  5. Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

    本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...

  6. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  7. 06_在web项目中集成Spring

    在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...

  8. 控制反转和spring在项目中可以带来的好处

    Spring实例化Bean的三种方式分别是: 1,xml配置使用bean的类构造器 <bean id="personService" class="cn.servi ...

  9. Axis2在Web项目中整合Spring

    一.说明: 上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢? 二.Axis2 ...

随机推荐

  1. 透过源码看看Redis中如何计算QPS

    通常我们采集Redis的性能数据时,或者想要知道Redis当前的性能如何时,需要知道这个实例的QPS数据,那么这个QPS数据是如何计算的呢?我们都有哪些办法或者这个QPS ? QPS顾名思义就是每秒执 ...

  2. 用PHP和Ajax进行前后台数据交互——以用户登录为例

    很多网站中都有用户登录系统,要完成用户的注册和登陆,就一定要用到前后台的数据交互.在这里以简单的用户注册和登陆为例介绍一下前后台交互的大致流程. 首先,我们来做一个简单的登陆界面. 这里为了方便我使用 ...

  3. java入门学习笔记之1(类的定义,代码的编译执行)

    这篇文章讲解Java代码的基本执行过程 我们先抛开各种JAVA IDE,开发工具,只使用文本编辑器,以突出最本质的东西. 在Linux环境下,我们编辑一个文件: vim HelloWorld.java ...

  4. 关于MATLAB处理大数据坐标文件2017526

    运行六个特征,提高了3分,也就是说以前做的特征已经用完了,穷途末路,依靠以前的特征已经很难取得进步了,提出以下建议 1.测试集曾经运行错误的数据尽早画出图形,并尽可能发现问题并提出特征 2.运行其他程 ...

  5. Ubutnu16.04安装pytorch

    1.下载Anaconda3 首先需要去Anaconda官网下载最新版本Anaconda3(https://www.continuum.io/downloads),我下载是是带有python3.6的An ...

  6. 论文笔记 Generative Face Completion

    这篇paper将巧妙地将四个loss函数结合在一起,其中每一个loss的功能不同.但这篇paper不够elegant的地方也是loss太多!在本文中,我采用散文的写作方法谈谈自己对这篇paper的理解 ...

  7. 【ASP.NET MVC 牛刀小试】 URL Route

    例子引入 先看看如下例子,你能完全明白吗? using System; using System.Collections.Generic; using System.Linq; using Syste ...

  8. 【Android Developers Training】 73. 布局变化的动画

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. 【Android Developers Training】 63. 定义形状

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  10. TSC打印机使用教程终极版

    最近公司做一个资产采集的项目,之前做过此类项目,不过没有整理资料,借这次机会写一下,做个记录. 本教程使用的打印机型号:TSC TTP-244 Plus     官方文档 一.TSC打印机安装 1.机 ...