纯注解使用servlet并整合spring



    4.0.0

    com.siyue
    servlet
    1.0-SNAPSHOT
    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        
        
        
            com.google.code.gson
            gson
            2.8.6
        


    
    
        8
        8
        5.3.5
    


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




你可能感兴趣的:(纯注解使用servlet并整合spring)