项目中ApplicationContext
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的更多相关文章
- 项目中运行报错: Loading XML bean definitions from class path resource [applicationContext.xml]
记录一下: org.springframework.context.support.AbstractApplicationContext prepareRefresh Refreshing org.s ...
- Spring Scope:Web项目中如何安全使用有状态的Bean对象?
Web系统是最常见的Java应用系统之一,现在流行的Web项目多使用ssm或ssh框架,使用spring进行bean的管理,这为我们编写web项目带来了很多方便,通常,我们的controler层使用注 ...
- Atitit.mybatis的测试 以及spring与mybatis在本项目中的集成配置说明
Atitit.mybatis的测试 以及spring与mybatis在本项目中的集成配置说明 1.1. Mybatis invoke1 1.2. Spring的数据源配置2 1.3. Mybatis ...
- web项目中加入struts2、spring的支持,并整合两者
Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...
- web项目中 集合Spring&使用junit4测试Spring
web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...
- 06_在web项目中集成Spring
在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...
- 控制反转和spring在项目中可以带来的好处
Spring实例化Bean的三种方式分别是: 1,xml配置使用bean的类构造器 <bean id="personService" class="cn.servi ...
- Axis2在Web项目中整合Spring
一.说明: 上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢? 二.Axis2 ...
随机推荐
- 通过LOGBACK实现每个类、包或自定义级别
项实现LOGBACK对每个包或者类或者通过自定义级别的方式实现自定义输出的日志进入制定的文件.查阅了很多资料,都没有找到行之有效的解决方案,直到看到了这篇文章http://www.360doc.com ...
- java中的vo、dto 、dao
VO是跟数据库里表的映射,一个表对应一个VO DAO是用VO来访问真实的表,对数据库的操作都在DAO中完成 BO是业务层,做逻辑处理的 VO , PO , BO , QO, DAO ,POJO ...
- 实现简易版的moment.js
github源码地址: www.baidu.com 作者: 易怜白 项目中使用了时间日期的处理方法,只使用了部分方法,为了不在引入第三方的库(moment.js),这里自己封装了项目中使用到的方法. ...
- Service 中添加同步块防止并发 重复
Service 中添加同步块防止并发 重复. synchronized(this){}
- [leetcode-312-Burst Balloons]
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- Python对数据库的增删改查
#!/usr/bin/env python import MySQLdb DATABASE_NAME = 'hero' class HeroDB: # init class and ...
- 第一个SpringMVC实例和解析(HelloSpringMVC)
1. 开发步骤: (1)增加Spring支持 下载Spring安装包和其依赖的commons-logging.jar,复制到项目Web应用的lib文件夹(WebRoot/WEB-INF/lib): S ...
- LinkedList原理及源码解析
简介 LinkedList是一个双向线性链表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer).由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度, ...
- IOS开发基础环境搭建
一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包: ...
- 暂停和播放CSS3动画的两种实现方法
1,直接修改animationPlayState <!DOCTYPE html> <html> <head lang="en"> <met ...