美文网首页
纯注解使用servlet并整合spring

纯注解使用servlet并整合spring

作者: 四月的谎言v5 | 来源:发表于2021-04-20 09:49 被阅读0次
    <?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.siyue</groupId>
        <artifactId>servlet</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
                <scope>provided</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.6</version>
            </dependency>
    
    
        </dependencies>
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
            <spring.version>5.3.5</spring.version>
        </properties>
    
    </project>
    

    src/main/java/com/siyue/servlet/vo/UserVo.java

    package com.siyue.servlet.vo;
    
    public class UserVo {
        private Integer id;
        private String username;
        private String password;
    
        public UserVo(Integer id, String username, String password) {
            this.id = id;
            this.username = username;
            this.password = password;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    src/main/java/com/siyue/servlet/service/UserService.java

    package com.siyue.servlet.service;
    
    import com.siyue.servlet.vo.UserVo;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService {
    
        public UserVo getUserInfo() {
            return new UserVo(10086, "四月", "123456");
        }
    }
    

    src/main/java/com/siyue/servlet/config/SpringConfig.java

    package com.siyue.servlet.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan(basePackages = {"com.siyue.servlet"})
    public class SpringConfig {
    }
    

    src/main/java/com/siyue/servlet/init/BaseServlet.java

    package com.siyue.servlet.init;
    
    import com.google.gson.Gson;
    import com.siyue.servlet.service.UserService;
    import com.siyue.servlet.vo.UserVo;
    import org.springframework.web.context.WebApplicationContext;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class BaseServlet extends HttpServlet {
        private final  WebApplicationContext ctx;
    
        public BaseServlet(WebApplicationContext ctx) {
            this.ctx = ctx;
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            UserService userService = ctx.getBean(UserService.class);
            UserVo userVo = userService.getUserInfo();
            Gson gson = new Gson();
            String json = gson.toJson(userVo);
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().print(json);
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            doPost(req, resp);
        }
    }
    

    src/main/java/com/siyue/servlet/init/MyWebApplicationInitializer.java

    package com.siyue.servlet.init;
    
    import com.siyue.servlet.config.SpringConfig;
    import org.springframework.web.WebApplicationInitializer;
    import org.springframework.web.context.ContextLoaderListener;
    import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletRegistration;
    
    public class MyWebApplicationInitializer implements WebApplicationInitializer {
        @Override
        public void onStartup(ServletContext container) {
            AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
            ac.register(SpringConfig.class);
            container.addListener(new ContextLoaderListener(ac));
    
            ServletRegistration.Dynamic baseServlet = container.addServlet("baseServlet", new BaseServlet(ac));
            // 添加映射文件路径
            baseServlet.addMapping("/");
            // 添加启动时机
            baseServlet.setLoadOnStartup(1);
        }
    }
    

    src/main/webapp/WEB-INF/web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    </web-app>
    

    相关文章

      网友评论

          本文标题:纯注解使用servlet并整合spring

          本文链接:https://www.haomeiwen.com/subject/hwbglltx.html