我的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; } }
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
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5313291.html,如需转载请自行联系原作者