前言
最近有些空,想自己写个跟spring里的注解一样的注解来用,然后希望能找到使用了自己写了注解的类,下面来介绍一下实现方法
声明,下面代码是没看过spring源码写的,基本上都是网上找的博客,整理的
定义注解
Controller.java
?
1 2 3 4 5 6 |
@Target ({ElementType.TYPE}) @Retention (RetentionPolicy.RUNTIME) @Documented public @interface Controller {
} |
RequestMapping.java
?
1 2 3 4 5 6 7 8 |
@Target ({ElementType.METHOD}) @Retention (RetentionPolicy.RUNTIME) @Documented public @interface RequestMapping {
String value(); } |
使用注解
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@Controller public class IndexController {
@RequestMapping ( "/" ) public void index() {
System.out.println( "index method" ) } @RequestMapping ( "/about" ) public void about(String args) {
System.out.println( "about method" ) } } |
扫描包下所有类