zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    机器学习的几个知识点记录(转)
    mysql缓存、存储引擎
    特性选择、特征抽取阅读记录
    聚类、降维、文本处理阅读记录
    标准化、正则化阅读记录
    广播变量、累加器、collect
    实现Map-side Join和Reduce-side Join(转)
    MySQL数据库迁移(转)
    “数据迁移”考虑
    QT的信号和槽
  • 原文地址:https://www.cnblogs.com/heroinss/p/10510990.html
Copyright © 2011-2022 走看看