whycxzp
2025-03-06 dfd45b0998e4b9279e8ed8a33fbf82fc4e8b6015
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.whyc;
 
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
/**
 * @Description : 启动类
 * @date 2020/10/15
 **/
@SpringBootApplication
@EnableWebMvc
@ServletComponentScan(basePackages = {"com.whyc.filter","com.whyc.servlet","com.whyc.listener"})
@EnableCaching
public class App extends WebMvcConfigurerAdapter  implements WebMvcConfigurer {
 
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
 
    @Bean
    //配置内的system.subType=2时,端口为18919
    @ConditionalOnProperty(name = "system.subType", havingValue = "2")
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
        return factory -> factory.setPort(18919);
    }
 
    @Value("${http.port}")
    private Integer httpPort;
 
    @Bean
    @ConditionalOnProperty(prefix = "spring.profiles",name = "active",havingValue = "prod-ssl")
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        // 添加http
        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
        return tomcat;
    }
 
    /**
     * 配置http
     *
     * @return connector
     */
    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpPort);
        return connector;
    }
}