JavaEE-自定义SSM-编写核心

3.1 解决编写错误
JavaEE-自定义SSM-编写核心_第1张图片

3.1.1 注解

1)MySpringBootApplication
package com.czxy.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.TYPE)               //只能修饰类
@Retention(RetentionPolicy.RUNTIME)     //策略 运行时(内存)
public @interface MySpringBootApplication {
}

2)MyMapper
package com.czxy.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.TYPE)               //只能修饰类
@Retention(RetentionPolicy.RUNTIME)     //策略 运行时(内存)
public @interface MyMapper {
}

3)MySelect
package com.czxy.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.METHOD)              //只能修饰方法
@Retention(RetentionPolicy.RUNTIME)     //策略 运行时(内存)
public @interface MySelect {
    public String value() ;             //注解参数
}

4)MyService
package com.czxy.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.TYPE)               //只能修饰类
@Retention(RetentionPolicy.RUNTIME)     //策略 运行时(内存)
public @interface MyService {
}

5)MyRestController
package com.czxy.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.TYPE)               //只能修饰类
@Retention(RetentionPolicy.RUNTIME)     //策略 运行时(内存)
public @interface MyRestController {
}

6)MyRequestMapping
package com.czxy.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.TYPE)               //只能修饰类
@Retention(RetentionPolicy.RUNTIME)     //策略 运行时(内存)
public @interface MyRequestMapping {

    public String value();              //参数
}

7)MyGetMapping
package com.czxy.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.METHOD)                     //只能修饰方法
@Retention(RetentionPolicy.RUNTIME)             //策略 运行时(内存)
public @interface MyGetMapping {
//    public String value() default "" ;       //注解参数
}

8)MyPostMapping
package com.czxy.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.METHOD)                     //只能修饰方法
@Retention(RetentionPolicy.RUNTIME)             //策略 运行时(内存)
public @interface MyPostMapping {
//    public String value() default "" ;       //注解参数
}

3.1.2 spring boot核心类

package com.czxy.boot;

import com.czxy.annotation.MySpringBootApplication;


public class MySpringApplication {
    /**
     * 整个程序的入口
     * @param appClass
     * @param args
     */
    public static void run(Class appClass, String[] args) {
        // 1)校验启动类是否有 @MySpringBootApplication注解
        boolean hasBootApp = appClass.isAnnotationPresent(MySpringBootApplication.class);
        if(!hasBootApp) {
            throw new RuntimeException(appClass + "没有添加@MySpringBootApplication注解");
        }

        System.out.println("正常运行");
    }
}

3.2 编写tomcat

