SpringBoot 学习笔记(一)

1.springboot内嵌tomcat,但是不支持jsp,如果需要,就手动导入jsp,以下是相关配置


<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
</dependency>

2.springboot支持自动配置,可以手动关闭某一项的自动配置

例:@SpringBootApplication(exclude={RedisAutoConfiguration.class})

3.Springboot的配置文件application.properties或者 application.yml,

4.springboot配置springmvc拦截器

    a .在springboot主入口程序包下新建一个类继承WebMvcConfigurerAdapter,

    b.类上面加注解@Configuration

     c.重新写HandlerInerceptor

import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration //申明这是一个配置
public class MySrpingMVCConfig extends WebMvcConfigurerAdapter{

    // 自定义拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                    throws Exception {
                System.out.println("自定义拦截器............");
                return true;
            }
            
            @Override
            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                    ModelAndView modelAndView) throws Exception {
                
            }
            
            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                    Exception ex) throws Exception {
            }
        };
        registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
    }

   

}

运行流程:Springboot主入口程序的 @ComponentScan  注解会扫描主入口程序包下当前包及子包下所有类。

                   扫描到我们定义的类 后有注解configuration 就会继续扫描,运行拦截器

5.SpringBoot项目发布到独立的Tomcat中运行

  a.工程的打包方式 为war包

  b.将Spring-boot-starter-tomcat的范围设置为provided。

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
</dependency>

 c.修改代码,设置启动配置

    需要继承SpringBootServletInitializer,然后重写configure,将SpringBoot的入口类设置进去

  d.打包。

猜你喜欢

转载自blog.csdn.net/qq_16008855/article/details/84966465