doodle 是一个简易的 Java MVC 框架,它提供了类似于 spring 的Bean容器、IOC、AOP、MVC等功能。
代码简洁、轻量,适合用于参考学习spring。
DispatcherServlet
doodle 暂时功能没有完善,在使用过程中有一些约束条件
@Component
Controller
@RequestMapping
@RequestParam
git clone https://github.com/zzzzbw/doodle.git 或直接下载代码到本地
git clone https://github.com/zzzzbw/doodle.git
将项目导入到IDE中,这是maven工程,确保你已经安装maven
执行doodle-example/src/main/java下的github.zzzzbw.App类的main()方法即可启动Tomcat服务器。
doodle-example/src/main/java
github.zzzzbw.App
main()
http://localhost:9090/user/list
http://localhost:9090/user/detail?id=1
post
http://localhost:9090/user/add?name=zzzzbw
http://localhost:9090/user/delete?id=1
创建一个 maven 项目,引入核心依赖。
<dependency> <groupId>com.github.zzzzbw</groupId> <artifactId>doodle-core</artifactId> <version>0.1</version> </dependency>
启动类:
public class App { public static void main(String[] args) { Doodle.run(App.class, 9090); } }
使用方式与 spring 基本一致,不过只支持类型注入,即如果被注入对象为类,则注入其实例,如果被注入对象为接口,则注入其实现类。
@Component public class EmailComponent { // do something }
为UserController注入EmailComponent
UserController
EmailComponent
@Controller public class UserController { @Autowired private EmailComponent emailComponent; // do something }
MethodBeforeAdvice
AfterReturningAdvice
ThrowsAdvice
AroundAdvice
@Aspect
(可选)@Order注解来决定增强处理类的先后顺序
@Order
@Order(1) @Aspect(pointcut = “@within(Controller)”) public class Aspect1 implements AroundAdvice { @Override public void afterReturning(Class<?> clz, Object returnValue, Method method, Object[] args) throws Throwable { // do something } @Override public void before(Class<?> clz, Method method, Object[] args) throws Throwable { // do something } @Override public void afterThrowing(Class<?> clz, Method method, Object[] args, Throwable e) { // do something } }
@Order(2) @Aspect(pointcut = “@within(Controller)”) public class Aspect2 implements AroundAdvice { @Override public void afterReturning(Class<?> clz, Object returnValue, Method method, Object[] args) throws Throwable { // do something }
@Override public void before(Class<?> clz, Method method, Object[] args) throws Throwable { // do something } @Override public void afterThrowing(Class<?> clz, Method method, Object[] args, Throwable e) { // do something }
}
上面两个增强处理类Aspect1和Aspect2,会增强被@Controller注解的类下的方法,且顺序为:
Aspect1
Aspect2
@Controller
Aspect1@before()->Aspect2@before()->method.invoke()->Aspect2@afterReturning()->Aspect1@afterReturning()。
使用几乎与 spring 一致
返回String类型的则返回jsp页面的路径,返回ModelAndView则在其view中设置jsp页面路径,同时可以传一些参数给jsp页面。
被@ResponseBody注解的则返回的是json格式数据。
@ResponseBody
@Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/list", method = RequestMethod.GET) public ModelAndView getUserList() { List<User> list = userService.getUser(); return new ModelAndView().setView("user_list.jsp").addObject("list", list); } @RequestMapping(value = "/detail", method = RequestMethod.GET) public ModelAndView getUser(@RequestParam(value = "id") long id) { User user = userService.getUserById(id); return new ModelAndView().setView("user_detail.jsp").addObject("user", user); } @RequestMapping(value = "/add", method = RequestMethod.POST) @ResponseBody public Result<User> addUser(@RequestParam(value = "name") String name) { User user = userService.addUser(name); return new Result<>(user, 0, ""); } @RequestMapping(value = "/delete", method = RequestMethod.POST) @ResponseBody public Result<String> deleteUser(@RequestParam(value = "id") long id) { userService.deleteUser(id); return new Result<>("success", 0, ""); } }
本项目不仅参考了spring-boot,同时还参考了:
这些都是国人写的更轻量而且也很优秀的框架,相比spring-boot来说阅读源码的难度会小一些。感谢这些框架给我提供学习的思路,同时给我们更多的选择。