dreamvc框架(一)ioc容器的集成
我的dreamvc框架最终写得差点儿相同了,借鉴了非常多开源框架,SpringMVC、Struts2等,眼下放在github上面。地址请猛戳我
写得差点儿相同了,是要写一个总结,把自己当时的思路记录下来!还有很多其它的工作要做!
(一)
首先,IOC容器作为管理bean的重要工具,我们在日常的开发其中经经常使用到,最经常使用的就属SPRINGIOC了吧!当然,假设开发人员不原理是用SPRINGIOC。那么你能够实现自己的容器,或者是用其它的3方ioc工具。仅仅要实现dreamvc提供的IocFactory或者继承AbstractIocFactory。请看以下这个接口
package org.majorxie.dreamvc.ioc.factory; import java.util.List; import javax.servlet.ServletConfig;
import javax.servlet.ServletContext; /**
*IOC 容器 工厂接口
*
* @author xiezhaodong(majorxie@139.com)
*2014-10-24
*/
public interface IocFactory {
/**
* 载入容器
* @param config
*/
void init(ServletContext context); /**
* destory ioc
*/
void destroy(); /**
* 得到全部的controller对象
* @return
*/
List<Object> getControllers()throws Exception; /**
* 是否是拦截器
* @return
*/
List<Object> getInterceptors(); /**
* 得到其它对象
* @return
*/
List<Object> getOthers(); }
package org.majorxie.dreamvc.ioc.factory; import java.util.List; /**
* 假设是Spring容器就让他自己destory,其它的能够继承该类覆盖此方法
* 假设想要扩展ioc,则能够选择使用extends还是implements
* @author xiezhaodong
*2014-10-25
*/
public abstract class AbstractIocFactory implements IocFactory {
/**
* 默认实现为空
*/
public void destroy() { } public List<Object> getOthers() { return null;
} }
开发人员依照接口定义内容。封装好自己的controller和Interceptor。然后在web.xml里面配置实现类的路径即可了。dreamvc已经默认的实现了springioc。请看实现类
package org.majorxie.dreamvc.ioc.factory; import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletContext; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.majorxie.dreamvc.exception.NotHaveControllerException;
import org.majorxie.dreamvc.interceptor.Interceptor;
import org.majorxie.dreamvc.tag.Controller;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; /**
* Spring容器的实现,我们让spring去destory所以继承的abstract
*
* @author xiezhaodong 2014-10-25 01:34
*
*/
public class SpringIocFactory extends AbstractIocFactory {
private final Log log = LogFactory.getLog(SpringIocFactory.class); private ApplicationContext applicationContext;
private List<Object> controllerBeans = new ArrayList<Object>();
private List<Object> otherBeans = new ArrayList<Object>();
private List<Object> interceptorBeans = new ArrayList<Object>(); public void init(ServletContext context) {
log.info("init context...");
applicationContext = WebApplicationContextUtils
.getRequiredWebApplicationContext(context);
initBeans();
} public List<Object> getControllers() throws NotHaveControllerException { if (controllerBeans.size() == 0) {
throw new NotHaveControllerException("you need at least one controller ");
} else {
return controllerBeans;
}
} public List<Object> getInterceptors() {
return interceptorBeans;
} /**
* 假设是Interceptor或者controller类。或者有着两个的注解都算是该类的类
* 遍历全部的bean装载到list
*
*/
@SuppressWarnings("unchecked")
private void initBeans() {
String[] beanNames = applicationContext.getBeanDefinitionNames();
for (String beanName : beanNames) {
if (applicationContext.getBean(beanName) instanceof Interceptor/*||applicationContext.getType(beanName).isAnnotationPresent(org.majorxie.dreamvc.annotation.Interceptor.class)==true*/) {
// applicationContext.getBean(beanName, Interceptor.class);
interceptorBeans.add(applicationContext.getBean(beanName));
log.info("init interceptor..");
} else if (applicationContext.getBean(beanName) instanceof Controller||applicationContext.getType(beanName).isAnnotationPresent(org.majorxie.dreamvc.annotation.Controller.class)==true) {
controllerBeans.add(applicationContext.getBean(beanName));
log.info("init controller....");
} else {
otherBeans.add(applicationContext.getBean(beanName));
log.info("init others...");
} } } @Override
public List<Object> getOthers() { return otherBeans;
} }
配置web.xml载入參数
<init-param>
<param-name>container</param-name>
<param-value>org.majorxie.dreamvc.ioc.factory.SpringIocFactory</param-value>
</init-param>
ok,我们的ioc集成已经完毕了,如今就能够在application.xml配置了
<span style="white-space:pre"> </span><bean id="test" class="test.ConTest"></bean>
<bean id="inter2" class="test.Interceptor_02"></bean>
<bean id="inter" class="test.LoginInterceptor"></bean>
下一篇,会解说dreamvc的机制~~么么哒
转载请注明出处http://blog.csdn.net/a837199685
dreamvc框架(一)ioc容器的集成的更多相关文章
- Spring框架及IOC容器
Spring是一个非常活跃的开源框架, 它是一个基于IOC和AOP来构架多层JavaEE系统的框架,它的主要目地是简化企业开发.Spring以一种非侵入式的方式来管理你的代码, Spring提倡”最少 ...
- Spring框架 之IOC容器 和AOP详解
主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置 一.S ...
- 【.NET6+WPF】WPF使用prism框架+Unity IOC容器实现MVVM双向绑定和依赖注入
前言:在C/S架构上,WPF无疑已经是"桌面一霸"了.在.NET生态环境中,很多小伙伴还在使用Winform开发C/S架构的桌面应用.但是WPF也有很多年的历史了,并且基于MVVM ...
- 基于nutz框架理解Ioc容器
同样我们从问题入手去验证以及去理解Ioc容器都做了哪些事情: 1.nutz是有几种方式获取需要容器管理bean的信息? 第一种是使用json格式的文件进行配置,如: 第二种:使用注解@IocBean ...
- Spring框架学习[IoC容器高级特性]
1.通过前面4篇文章对Spring IoC容器的源码分析,我们已经基本上了解了Spring IoC容器对Bean定义资源的定位.读入和解析过程,同时也清楚了当用户通过getBean方法向IoC容器获取 ...
- ASP.NET Web API 框架研究 IoC容器 DependencyResolver
一.概念 1.IoC(Inversion of Control),控制反转 即将依赖对象的创建和维护交给一个外部容器来负责,而不是应用本身.如,在类型A中需要使用类型B的实例,而B的实例的创建不是由A ...
- spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean
5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...
- Spring IoC容器总结(未完)
在面向对象系统中,对象封装了数据和对数据的处理,对象的依赖关系常常体现在对数据和方法的依赖上.这些依赖关系可以通过把对象的依赖注入交给框架或IOC容器来完成,这种从具体对象手中交出控制的做法是非常有价 ...
- Spring IOC容器的实现原理
1 概述 1.1 依赖反转模式 在Java中,一个复杂的功能一般都需要由两个或者两个以上的类通过彼此合作来实现业务逻辑的,这使得每个对象都需要与其合作的对象的引用.如果这个获取依赖对象的过程需要自己去 ...
随机推荐
- [Machine Learning] 梯度下降(BGD)、随机梯度下降(SGD)、Mini-batch Gradient Descent、带Mini-batch的SGD
一.回归函数及目标函数 以均方误差作为目标函数(损失函数),目的是使其值最小化,用于优化上式. 二.优化方式(Gradient Descent) 1.最速梯度下降法 也叫批量梯度下降法Batch Gr ...
- 11-18的学习总结(DOMSecondday)
DOM:读取访问节点对象属性 批量删除父元素下所有子节点 elem.innerHTML=""; 批量替换父元素下所有子节点 elem.innerHTML="所有子元素标签 ...
- android按行读取文件内容的几个方法
一.简单版 import java.io.FileInputStream; void readFileOnLine(){ String strFileName = "Filename.txt ...
- 【Python开发实战】Windows7+VirtualBox+Ubuntu环境配置
1. VirtualBox的安装 参考常规安装方式即可. VirtualBox 4.3.14 for Windows hosts:http://download.virtualbox.org/virt ...
- Django设置TIME_ZONE和LANGUAGE_CODE为中国区域
Django默认的timezone是 TIME_ZONE = 'America/Chicago' LANGUAGE_CODE = 'en-us' 设置为中国区域: TIME_ZONE = 'Asia/ ...
- Java实现Http服务器(四)
(1)HTTPServer的监听启动 sun.net.httpserver.ServerImpl类中启动了Socket监听,ServerImpl的内部类Dispatch类启动了Http服务器的监听 / ...
- Zoj 3868 GCD Expectation
给一个集合,大小为n , 求所有子集的gcd 的期望和 . 期望的定义为 这个子集的最大公约数的K次方 : 每个元素被选中的概率是等可能的 即概率 p = (发生的事件数)/(总的事件数); 总的事件 ...
- matlab中元胞数组(cell)转换为矩阵
matlab中元胞数组(cell)转换为矩阵. cell转换为矩阵函数为:cell2mat(c),其中c为待转换的元胞数组: 转化之后的矩阵可能不满足我们对矩阵维数的要求,那么也许还需要下面两个函数: ...
- API通常的url语法
?后面带的是get方式传递的值,如果有多个值,用 & 号分割.另外正式项目一般不用get方式传递,容易被人sql注入,即所谓的入侵. 详细看这篇http://www.cnblogs.com/k ...
- 如何用 React Native 创建一个iOS APP?(二)
我们书接上文<如何用 React Native 创建一个iOS APP?>,继续来讲如何用 React Native 创建一个iOS APP.接下来,我们会涉及到很多控件. 1 AppRe ...