我的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
原文地址:http://blog.csdn.net/a837199685/article/details/41148003