FreeCMS商业版

功能强大、开源、代码通俗易懂、注释详细、面向二次开发友好!

面向二次开发友好的java开源CMS!打造更好的中国开源jsp java CMS!

FreeCMS商业版V2.7已发布!购买请联系客服QQ 124812878 查看更新日志



在线演示 立即购买 下载免费版 下载商业版用户手册
客服QQ: 124812878 联系电话: 18339991503 Email: freeteam@foxmail.com

手写 springmvc - Ctrl+C&Ctrl+V - ITeye博客

加入收藏夹】     【打印】     【关闭】 来源: 日期:2018-01-17 22:00:06 点击量: 收藏

 

 

手写 springmvc

代码下载 : demo 

 

 

 

结果测试:



 

 

项目结构

 

0.pom

 <properties>
		<project.build.sourceEncoding>UTF-8<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/project.reporting.outputEncoding>
		<java.version>1.8<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/java.version> 
		<org.slf4j-version>1.7.9<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/org.slf4j-version>
		<org.logback-version>1.0.1<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/org.logback-version>
		
	<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/properties>
	
  
  <dependencies>
   <!-- servlet 3.0 start -->
		<dependency>
			<groupId>javax.servlet<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/groupId>
			<artifactId>javax.servlet-api<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/artifactId>
			<version>3.0.1<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/version>
			<scope>provided<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/scope>
		<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/dependency>
		 <dependency>
            <groupId>javax.servlet<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/groupId>
            <artifactId>jstl<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/artifactId>
            <version>1.2<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/version>
        <http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/dependency>
		<dependency>
			<groupId>javax.servlet.jsp<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/groupId>
			<artifactId>jsp-api<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/artifactId>
			<version>2.1<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/version>
			<scope>provided<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/scope>
		<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/dependency>
		<!-- servlet 3.0 end -->
		
		   <!-- loggger start -->
			<dependency>
				<groupId>org.slf4j<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/groupId>
				<artifactId>log4j-over-slf4j<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/artifactId>
				<version>${org.slf4j-version}<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/version>
			<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/dependency>

			<dependency>
				<groupId>org.slf4j<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/groupId>
				<artifactId>slf4j-api<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/artifactId>
				<version>${org.slf4j-version}<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/version>
			<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/dependency>
			<!-- loggger end -->
			
			
			<!-- json start -->
			<dependency>
				<groupId>com.alibaba<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/groupId>
				<artifactId>fastjson<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/artifactId>
				<version>1.2.8<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/version>
			<http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/dependency>
			<!-- json end -->
			
  <http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/dependencies>

  

 

1. AttributeParams  

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

 

2.

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

 

3.

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

 

4.

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

 

5.

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

 

6.

@RequestMapping("http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/users")
@Controller
public class UserController {

	@AutoWrite
	private  UserService userService;
	 
	@RequestMapping("http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/user")
	public  User user(@AttributeParams("name") String name ){
		User user = userService.getUserByName(name);
		return user;
	} 
	
}

 

7.

public class User implements Serializable{

	private String name;
	private String pwd;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

}

 

8.

public interface UserService {

	public  User getUserByName(String name);

}

 

 

9.

@Service
public class UserServiceImpl implements UserService{

	@Override
	public User getUserByName(String name) {
		User user = new User();
		user.setName(name);
		user.setPwd(name +"123");
		return user;
	}

	 
}

 

 

10.

public class DispactherServlet extends HttpServlet{

	  
	private static final long serialVersionUID = 3077215544607397824L;

	private final Logger log = Logger.getLogger(DispactherServlet.class); 
	
	static List<String> packageNames = new ArrayList<String>();  
	static  Map<String, Object> instanceMapping = new ConcurrentHashMap<String, Object>(); 
	static  Map<String, ApplicationEntity> haddlerMapping = new ConcurrentHashMap<String, ApplicationEntity>(); 
	
	 @Override
	public void init() throws ServletException {
		 super.init();
		 String application = this.getInitParameter("contextConfigLocation");
		 http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/this.getServletContext().getClass().getResourceAsStream(application);
		 log.info("=== " +application);
	     String scanPackage="com.curiousby.baoyou.cn.showandshare.customized.mvc";
	     http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/1.获取扫描位置 
	     doScan(scanPackage); 
		 for (String packageName : packageNames) {
			 System.out.println(""+packageName);
		 }
		 
	     http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/2.实例化 service 和 controller
	     doInstances();
	     for (Entry<String,Object> entry : instanceMapping.entrySet()) {
			System.out.println(entry.getKey() +"    \t " + entry.getValue() );
		 }
	     
	     
	     http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/3. 建立映射关系 
	     doHaddlerMapping();
	     for (Entry<String,ApplicationEntity> entry : haddlerMapping.entrySet()) {
	    	 System.out.println(entry.getKey() +"    \t " + entry.getValue() );
		 }
	     
	     
	     http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/4.实现注入
	     doAop();
	     
	     http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/5.
	     
	     
	     
	 }
	 
	 private void doAop() { 
		 if (instanceMapping.size() <= 0 ) {
				return;
		 }
		 for (Map.Entry<String, Object> entry : instanceMapping.entrySet()) { 
			   Field[] declaredFields = entry.getValue().getClass().getDeclaredFields();
			   for (Field field : declaredFields) {
				   field.setAccessible(true);
				   if(field.isAnnotationPresent(AutoWrite.class)){
					   AutoWrite autoWrite = field.getAnnotation(AutoWrite.class);
					   String autoWriteName = autoWrite.value();
					   if(!UtilValidate.isNotEmpty(autoWrite.value())){
						   autoWriteName =field.getName();
					   }
					   autoWriteName = StringUtils.toLowerFirstString(autoWriteName);
					   field.setAccessible(true);
					   try {
						field.set(entry.getValue(), instanceMapping.get(autoWriteName));
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						}
				   }
			}
		 }
	 }

