zoukankan      html  css  js  c++  java
  • Spring(二) Mini版Spring的实现

    实现思路

    先来介绍一下 Mini 版本的 Spring 基本实现思路,如下图所示:

    自定义配置

    配置 application.properties 文件
    为了解析方便,我们用 application.properties 来代替 application.xml 文件,具体配置内容如下:
    scanPackage=com.gupaoedu.demo 

    配置 web.xml 文件

    大家都知道,所有依赖于 web 容器的项目,都是从读取 web.xml 文件开始的。我们先配置好 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>Gupao Web Application</display-name>
    <servlet>
    <servlet-name>gpmvc</servlet-name>
    <servlet-class>com.gupaoedu.mvcframework.v1.servlet.GPDispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>application.properties</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>gpmvc</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>
    </web-app>
    其中 GPDispatcherServlet 是有自己模拟 Spring 实现的核心功能类。

    自定义 Annotation

    @GPService 注解:
    package com.gupaoedu.mvcframework.annotation;
    import java.lang.annotation.*;
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface GPService {
    String value() default "";
    }
    @GPAutowired 注解:
    package com.gupaoedu.mvcframework.annotation;
    import java.lang.annotation.*;
    @Target({ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface GPAutowired {
    String value() default "";
    }
    @GPController 注解:
    package com.gupaoedu.mvcframework.annotation; 
    import java.lang.annotation.*;
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface GPController {
    String value() default "";
    }
    @GPRequestMapping 注解:
    package com.gupaoedu.mvcframework.annotation;
    import java.lang.annotation.*;
    @Target({ElementType.TYPE,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface GPRequestMapping {
    String value() default "";
    }
    @GPRequestParam 注解:
    package com.gupaoedu.mvcframework.annotation;
    import java.lang.annotation.*;
    @Target({ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface GPRequestParam {
    String value() default "";
    }
    配置 Annotation
    下面附上代码链接:
     https://github.com/FLGBetter/MiniSpring
  • 相关阅读:
    初学微信小程序 TodoList
    设计一个基于svg的涂鸦组件(一)
    基于51单片机的12864驱动
    java 使用xom对象数据序列化为xml、反序列化、Preferences相关操作小案例
    ios UIWebView 播放优酷土豆视频
    VMware Player 使用错误集锦
    Django 使用UEditor
    Entity Framework底层操作封装V2版本号(3)
    cocos2dx笔记1:概述
    oracle10g精简版安装步骤
  • 原文地址:https://www.cnblogs.com/flgb/p/12493139.html
Copyright © 2011-2022 走看看