• springboot之零碎小知识


    1.springboot启动类加载yml配置项

    主要是如下方法,读取了yml的配置项,赋值为类成员属性

    @Autowired
        public void setEnvironment(Environment environment) {
            host = environment.getProperty("server.port");
        }
    package com.shy;
    
    import com.shy.iot.facerecognition2.tcp.Server;
    import com.shy.iot.netty.server.NettyServer;
    import org.apache.catalina.Context;
    import org.apache.catalina.connector.Connector;
    import org.apache.tomcat.util.descriptor.web.SecurityCollection;
    import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.core.env.Environment;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    
    @SpringBootApplication
    @EnableCaching
    @EnableWebMvc
    @MapperScan("com.shy.iot.*.dao")
    public class CentralPlatformApp extends WebMvcConfigurerAdapter {
    
        String host;
    
        public static void main(String[] args) throws InterruptedException {
            ConfigurableApplicationContext ctx = SpringApplication.run(CentralPlatformApp.class, args);
            String flag = ctx.getEnvironment().getProperty("netty.isStart");
            if ("start".equals(flag)) {//判断当前项目启动是否要启动智能家居模块
                NettyServer nettyServer = (NettyServer) ctx.getBean("nettyServer");
                nettyServer.start();
            }
            Server bean = ctx.getBean(Server.class);
            bean.run();
        }
    
        @Autowired
        public void setEnvironment(Environment environment) {
            host = environment.getProperty("server.port");
        }
    
        /**
         * it's for set http url auto change to https
         */
        @Bean
        public TomcatEmbeddedServletContainerFactory servletContainer() {
            TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
                @Override
                protected void postProcessContext(Context context) {
                    SecurityConstraint securityConstraint = new SecurityConstraint();
                    securityConstraint.setUserConstraint("CONFIDENTIAL");//confidential
                    SecurityCollection collection = new SecurityCollection();
                    collection.addPattern("/*");
                    securityConstraint.addCollection(collection);
                    context.addConstraint(securityConstraint);
                }
            };
            tomcat.addAdditionalTomcatConnectors(httpConnector());
            return tomcat;
        }
    
        @Bean
        public Connector httpConnector() {
            Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
            connector.setScheme("http");
            connector.setPort(9000);
            connector.setSecure(true);
            connector.setRedirectPort(Integer.parseInt(host));
            return connector;
        }
    }
  • 相关阅读:
    Java中类的继承
    信号量、PV原语及其应用
    Python-subprocess执行命令并将输出劫持实现实时记录到日志
    Python-logging模块定制格式描述符实现定长日志等级
    Python-logging模块实现同时向控制台和文件打印日志
    SpringBoot学习笔记(二)
    Kubernetes学习日记(四)
    Kubernetes学习日记(三)
    SpringBoot学习笔记(一)
    Kubernetes学习日记(二)
  • 原文地址:https://www.cnblogs.com/heroinss/p/10510990.html
Copyright © 2020-2023  润新知