一、概述
本章讲解了一个mvc框架的核心部分,大致分为9个部分
1.开发环境:pom.xml文件
2.配置文件:smart.properties(不属于框架,在实际的web应用中定义)
3.加载配置项: (interface) ConfigConstant
(final class) ConfigHelper
4.类加载器: (final class) ClassUtil
(@Interface) Controller、Service、Action、Inject
(final class) ClassHelper
5.Bean容器:(final class) Reflection
(final class) BeanHelper
6.依赖注入: (final class) IocHelper
7.加载Controller:(final class) ControllerHelper
8.初始化框架:(final class) HelperLoader
9.请求转发器:(class) Request、Handler、Param
(class) DispatcherServlet extend HttpServlet
(final class) StreamUtil、CodeUtil、JsonUtil
二、框架核心的具体实现
2.1 想要框架达成的使用效果:
1 package cn.m4.chapter3.controller; 2 3 4 5 import cn.ease4j.annotation.Action; 6 import cn.ease4j.annotation.Controller; 7 import cn.ease4j.annotation.Inject; 8 import cn.ease4j.bean.Data; 9 import cn.ease4j.bean.Param; 10 import cn.ease4j.bean.View; 11 import cn.m4.chapter3.entity.Customer; 12 import cn.m4.chapter3.service.MyService; 13 14 import java.util.List; 15 16 @Controller 17 public class MyController { 18 19 @Inject 20 private MyService myService; 21 22 @Action("get:/customer_create") 23 public Data getCustomerList(Param param){ 24 List<Customer> customerList = myService.getCustomerById(); 25 return new Data(customerList); 26 } 27 }
2.2 框架核心的具体搭建
2.2.1 首先添加一个Java web框架的基本依赖,具体步骤是:在IDEA中新建一个Maven工程,在pom文件中添加如下10个基本依赖
(Servlet、JSP、JSTL、JUNIT、SLF4J、mysql、JSON、Apache CommonsLand、Apache Commons Collections、Apache Commons DBUtils、dbcp)
View Code
<dependencies> <!--01 Servlet依赖--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--02 JSP依赖--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!--03 JSTL依赖--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--04 SLF4J--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <!--05 mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> <scope>runtime</scope> </dependency> <!--06 Json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.4</version> </dependency> <!--添加两个Apache Commons依赖,用于提供常用工具类--> <!--07 Apach Commons Lang--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <!--08 Apache Commons Collections--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.0</version> </dependency> <!--09 Apache Commons DBUtils--> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.6</version> </dependency> <!--10 数据库连接池--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.0.1</version> </dependency> <!-- cglib --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency>
2.2.2 提供获取并处理框架配置的工具类:ConfigConstant、ConfigHelper
ConfigConstant是一个存放.properties配置文件中常量名称的接口,ConfigHelper则是调用第二章中的PropUtil工具类去读取配置
(未完。。)