转自:http://blog.csdn.net/chaoyueygw/article/details/53393952 略作修改
1.第一步编写几个注解,名称作用与SpringMVC中相对应
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Controller {
String value() default "";
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Service {
String value() default "";
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Quatifier {
String value() default "";
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestMapping {
String value() default "";
}
2.第二步 编写Servlet类,在init()方法中 完成实例化和注入,以及建立映射关系。
①包扫描:将整个项目所有类扫描并保存起来,对应下面scanPackage(path)方法,包参数直接写死就行。
②过滤和实例化:将有Controller和Service注释的类过滤出来,实例化。并以注释value为key值,实例为value存储起来
③建立映射关系:将RequestMapping注释的value与注释的方法method对象存储起来
④注入:根据注解将Service实例注入到Controller中的属性
servlet代码如下:
@WebServlet("/")
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
List<String> packageNames = new ArrayList<String>();
// 所有类的实例,key是注解的value,value是所有类的实例
Map<String, Object> instanceMap = new HashMap<String, Object>();
Map<String, Object> handerMap = new HashMap<String, Object>();
public DispatcherServlet() {
super();
}
public void init() throws ServletException {
// 包扫描,获取包中的文件
scanPackage("com.chaoyue");
try {
filterAndInstance();
} catch (Exception e) {
e.printStackTrace();
}
// 建立映射关系
handerMap();
// 实现注入
ioc();
}
private void filterAndInstance() throws Exception {
if (packageNames.size() <= 0) {
return;
}
for (String className : packageNames) {
Class<?> cName = Class.forName(className.replace(".class", "").trim());
if (cName.isAnnotationPresent(Controller.class)) {
Object instance = cName.newInstance();
Controller controller = (Controller) cName.getAnnotation(Controller.class);
String key = controller.value();
instanceMap.put(key, instance);
} else if (cName.isAnnotationPresent(Service.class)) {
Object instance = cName.newInstance();
Service service = (Service) cName.getAnnotation(Service.class);
String key = service.value();
instanceMap.put(key, instance);
} else {
continue;
}
}
}
private void ioc() {
if (instanceMap.isEmpty())
return;
for (Map.Entry<String, Object> entry : instanceMap.entrySet()) {
// 拿到里面的所有属性
Field fields[] = entry.getValue().getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);// 可访问私有属性
if (field.isAnnotationPresent(Quatifier.class));
Quatifier quatifier = field.getAnnotation(Quatifier.class);
String value = quatifier.value();
field.setAccessible(true);
try {
field.set(entry.getValue(), instanceMap.get(value));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
/**
* 扫描包下的所有文件
*
* @param Package
*/
private void scanPackage(String Package) {
URL url = this.getClass().getClassLoader().getResource("/" + replaceTo(Package));// 将所有的.转义获取对应的路径
String pathFile = url.getFile();
File file = new File(pathFile);
String fileList[] = file.list();
for (String path : fileList) {
File eachFile = new File(pathFile + path);
if (eachFile.isDirectory()) {
scanPackage(Package + "." + eachFile.getName());
} else {
packageNames.add(Package + "." + eachFile.getName());
}
}
}
/**
* 建立映射关系
*/
private void handerMap() {
if (instanceMap.size() <= 0)
return;
for (Map.Entry<String, Object> entry : instanceMap.entrySet()) {
if (entry.getValue().getClass().isAnnotationPresent(Controller.class)) {
Controller controller = (Controller) entry.getValue().getClass().getAnnotation(Controller.class);
String ctvalue = controller.value();
Method[] methods = entry.getValue().getClass().getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(RequestMapping.class)) {
RequestMapping rm = (RequestMapping) method.getAnnotation(RequestMapping.class);
String rmvalue = rm.value();
handerMap.put("/" + ctvalue + "/" + rmvalue, method);
} else {
continue;
}
}
} else {
continue;
}
}
}
private String replaceTo(String path) {
return path.replaceAll("\\.", "/");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String url = req.getRequestURI();
String context = req.getContextPath();
String path = url.replace(context, "");
Method method = (Method) handerMap.get(path);
SpringmvcController controller = (SpringmvcController) instanceMap.get(path.split("/")[1]);
try {
method.invoke(controller, new Object[] { req, resp, null });
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
Controller类代码如下:
@Controller("controller")
public class SpringmvcController {
@Quatifier("MyServiceImpl")
MyService myService;
@RequestMapping("insert")
public String insert(HttpServletRequest request, HttpServletResponse response, String param) {
myService.insert(null);
return null;
}
@RequestMapping("delete")
public String delete(HttpServletRequest request, HttpServletResponse response, String param) {
myService.delete(null);
return null;
}
@RequestMapping("update")
public String update(HttpServletRequest request, HttpServletResponse response, String param) {
myService.update(null);
return null;
}
@RequestMapping("select")
public String select(HttpServletRequest request, HttpServletResponse response, String param) {
myService.select(null);
return null;
}
}
Service接口及实现类如下:
public interface MyService {
int insert(Map map);
int delete(Map map);
int update(Map map);
int select(Map map);
}
@Service("MyServiceImpl")
public class MyServiceImpl implements MyService {
@Override
public int insert(Map map) {
System.out.println("MyServiceImpl:" + "insert");
return 0;
}
@Override
public int delete(Map map) {
System.out.println("MyServiceImpl:" + "delete");
return 0;
}
@Override
public int update(Map map) {
System.out.println("MyServiceImpl:" + "update");
return 0;
}
@Override
public int select(Map map) {
System.out.println("MyServiceImpl:" + "select");
return 0;
}
}
这样运行项目,输入地址:localhost:8080/项目名/controller/insert
控制台打印 MyServiceImplinsert