自己动手写SpringMVC(三)

此篇我们开始写DispacherServlet之前先好一个包含query方法的Controller,以及Controller里调用的Service接口,以及此接口的实现类:

首先代码结构图如下:

1.Controller  中实现代码如下:

@Controller
@RequestMapping("/student")
public class StudentController {
	@AutoWired("StudentServiceImpl")   
	private StudentService studentService;
	
	@RequestMapping("/query")
	public void query(HttpServletRequest request,HttpServletResponse response,
			@RequestParam("name") 
			String name,
			@RequestParam("age") 
			String age){
		
		try{
			PrintWriter pw = response.getWriter();
			String result = studentService.query(name, age);
			pw.write(result);
		}catch(IOException e){
			e.printStackTrace();
		}		
	}
}

2.Service中实现代码如下:

public interface StudentService {
	String query(String name,String age);
}

3.ServiceImpl实现代码如下:(这里省略持久层,查数据的代码)

@Service("StudentServiceImpl")
public class StudentServiceImpl implements StudentService {
	public String query(String name,String age){
		return "studentName:"+name+"\n studentAge:"+age;
	}
}

以上三段代码是我们平时使用SpringMVC中最最常见的代码和注解了,现在我们把这些代码进行改造,改造前先自己定义自己的相应注解:

@Controller              --------          @MyController

@RequestMapping  ---------        @MyRequestMapping 

@AutoWired            ----------       @MyAutoWired

@RequestParam     ----------      @MyRequestParam

扫描二维码关注公众号,回复: 3074735 查看本文章

@Service                 ----------      @MyService

@MyController

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyController {
	String value() default "";
}

@MyRequestMapping 

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented  
public @interface MyRequestMapping {
	String value() default "";
}

@MyAutoWired

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAutoWired {
	String value() default "";
}

@MyRequestParam

@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyRequestParam {
	String value() default "";
}

@MyService

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyService {
	String value() default "";
}

注解已经定义好,现在我们把上面的代码中注解修改为我们自己定义的注解,代码如下:

@MyController
@MyRequestMapping("/student")
public class StudentController {
	@MyAutoWired("StudentServiceImpl")   
	private StudentService studentService;
	
	@MyRequestMapping("/query")
	public void query(HttpServletRequest request,HttpServletResponse response,
			@MyRequestParam("name") 
			String name,
			@MyRequestParam("age") 
			String age){
		
		try{
			PrintWriter pw = response.getWriter();
			String result = studentService.query(name, age);
			pw.write(result);
		}catch(IOException e){
			e.printStackTrace();
		}		
	}
}
@MyService("StudentServiceImpl")
public class StudentServiceImpl implements StudentService {
	public String query(String name,String age){
		return "studentName:"+name+"\n studentAge:"+age;
	}
}

现在运行代码,肯定运行不出结果来,因为没有程序根据注解创建bean对象,没有进行注入,没有产生映射关系,因此我们最后剩下一步就是实现前面说了很久的DispatcherServlet!

猜你喜欢

转载自blog.csdn.net/tangtang1226/article/details/81266804