1. 简介
spring全部手写的过程,原理,设计模式 在博客里阐述,这是个浩大的工程.同时我也相信你是基本不能看到最后的.
这里博主写了个简易的springMvc.代码比较简单.而且都写了注释,我想比看博客看我BB,要简单的多.废话就不多说了,上github
2.项目介绍
项目地址 : https://github.com/cao2068959/ChySpring2.0.git
本项目实现了 ioc / mvc / aop
注解和原版 spring 一样, 只是在前面 加了 Chy 前缀
3. 启动方法
和原版的springmvc 一样 提供了一个
DispatcherServlet 作为启动的servlet .
如果只想启动ioc 那么使用 直接在main方法调用即可.
ChyAppilicationContext chyAppilicationContext = new ChyAppilicationContext("application.properties");
本项目因为精简的关系,能用注解配置的 就不会使用配置文件,所以不支持XML的配置方式.
application.properties文件中只要一个简单的 package=要扫描的包 即可
4 . 提供测试项目
github地址 : https://github.com/cao2068959/ChySpringDemo2.git
本项目是前面手写springMVC的应用测试项目.就是一个简单的 servlet 项目,你也可以自己手动创建
因为这个手写框架是 基于 maven构建的.所以要先下载第二章所提供的项目,然后安装到你对应的maven仓库中使用,直接使用
<dependency>
<groupId>chy</groupId>
<artifactId>ChySpring2.0</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
前面提到,使用方法和spring原版一样,在web.xml文件中使用
<servlet>
<servlet-name>chy</servlet-name>
<servlet-class>chy.spring.mvc.DispatcherServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>application.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>chy</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这里设置一个controller
@ChyController
@ChyRequestMapping("/chy/")
public class MyController {
@ChyAutowired
public MyService myService;
@ChyRequestMapping("/test")
public String test(@ChyRequestParam("age") Integer age, String xx, HttpServletRequest request){
System.out.println(age + "@@@@@@@@" + xx + "##########"+request);
return "/index.html";
}
@ChyRequestMapping("/test2")
@ChyResponebody
public Map<String,Object> test2(@ChyRequestParam("age") Integer age,@ChyRequestParam("name") String name,String xx, HttpServletRequest request){
Map<String,Object> pa = new HashMap<>();
pa.put("age",age);
pa.put("name",name);
return pa;
}
@ChyRequestMapping("/aop")
@ChyResponebody
public String aopTest(){
myService.xiaoxiao("xxxxx");
return "什么鬼";
}
}
其中 AOP 只能使用注解的方式配置.其中 切面的规则我使用了 正则的方式解析.
@ChyAspect
@ChyComponent
public class AopConfig {
@ChyPointcut("public .* chy\\.service\\.MyService\\..*(.*)")
public void pointcut(){}
@ChyAfter("pointcut()")
public void after(){
System.out.println("执行了after方法=====================");
}
@ChyBefore("pointcut()")
public void before(){
System.out.println("执行了before方法====================");
}
@ChyAround("pointcut()")
public void aroud(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕通知开始");
proceedingJoinPoint.proceed();
System.out.println("环绕通知结束");
}
}
执行结果
这里因为我在同一个方法配置了 2个aop 所以除了上面配置的还有其他配置的通知