A core function to achieve their own spring

 

Talk about spring

       spring for java developers, is the most familiar of the framework, our daily development are using it every day. It has a variety of benefits, easy to use, handy ... ...

We will be talked about when it comes to spring ioc, aop, dependency injection, notes and other professional terms, a lot of people new to java, are confused, it is difficult to intuitively understand What the hell, but the use of multiple I fell in love with the convenience it has brought us.

 

Exploring spring

     When we use it will be skilled curious, ioc how to achieve it? Why me just add @AutoWrited in the variable class will be able to use this variable? With doubts we will begin to turn the source code, find the answers. After some effort, we finally locate the spring-context.jar package, found

Refresh method AbstractApplicationContext object, there is the realization and steps, is the core, which is the realization of the entire spring function of our other amazing, but also quite complex, uses a lot of design patterns, it is difficult to get a glimpse of the whole picture, a lot of places we do not design I know why it takes so realized. But we still know ioc container is actually used Hashmap, 
dependency injection using a reflective, aop actually a dynamic proxy completed. There is a great part of the code to see a little dizzy, it is to strengthen the robustness of the life cycle and ensure various characteristics. Anyway, it was all someone else's stuff, we only use the source code we looked at a lot, but few do it yourself out.

spring realization analysis

  Anyway, other people realize that other people write your own stuff is out of our own. Now that he is determined to achieve, and then we need to analyze analyzed.

      spring how to do less intrusive code for all levels and clear it?

         using the spring configuration is greater than the convention, according to a fixed pattern, hierarchy into Controller, Service, Component, Bean, Configuration to identify the type

      spring is how to manage dependencies?

          By annotation or xml

      spring is how to discover which classes need to be managed?

          Annotation packet is marked path scanning +

      spring is how to achieve Dependency Injection?

         By adding the reflective markers AutoWrited annotation object instance

      How to manage the Bean spring?

         Use HashMap container, Set single-container embodiments Bean

      spring aop facets of how to achieve it?

         The use of dynamic proxy mode, and provides a default implementation cglib and jdk two ways

      spring is loaded into memory when it?

         springMvc is triggered by an inlet arranged web.xml, springboot triggered by initializing springApplication

 

 The realization spring core function involves what point?

      1. Definition and use annotations

      2. The container initialization

      3. read and use profiles

      4.sevlet use

      The definition and use of annotations

      6. The use of reflection

      7.url routing and mapping methods

      8. Parameter Analysis and bound

      9. regular and escaped

 

 

 Officially begin

  First create a maven project, the code is structured as follows:

 

Add jar package dependencies:

There are two jar package plus a plug

javax.servlet-api.jar core code and to enable the processing request
fastjson.jar json parameters used for binding 

jetty-maven-plugin used as the web container jetty start


Maven complete code is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.honstat</groupId>
    <artifactId>test-spring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId></fastjsonartifactId>
            <version>1.2.56</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.5.v20170502</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                    <httpConnector>
                        <port>8080</port>
                    </httpConnector>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
View Code

 

 Creating a front controller class

  Front Controller servlet request special treatment, matched to the corresponding method after the implementation of the return, the front controller is what I can refer to another blog "SpringMvc request processing flow source Quest"

   Here we create a class called CJDispatcherServlet, it inherited HttpServlet class and override the init HttpServlet of (), doGet (), doPost () these three methods, the figure HomeService first ignored and can not write StudentService

 

 Configuring web.xml

 Configure <servlet> and <servlet-mapping> 2 labels

<Servlet> requires

1. Specify the name of the servlet

 2. The front end controller class designated processing request

 3. Set the initial configuration file path

 

 Complete web.xml file as follows:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

         version="2.5">
    <servlet>
        <servlet-name>cjservletMVC</servlet-name>
        <servlet-class>com.honstat.spring.service.CJDispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>spring/application.properties</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cjservletMVC</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>
View Code

 

 添加注解

 我们知道,spring里面是通过给类加注解来识别各种使用场景的,那我们就来实现几个必用的

作用在类上的:

  JCController

  JCService

  JCComponent

 

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

 全部采用这种类型

作用在方法上和类上的:

JCRequestMapping

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

 

作用在字段上的:

JCAutoWrited

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

 

到此我们已经初步的创建完了需要准备的类,准备工作告一段落。

 

 由于篇幅有限,下一篇开始实现核心功能了!

 完整代码地址

Guess you like

Origin www.cnblogs.com/jingch/p/11368982.html