码迷,mamicode.com
首页 > 编程语言 > 详细

springboot

时间:2019-02-25 13:24:16      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:cti   alt   demo   code   不用   out   throw   pattern   gap   

@SpringBootApplication
public class demotest {

    public static void main(String[] args) {
        SpringApplication.run(demotest.class ,args);
    }
}

springboot 的启动类

@Controller
public class controllertest {
    @RequestMapping("/hello")
    @ResponseBody
    public Map<String,Object>hello(){
        Map<String,Object>map=new HashMap<String, Object>();
        map.put("msg","hello");
        return  map;
    }
}

对应的conroller

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

pom文件配置

这里应注意启动类应该在conroller同一个包下,或者在启动器上一层包里,还有一个就是这里如果直接将配置文件中的版本号删掉不能用。需要加上版本号。

 

springboot 和servlet的整合

在启动类上添加@ServletComponentScan,启动扫描servlet

@SpringBootApplication
@ServletComponentScan
public class app {
    public static void main(String[] args) {
        SpringApplication.run(app.class,args);
    }
}

编写servlet类,并配置到spring中

@WebServlet(name = "nihao",urlPatterns = "/first")
public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("shdihfishdoifhoiasdoif");
    }
}

另一种springboot和servlet整合的方式(不需要只要编写servlet就行,不用@ServletComponentScan,而且servlet类上面也不需要多余的注解@WebServlet)

    @Bean                                     //相当于@ServletComponentScan,上一种是自动扫描,这种是手动添加
    public ServletRegistrationBean register(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
public class SecondServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("second servlet...");
    }
}

springboot整合filter

@SpringBootApplication
@ServletComponentScan
public class app {
    public static void main(String[] args) {
        SpringApplication.run(app.class,args);
    }
}
//@WebFilter(filterName = "Firstfilter",urlPatterns = {"*.do","*.action"})     这种可以添加多种拦截规则
@WebFilter(filterName = "first",urlPatterns = "/first")
public class Firstfilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("ENTER");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("leave");
    }
    public void destroy() {
    }
}

整合filter方式二

public class app {
    public static void main(String[] args) {
        SpringApplication.run(app.class,args);
    }
    @Bean
    public ServletRegistrationBean register(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
    @Bean
    public FilterRegistrationBean getfilter(){
        FilterRegistrationBean filterRegistration=new FilterRegistrationBean(new Firstfilter());
        filterRegistration.addUrlPatterns("/second");
        return filterRegistration;
    }
}
public class Firstfilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("ENTER");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("leave");
    }
    public void destroy() {
    }
}

springboot整合listener

@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
@WebListener
public class Firstlistener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("创建了监听器");
    }
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}

第二种方式通过函数注册

@SpringBootApplication
@ServletComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }

    @Bean
    public ServletListenerRegistrationBean <Firstlistener> register(){
        ServletListenerRegistrationBean <Firstlistener>bean =new ServletListenerRegistrationBean<Firstlistener>(new Firstlistener());
        return bean;
    }
}

这种比上面的几种多了泛型,listener的编写和上面的一样,就是少了@WebListenner注解

springboot对静态资源的访问

1必须在classpath/static文件夹下面,就是在resources目录下创建static目录

技术图片

 

 2或者在src/main/webapp目录下面,同样文件名字不能变

 

springboot

标签:cti   alt   demo   code   不用   out   throw   pattern   gap   

原文地址:https://www.cnblogs.com/feixiangdecainiao/p/10430249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!