SSM源碼分析之Spring07-手寫SpringV1.0

前言

《SSM源碼分析之Spring05-DI實現原理(基於Annotation 注入)》
《SSM源碼分析之Spring06-IOC 容器中那些鮮爲人知的事兒》
前面的章節我們主要看了一下在spring源碼是如何設計管理bean的。這節我們用簡單的幾個類實現一個核心的IOC功能~

手寫SpringV1.0

準備工作

我們首先整理一下思路:
在這裏插入圖片描述
然後,創建一個maven項目,最後的項目結構如下:
在這裏插入圖片描述
pom.xml引入servlet依賴:

    <properties>
        <!-- dependency versions -->
        <servlet.api.version>2.4</servlet.api.version>
    </properties>

    <dependencies>
        <!-- requied start -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.api.version}</version>
            <scope>provided</scope>
        </dependency>
        <!-- requied end -->
    </dependencies>

在web.xml中註冊Servlet:(關於servlet這裏不做過多解釋,可以參考文章快速創建一個servlet並且在web.xml配置和使用它)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">
	<display-name>Spring Application</display-name>

	<servlet>
		<servlet-name>mvc</servlet-name>
		<servlet-class>com.xxx.vip.spring.servlet.DispatchServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:application.properties</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

application.properties配置文件:

scanPackage=com.xxx.vip.demo
templateRoot=layouts

DispatchServlet實現Bean的定位、加載、註冊


public class DispatchServlet extends HttpServlet {

    private Properties contextConfig = new Properties();

    private Map<String,Object> beanMap = new ConcurrentHashMap<String,Object>();

    private List<String> classNames = new ArrayList<String>();


    @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 {

        System.out.println("---------- 調用doPost ----------");


    }

    @Override
    public void init(ServletConfig config) throws ServletException {

        //開始初始化的進程

        //定位
        doLoadConfig(config.getInitParameter("contextConfigLocation"));

        //加載
        doScanner(contextConfig.getProperty("scanPackage"));

        //註冊
        doRegistry();

        //自動依賴注入

        //在Spring中是通過調用getBean方法纔出發依賴注入的
        doAutowired();


//        DemoAction action = (DemoAction)beanMap.get("demoAction");
//        action.query(null,null,"Tom");

        //如果是SpringMVC會多設計一個HnandlerMapping

        //將@RequestMapping中配置的url和一個Method關聯上
        //以便於從瀏覽器獲得用戶輸入的url以後,能夠找到具體執行的Method通過反射去調用
        initHandlerMapping();

    }

    private void initHandlerMapping() {
    }

    private void doRegistry() {

        if(classNames.isEmpty()){ return;}

        try{

            for(String className : classNames){
                Class<?> clazz = Class.forName(className);


                //在Spring中用的多個子方法來處理的
                if(clazz.isAnnotationPresent(Controller.class)){

                    String beanName = lowerFirstCase(clazz.getSimpleName());

                    //在Spring中在這個階段不是不會直接put instance,這裏put的是BeanDefinition
                    beanMap.put(beanName,clazz.newInstance());

                }else if(clazz.isAnnotationPresent(Service.class)){

                    Service service = clazz.getAnnotation(Service.class);

                    //默認用類名首字母注入
                    //如果自己定義了beanName,那麼優先使用自己定義的beanName
                    //如果是一個接口,使用接口的類型去自動注入

                    //在Spring中同樣會分別調用不同的方法 autowriedByName autowritedByType

                    String beanName = service.value();
                    if("".equals(beanName.trim())){
                        beanName = lowerFirstCase(clazz.getSimpleName());
                    }

                    Object instance = clazz.newInstance();

                    beanMap.put(beanName,instance);

                   Class<?>[] interfaces = clazz.getInterfaces();

                   for (Class<?> i :interfaces){
                       beanMap.put(i.getName(),instance);
                   }

                }else{
                    continue;
                }

            }


        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void doAutowired() {
        if(beanMap.isEmpty()){ return; }

        for (Map.Entry<String,Object> entry : beanMap.entrySet()) {

            Field [] fields = entry.getValue().getClass().getDeclaredFields();

            for (Field field : fields){

                if(!field.isAnnotationPresent(Autowried.class)){continue;}

                Autowried autowried = field.getAnnotation(Autowried.class);

                String beanName = autowried.value().trim();
                if("".equals(beanName)){
                    beanName = field.getType().getName();
                }
                field.setAccessible(true);

                try {
                    field.set(entry.getValue(),beanMap.get(beanName));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            }

        }
    }

    private void doScanner(String packageName) {

        URL url = this.getClass().getClassLoader().getResource("/" + packageName.replaceAll("\\.","/"));

        File classDir = new File(url.getFile());

        for (File file : classDir.listFiles()){
            if(file.isDirectory()){
                doScanner(packageName + "." +file.getName());
            }else {
                classNames.add(packageName + "." + file.getName().replace(".class",""));
            }
        }


    }

    private void doLoadConfig(String location) {
        //在Spring中是通過Reader去查找和定位對不對
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(location.replace("classpath:",""));

        try {
            contextConfig.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null != is){is.close();}
            }catch (Exception e){
                e.printStackTrace();
            }

        }


    }


    private String lowerFirstCase(String str){
        char [] chars = str.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }

}

自定義註解

Autowried

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

Controller

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Controller {

    String value() default  "";
}

RequestMapping

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

}

RequestParam

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

Service

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

測試

Service層:

public interface IDemoService {
	
	String get(String name);
	
}

實現類:

@Service
public class DemoService implements IDemoService {

	public String get(String name) {
		return "My name is " + name;
	}

}

Controller層:

@Controller
public class MyAction {

		@Autowried IDemoService demoService;
	
		@RequestMapping("/index.html")
		public void query(){

		}
	
}

帶參數測試

@Controller
@RequestMapping("/demo")
public class DemoAction {
	
	@Autowried private IDemoService demoService;
	
	@RequestMapping("/query.json")
	public void query(HttpServletRequest req,HttpServletResponse resp,
		   @RequestParam("name") String name){
		String result = demoService.get(name);
		System.out.println(result);
//		try {
//			resp.getWriter().write(result);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
	
	@RequestMapping("/edit.json")
	public void edit(HttpServletRequest req,HttpServletResponse resp,Integer id){

	}
	
}

後記

本節springV1.0的代碼地址如下:手寫spring的ioc容器
在後續章節,我們依次手寫實現spring的AOP、springMVC等核心功能

發佈了57 篇原創文章 · 獲贊 5 · 訪問量 5012
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章