Spring boot

    简述

        spring boot使用“习惯优于配置”的理念,可以让项目快速的运行;

        使用spring boot很容易创建一个独立运行(内嵌servlet容器)基于spring框架的项目,开发者可以不用或者只使用很少的spring配置。

    父级依赖  

                

                    org.springframework.boot        

                    spring-boot-starter-parent        

                    1.5.1.RELEASE    

                    

            

           有了spring-boot-starter-parent,就是一个spring boot项目,而且定义了一些jar包的version,可以省去version标签

    起步依赖Spring-boot-starter-xx

            如我们之前要实现web功能,引入了spring-boot-starter-web这个起步依赖,定义了对其他库的传递依赖,这些东西加在一起即支持某项功能,它自动组装了spring-web,spring-webmvc等jar包

    基本配置

        入口类:spring boot通常有一个名为*Application的入口类,入口类中有一个main方法。

        @SpringBootApplication是spring boot的核心注解,它是一个组合注解:

            @Configuration     ——    声明当前类位配置类(xml)

            @EnableAutoConfiguration    —— 让 Spring Boot 根据类路径中的 jar 包依赖为当前项目进行自动配置,

                    例如添加了 spring-boot-starter-web 依赖,会自动添加 Tomcat 和 Spring MVC 的依赖,那么 Spring Boot 会对 Tomcat 和 Spring MVC 进行自动配置。

            @ComponentScan    —— 自动扫描包,扫描该层下所有声明bean的注解类,并且向spring容器中注册为bean;一般为@ComponentSatn("包路径")。

            

    外部配置

        spring boot允许使用properties文件,命令行等作为外部配置。

        命令行:如spring boot是可以基于jar包运行的,

        java -jar xx.jar --server.port=9090

    常规属性配置:

        application.properties文件中:

        book.authour=sun

        book.name=spring boot

    入口类:

            @RestController    //等价于@Controller+@ResponseBody的结合,使用这个注解的类里面的方法都以json格式输出,并且是将函数的返回值直 接填入HTTP响应体中

         @SpringBootApplication

            public class DemoApplication {

            

                            @Value("${book.name}")

                        private String bookname;

            

                        @RequestMapping("/")

                        String index(){

                            return "Hello Spring boot,bookname:"+bookname;

                        }

                        

                        public static void main(String[] args) {

                        

                            SpringApplication.run(DemoApplication.class, args);

                        }

            

           }

            spring boot(一)基本配置与SSL_第1张图片

    类型安全的配置:

        为了简化每个属性都由@value对应,通过@ConfigurationProperties将properties属性和一个bean及其属性关联

            @Component

            @ConfigurationProperties(prefix = "book")    //使用的配置参数的前缀

            @PropertySource("classpath:book.properties")    //配置文件的位置

            public class Book {

            

                private String name;

            

                public String getName() {

                    return name;

                }

            

                public void setName(String name) {

                    this.name = name;

                }

            }

            @RestController

            @SpringBootApplication

            public class DemoApplication {

            

                @Autowired

                private Book book;

            

                @RequestMapping("/")

                String index(){

                    return "Hello Spring boot,bookname:"+book.getName();

                }

            

                public static void main(String[] args) {

            

                    SpringApplication.run(DemoApplication.class, args);

                }

            }

                        spring boot(一)基本配置与SSL_第2张图片

    profile配置

        profile是spring boot用来针对不同的环境对不同的配置提供支持的,全局profile配置使用application-{profile}.properties,通过在application.properties中设置spring.profile.active=xx来制定活动的profile。


    spring boot注册Servlet,Filter,Listener


    传统的java ee是在web.xml中配置filter等信息,而spring boot支持两种方式:代码注册;注解注册。

        

                1.代码注册

                                            创建自定义的filter

                                            public class TestFilter implements Filter {

                                              @Override

                                              public void init(FilterConfig filterConfig) throws ServletException {

                                              }

                                             @Override

                                              public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)

                                                  throws IOException, ServletException {

                                                System.out.println("TestFilter");

                                              }

                                               @Override

                                              public void destroy() {

                                              }

                                            }

                                            配置类中注册过滤器:

                                            @Configuration        //@Configuration:标注当前类是配置类,并会将当前类内声明的一个或多个bean加入到spring容器中

                 public class WebConfig {

                                            @Bean        //相当于XML中的,放在方法的上面,而不是类,意思是产生一个bean,并交给spring管理。

                                                 public FilterRegistrationBean getDemoFilter(){

                                                 TestFilter demoFilter = new TestFilter();

                                                     FilterRegistrationBean registrationBean=new FilterRegistrationBean();

                                                    registrationBean.setFilter(demoFilter);

                                                     List urlPatterns=new ArrayList();

                                                     urlPatterns.add("/*");                            //拦截路径,可以添加多个

                                                     registrationBean.setUrlPatterns(urlPatterns);

                                                     registrationBean.setOrder(1);

                                                     return registrationBean;

                                                 }

                                            }


                2.注解注册

                                            创建自定义的filter,加@WebFilter注解

                                            @Order(1)

                                            @WebFilter(filterName = "testFilter2”, urlPatterns = "/*")

                                            public class TestFilter2 implements Filter {

                                              @Override

                                              public void init(FilterConfig filterConfig) throws ServletException {

                                             

                                              }

                                              @Override

                                              public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)

                                                  throws IOException, ServletException {

                                                filterChain.doFilter(servletRequest,servletResponse);

                                              }

                                              @Override

                                            

                                              public void destroy() {

                                              }

                                            }

                在工程入口加上注解@ServletComponentScan,实现自动的扫描注册(带有WebFilter,WebServlet,WebListener注解的类),不用在代码中写registrationBean.setFilter

                                       @SpringBootApplication

                                        @ServletComponentScan

                                        public class SpringBootSimpleApplication {

                                                      public static void main(String[] args) {

                                                        SpringApplication.run(SpringBootSimpleApplication.class, args);

                                                      }

                }


Spring boot SSL配置

    SSL(security sockets layer安全套接层)位于TCP/IP协议和各种应用层协议(如HTTP)之间,为数据通信提供安全支持。采用了SSL之后,HTTP就有了HTTPS的加密,证书和完整性保护这些功能。

            1.使用SSL首先需要生成证书,可以通过jdk里的证书管理工具keytool生成自签名的证书;

            2.配置SSL:spring boot允许在application.properties中配置http或https,但是只能配置一个,另外一个需要以编程的方式配置。https比较复杂,所以在配置文件中配置https信息:

             #https端口号.  

       server.port: 443  

        #证书路径.  

        server.ssl.key-store: classpath:keystore.p12  

        #证书密码

        server.ssl.key-store-password: 123456  

        #秘钥库类型  

        server.ssl.keyStoreType: JKS  

        #证书别名  

        server.ssl.keyAlias: tomcat 

    3.此时,可以在页面访问https的网页,但是访问不到http的网页,还需要实现http到https的重定向

    4.代码中配置http信息,并添加tomcat的connector,实现自动跳转

                     @Bean  

                public EmbeddedServletContainerFactory servletContainer() {  

                  TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {  

               

                       @Override  

                       protected void postProcessContext(Context context) {  

                                  SecurityConstraint securityConstraint = new SecurityConstraint();  

                                  securityConstraint.setUserConstraint("CONFIDENTIAL");  

                                  SecurityCollection collection = new SecurityCollection();  

                                  collection.addPattern("/*");  

                                  securityConstraint.addCollection(collection);  

                                  context.addConstraint(securityConstraint);  

                       }  

                   };  

                   //添加连接配置,http的配置信息.  

                   tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());  

                   return tomcat;  

                }  


                /**  

                 * 配置http连接信息. 

                 */  

                private Connector initiateHttpConnector() {  

                   Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");  

                   connector.setScheme("http");  

                   connector.setPort(8080);  

                   connector.setSecure(false);  

                   connector.setRedirectPort(443);  

                   return connector;  

                }  

            5.此时访问

        http://localhost:8080自动转到https://localhost:443


关于@Configuration注解、@Bean注解以及配置自动扫描

转自https://blog.csdn.net/javaloveiphone/article/details/52182899

    spring boot(一)基本配置与SSL_第3张图片

      spring boot(一)基本配置与SSL_第4张图片

        spring boot(一)基本配置与SSL_第5张图片

          spring boot(一)基本配置与SSL_第6张图片

            spring boot(一)基本配置与SSL_第7张图片