博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dreamvc框架(一)ioc容器的集成
阅读量:7008 次
发布时间:2019-06-28

本文共 3946 字,大约阅读时间需要 13 分钟。

我的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 getControllers()throws Exception;		/**	 * 是否是拦截器	 * @return	 */	List getInterceptors();		/**	 * 得到其它对象	 * @return	 */	List 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 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 controllerBeans = new ArrayList();	private List otherBeans = new ArrayList();	private List interceptorBeans = new ArrayList(); public void init(ServletContext context) { log.info("init context..."); applicationContext = WebApplicationContextUtils .getRequiredWebApplicationContext(context); initBeans(); } public List getControllers() throws NotHaveControllerException { if (controllerBeans.size() == 0) { throw new NotHaveControllerException("you need at least one controller "); } else { return controllerBeans; } } public List 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 getOthers() { return otherBeans; }}
配置web.xml载入參数

container
org.majorxie.dreamvc.ioc.factory.SpringIocFactory
ok,我们的ioc集成已经完毕了,如今就能够在application.xml配置了

	

下一篇,会解说dreamvc的机制~~么么哒

转载请注明出处http://blog.csdn.net/a837199685

本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5313291.html,如需转载请自行联系原作者
你可能感兴趣的文章