手写代码提炼Spring核心原理

基本实现思路

一.配置阶段:配置web.xml (DispatcherServlet) —>设定init-param(contextConfigLocation=classpath:application.xml)—>设定url-pattern(/*)—>配置Annotation(@Controller @Service @Autowired @RequestMapping)

二.初始化阶段:调用init()方法(加载配置文件)—>IOC容器初始化(Map)—>扫描相关的类(scan-package=“com.spring”)—>创建实例化并保存至容器(通过反射机制将类实例化放入IOC容器)—>进行DI操作(扫描IOC容器中的实例),给没有赋值的属性自动赋值—>初始化HandleMapping(将一个URL和一个Method进行一对一的关联映射Map

三.运行阶段:调用doPost()/doGet() (Web容器调用doPost/doGet方法,获取request/response对象)—>匹配HandlerMapping (从request对象中获得用户输入的url,找到其对应的Method)—>反射调用method.invoker()(利用反射调用方法并返回结果)—>response.getWrite().write()(将返回结果输出到浏览器)

自定义配置

配置application.properties文件:

scanPackage=com.spring

配置web.xml文件



    Web Application
    
        zjhmvc
        com.spring.mvcframework.v1.servlet.ZJHDispatcherServlet
        
            contextConfigLocation
            application.properties
        
        1
    
    
        zjhmvc
        /*
    

自定义Annotation

@ZJHService 注解:

package annotation;

import java.lang.annotation.*;

/**
 * Created by Lenovo on 2019-8-10.
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZJHService {
    String value() default "";
}

@ZJHAutowired

package annotation;

import java.lang.annotation.*;

/**
 * Created by Lenovo on 2019-8-10.
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZJHAutowired {
    String value() default "";
}

@ZJHController

package annotation;

import java.lang.annotation.*;

/**
 * Created by Lenovo on 2019-8-10.
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZJHController {
    String value() default "";
}

@ZJHRequestMapping

package annotation;

import java.lang.annotation.*;

/**
 * Created by Lenovo on 2019-8-10.
 */
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZJHRequestMapping {
    String value() default "";
}

@ZJHRequestParam

package annotation;

import java.lang.annotation.*;

/**
 * Created by Lenovo on 2019-8-10.
 */
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZJHRequestParam {
    String value() default "";
}

配置Annotation

service 接口

package service;

/**
 * Created by Lenovo on 2019-8-10.
 */
public interface IDemoService {
   public String get(String name);
}

service实现类

package service;

import annotation.ZJHService;

/**
 * Created by Lenovo on 2019-8-10.
 */
@ZJHService
public class DemoService implements IDemoService {
    public String get(String name) {
        return "My name is"+name;
    }
}

配置入口类DemoAction:

package action;

import annotation.ZJHAutowired;
import annotation.ZJHRequestMapping;
import annotation.ZJHRequestParam;
import service.IDemoService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Lenovo on 2019-8-10.
 */
public class DemoAction {
    @ZJHAutowired
    private IDemoService demoService;
    @ZJHRequestMapping("/query")
    public void query(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("name") String name){
        String result =demoService.get(name);
        try{
            resp.getWriter().write(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @ZJHRequestMapping("/add")
    public void add(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("a") Integer a,@ZJHRequestParam("b") Integer b){
        try{
            resp.getWriter().write(a+"+"+b+"="+(a+b));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @ZJHRequestMapping("/remove")
    public void remove(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("id") Integer id){

    }
}

容器初始化

package action;

import annotation.ZJHAutowired;
import annotation.ZJHRequestMapping;
import annotation.ZJHRequestParam;
import service.IDemoService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Lenovo on 2019-8-10.
 */
public class DemoAction {
    @ZJHAutowired
    private IDemoService demoService;
    @ZJHRequestMapping("/query")
    public void query(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("name") String name){
        String result =demoService.get(name);
        try{
            resp.getWriter().write(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @ZJHRequestMapping("/add")
    public void add(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("a") Integer a,@ZJHRequestParam("b") Integer b){
        try{
            resp.getWriter().write(a+"+"+b+"="+(a+b));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @ZJHRequestMapping("/remove")
    public void remove(HttpServletRequest req,HttpServletResponse resp,@ZJHRequestParam("id") Integer id){

    }
}

你可能感兴趣的:(Spring)