3.2.1 测试
JavaEE-自定义SSM-编写核心_第2张图片

  • 测试tomcat

    package com.czxy;
    
    import org.apache.catalina.Context;
    import org.apache.catalina.LifecycleException;
    import org.apache.catalina.Wrapper;
    import org.apache.catalina.connector.Connector;
    import org.apache.catalina.core.StandardContext;
    import org.apache.catalina.loader.ParallelWebappClassLoader;
    import org.apache.catalina.loader.WebappLoader;
    import org.apache.catalina.startup.Tomcat;
    import org.apache.juli.logging.Log;
    import org.apache.juli.logging.LogFactory;
    
    import java.io.File;
    
    
    public class TestTomcat {
    
        public static void main(String[] args) throws LifecycleException {
    
    //        https://blog.csdn.net/weixin_44061521/article/details/120262084
    
            // 1. 创建tomcat
            Tomcat tomcat = new Tomcat();
    
            // 2. 设置端口号
            tomcat.setPort(8080);
    
            // 3. 设置tomcat的工作目录
            tomcat.setBaseDir(new File("E:/workspaces_2021_quan12_5/mytomcat").getAbsolutePath());
    
            // 4. 确定http请求使用的协议
            Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
            connector.setThrowOnFailure(true);
            connector.setPort(8080);
    
            // 5. 使用的协议 与 tomcat 绑定
            tomcat.getService().addConnector(connector);
            tomcat.setConnector(connector);
            tomcat.getHost().setAutoDeploy(false);
    
            // 6. 设置上下文 (固定)
            StandardContext context = new StandardContext();
            context.setName("");
            context.setDisplayName("application");
            context.setPath("");
            context.setDocBase("/");
            context.addLifecycleListener(new Tomcat.FixContextListener());
            context.setParentClassLoader(StandardContext.class.getClassLoader());
            context.setCreateUploadTargets(true);
    
            // 7. web应用程序加载器(固定)
            WebappLoader loader = new WebappLoader();
            loader.setLoaderClass(ParallelWebappClassLoader.class.getName());
            loader.setDelegate(true);
            context.setLoader(loader);
    
            // 8. 设置默认Servlet
            addDefaultServlet(context);
    
            // 9. 将上下文对象设置给tomcat
            tomcat.getHost().addChild(context);
    
    //        tomcat.addServlet("/", "dispatcherServlet", new MyDispacherServlet());
            // 10. 启动tomcat
            tomcat.start();
        }
    
    
        private static void addDefaultServlet(Context context) {
            Wrapper defaultServlet = context.createWrapper();
            defaultServlet.setName("default");
    //        defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
            defaultServlet.setServletClass("com.czxy.servlet.MyDispacherServlet");
            defaultServlet.addInitParameter("debug", "0");
            defaultServlet.addInitParameter("listings", "false");
            defaultServlet.setLoadOnStartup(1);
            // Otherwise the default location of a Spring DispatcherServlet cannot be set
            defaultServlet.setOverridable(true);
            context.addChild(defaultServlet);
            context.addServletMappingDecoded("/", "default");
        }
    }
    
    
  • 自定义前端控制器

    package com.czxy.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    
    public class MyDispatcherServlet extends HttpServlet {
    
        @Override
        public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("哈哈,我执行了!");
    
            //返回数据
            response.getWriter().print("6666");
        }
    }
    
    
  • 创建文件件
    JavaEE-自定义SSM-编写核心_第3张图片
    浏览器测试
    JavaEE-自定义SSM-编写核心_第4张图片
    3.2.2 自定义web服务
    JavaEE-自定义SSM-编写核心_第5张图片

  • 创建web服务器(再target目录下创建tomcat需要的目录结构 target/mytomcat/webapps

    • 注意:自定义web服务,需修改一个bug,端口号port从yml文件中读取。
    package com.czxy.server;
    
    import com.czxy.servlet.MyDispatcherServlet;
    import org.apache.catalina.Context;
    import org.apache.catalina.LifecycleException;
    import org.apache.catalina.Wrapper;
    import org.apache.catalina.connector.Connector;
    import org.apache.catalina.core.StandardContext;
    import org.apache.catalina.loader.ParallelWebappClassLoader;
    import org.apache.catalina.loader.WebappLoader;
    import org.apache.catalina.startup.Tomcat;
    
    import java.io.File;
    import java.io.IOException;
    
    
    public class MyWebServer {
        //tomcat路径
        private File tomcatDir;
    
        private void init() {
            try {
                //1 获得target目录
                String classPath = MyWebServer.class.getClassLoader().getResource("").getPath();
                File target = new File(classPath,"../");
    
                //2 tomcat目录
                tomcatDir = new File(target, "mytomcat");
                if(!tomcatDir.exists()) {
                    tomcatDir.mkdirs();
                }
    
                //3 web程序目录
                File webAppDir = new File(tomcatDir, "webapps");
                if(!webAppDir.exists()) {
                    webAppDir.mkdirs();
                }
    
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
    
        public void start() {
            // 初始化操作,准备tomcat目录
            init();
            //创建tomcat实例
            try {
                Tomcat tomcat = new Tomcat();
                //获得设置的端口号,如果没有,默认8080
                Integer port = 8080;
                tomcat.setPort(port);
    
                //设置工作目录
                tomcat.setBaseDir(tomcatDir.getAbsolutePath());
    
                Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
                connector.setThrowOnFailure(true);
                connector.setPort(port);
                tomcat.getService().addConnector(connector);
                tomcat.setConnector(connector);
                tomcat.getHost().setAutoDeploy(false);
    
                //设置上下文
                StandardContext context = new StandardContext();
                context.setName("");
                context.setDisplayName("application");
                context.setPath("");
                context.setDocBase("/");
                context.addLifecycleListener(new Tomcat.FixContextListener());
                context.setParentClassLoader(StandardContext.class.getClassLoader());
                context.setCreateUploadTargets(true);
    
                //web程序加载器
                WebappLoader loader = new WebappLoader();
                loader.setLoaderClass(ParallelWebappClassLoader.class.getName());
                loader.setDelegate(true);
                context.setLoader(loader);
    
                //设置默认,直接整合spring mvc(DispatcherServlet)
                addDefaultServlet(context);
                tomcat.getHost().addChild(context);
                tomcat.start();
            } catch (LifecycleException e) {
                throw new RuntimeException(e);
            }
        }
    
    
        private static void addDefaultServlet(Context context) {
            Wrapper defaultServlet = context.createWrapper();
            defaultServlet.setName("default");
    //        defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
            defaultServlet.setServletClass(MyDispatcherServlet.class.getName());
            defaultServlet.addInitParameter("debug", "0");
            defaultServlet.addInitParameter("listings", "false");
            defaultServlet.setLoadOnStartup(1);
            // Otherwise the default location of a Spring DispatcherServlet cannot be set
            defaultServlet.setOverridable(true);
            context.addChild(defaultServlet);
            context.addServletMappingDecoded("/", "default");
        }
    }
    
    
  • 调用web服务(启动tomcat)
    JavaEE-自定义SSM-编写核心_第6张图片

package com.czxy.boot;

import com.czxy.annotation.MySpringBootApplication;
import com.czxy.server.MyWebServer;


public class MySpringApplication {
    /**
     * 整个程序的入口
     * @param appClass
     * @param args
     */
    public static void run(Class appClass, String[] args) {
        // 1)校验启动类是否有 @MySpringBootApplication注解
        boolean hasBootApp = appClass.isAnnotationPresent(MySpringBootApplication.class);
        if(!hasBootApp) {
            throw new RuntimeException(appClass + "没有添加@MySpringBootApplication注解");
        }

        System.out.println("正常运行");
        // 3)启动web服务
        MyWebServer myWebServer = new MyWebServer();
        myWebServer.start();
    }
}

你可能感兴趣的:(java,java-ee,java,服务器)