	private void doHaddlerMapping() {
		 if (instanceMapping.size() <= 0 ) {
				return;
		 }
		
		  for (Map.Entry<String, Object> entry : instanceMapping.entrySet()) {  
			  if (entry.getValue().getClass().isAnnotationPresent(Controller.class)) {
				  String baseUrl = "";
				  Controller controller = entry.getValue().getClass().getAnnotation(Controller.class);
				  
				 if(entry.getValue().getClass().isAnnotationPresent(RequestMapping.class)){
						RequestMapping requestMapping = (RequestMapping) entry.getValue().getClass().getAnnotation(RequestMapping.class);  
						baseUrl = requestMapping.value();
						
				 }
					
				  Method[] methods = entry.getValue().getClass().getMethods();  
				   for (Method method : methods) {
					   if (method.isAnnotationPresent(RequestMapping.class)) {  
						   RequestMapping methodRequestMapping = (RequestMapping) method.getAnnotation(RequestMapping.class);  
						   String url = baseUrl +  methodRequestMapping.value();
						   url = StringUtils.splitMultiPathToSimple("http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/"+url);
						   ApplicationEntity entity = new ApplicationEntity();
						   entity.method = method;
						   entity.controller = entry.getValue();  
						   haddlerMapping.put(url, entity);
						   
					   }else {  
	                       continue;  
	                   }  
				   }
			  }
		  }
	}

	private void doInstances() {
		if (packageNames.size() <= 0 ) {
			return;
		}
		
		for (String packageName : packageNames) {
			try {
				 Class<?> clazz = Class.forName(packageName.replace(".class", "").trim());  
				 String clazzName = clazz.getSimpleName();
				 
				if (clazz.isAnnotationPresent(Controller.class)) {
					Controller controller =  clazz.getAnnotation(Controller.class);
					if(UtilValidate.isNotEmpty(controller.value())){
						clazzName = controller.value();
					}
					Object newInstance = clazz.newInstance();
					clazzName = StringUtils.toLowerFirstString(clazzName);
					instanceMapping.put(clazzName, newInstance);
					
				}else if(clazz.isAnnotationPresent(Service.class)){
					Service service =  clazz.getAnnotation(Service.class);
					Class<?>[] interfaces = clazz.getInterfaces();
					if(UtilValidate.isNotEmpty(service.value())){
						clazzName = service.value();
					}else{
						for (Class<?> interfaceObj : interfaces) {
							clazzName = interfaceObj.getSimpleName();
							Object newInstance = clazz.newInstance();
							clazzName = StringUtils.toLowerFirstString(clazzName);
							instanceMapping.put(clazzName, newInstance);
						}
					}
					
					
					
					 
				}else{
					continue;
				}
			
				
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		
	}

	private void doScan(String scanPackage) {
		URL url = this.getClass().getClassLoader().getResource("http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/"+StringUtils.splitPathTransfer(scanPackage));
	    String path= url.getFile();
		for ( String file : new File(path).list()) {
	    	 File one = new File(path , file); 
	    	 if (one.isDirectory()) {
	    		 doScan(scanPackage +"."+ one.getName()); 
			 }else{
				 packageNames.add(scanPackage+"."+ one.getName());  
			 }
		}
		
	 }
	 
	 
	 

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	 
	 
	 @Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException { 
	     RespEntity  result = new RespEntity();
	     String url = req.getRequestURI();  
	     String context = req.getContextPath();  
	     String path = url.replace(context, "").trim();  
	     ApplicationEntity entity = (ApplicationEntity) haddlerMapping.get(path);
         if(null == entity){
        	 result.setCode(RespEnums.RESP_ERROR_NOT_FOUND.getCode());
        	 result.setMsg(RespEnums.RESP_ERROR_NOT_FOUND.getDesc());
        	 result.setTimestamp(System.currentTimeMillis());
        	 out(resp, FastJsonUtils.toJSONString(result));
        	 return;
         }	     
	     List<Object> bulidParameters = bulidParameters(req,resp,entity);
	     try {
	    	 Object obj = entity.method.invoke( entity.controller,bulidParameters.toArray()  );
	    	 System.out.println(obj);  
	    	 result = new RespEntity(obj);
	    	 out(resp, FastJsonUtils.toJSONString(result));
        	 return;
	     } catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	 
	 
	 private List<Object> bulidParameters(HttpServletRequest req,
			HttpServletResponse resp, ApplicationEntity entity) {
		 Parameter[] parameters = entity.method.getParameters();
		 List<Object> values = new ArrayList<Object>();
		 for (Parameter parameter : parameters) {
			if (parameter.getParameterizedType().getTypeName().equals("HttpServletRequest")) {
				values.add(req);
			}else if (parameter.getParameterizedType().getTypeName().equals("HttpServletResponse")) {
				values.add(resp);
			}else if(parameter.isAnnotationPresent(AttributeParams.class)){
				 String paramName = parameter.getAnnotation(AttributeParams.class).value();
				 if(!UtilValidate.isNotEmpty(paramName)){
					 paramName = parameter.getName();
				 }
				values.add(req.getParameter(paramName));
			}else{
				String paramName = parameter.getName();
				values.add(req.getParameter(paramName));
			}
		}
		 return values;
	}

	private void out(HttpServletResponse response, String str) {  
	        try {  
	            response.setContentType("applicationhttp:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/json;charset=utf-8");  
	            response.getWriter().print(str);  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	 }  
}

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者 

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。

 

个人主页http:http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/



 
 
 谢谢您的赞助,我会做的更好!

 


上一条


下一条

相关新闻
自定义表单

本例固定链接: 分享到:
更多