zoukankan      html  css  js  c++  java
  • SpringBoot-Web配置

    重写全局配置

    如果springboot提供的springmvc配置不符合要求,则可以通过一个配置类(标有@Configuration注解的类)加上@EnableWebMvc注解来实现完全自己控制的mvc配置

    当你既想保留springboot提供的配置,又想增加自己额外的配置时,可以定义一个配置类并继承WebMvcConfigurationAdapter,无须使用@EnableWebMvc注解

    web容器配置(servlet容器配

    如果需要使用代码的方式配置servlet容器,则可以注册一个实现EmbeddedServletContainerCustomizer接口的bean,若想直接配置Tomcat、Jetty、Undertow则可以直接定义TomcatEmbededServletContainerFactory、Jetty....、Undertow...

    springboot默认是以tomcat作为servlet容器,修改为其他web容器

    <dependency>
          <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
              <exclusions>
                   <exclusion>
                       <groupId>org.springframework.boot</groupId>
                       <artifactId>spring-boot-starter-tomcat</artifactId>
                   </exclusion>
              </exclusions>
              <version>1.3.7.RELEASE</version>
    </dependency>
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

    Servlet配置

    @Bean
    public ServletRegistrationBean camelServletRegistrationBean() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(), "/hsm/*");
        registration.setName("CamelServlet");
        return registration;
    }

    Filter配置

    @Bean
    public FilterRegistrationBean requestParamFilter() {
         FilterRegistrationBean filterReg = new FilterRegistrationBean();
         filterReg.setFilter(new RequestParamFilter());
         filterReg.setUrlPatterns(Collections.singletonList("/service/*"));
         filterReg.setName("requestParamFilter");
         return filterReg;
    }

    Listener配置

    listener类需要实现ServletContextListener接口,并标注@Component注解。想增加一些自定义的配置,可以如下

    @Bean
    public ServletListenerRegistrationBean socketListener(){
        ServletListenerRegistrationBean<EventListener> listenerRegistrationBean = new ServletListenerRegistrationBean();
        listenerRegistrationBean.setListener(new VideoUploadLinstener());
        return listenerRegistrationBean;
    }

    初始化参数配置

    @Bean
    public InitParameterConfiguringServletContextInitializer initParamsInitializer(Environment env) {
        Map<String, String> contextParams = new HashMap<>();
        contextParams.put("videoUploadServerPort", env.getProperty("videoUploadServerPort"));
        return new InitParameterConfiguringServletContextInitializer(contextParams);
    }

    初始化参数就相当于之前我们配置spring listener、servlet的initParam。可以通过  javax.servlet.ServletContext.getInitParameter(key) 来取值。这种方式是全局化的添加web初始化参数,通过ServletContext取值。

    加载自定义yml文件 

    springboot在容器启动默认会去加载jar包同级config目录、jar包同级目录、classpath config目录,classpath目录的application.yml(或者.properties)文件,使用 spring.profiles.active=dev 可以激活application-dev.yml文件,支持多个。此外使用 YamlPropertiesFactoryBean 类可以自定义加载。

    YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
    yamlPropertiesFactoryBean.setResources(new ClassPathResource("ftp.yml"));
    Properties properties = yamlPropertiesFactoryBean.getObject();
    isSftp = Boolean.valueOf(properties.getProperty("ftp.sftp"));
    url = properties.getProperty("ftp.url");
    port = Integer.parseInt(properties.getProperty("ftp.port"));
    username = properties.getProperty("ftp.username");
    password = properties.getProperty("ftp.password");
    localPath = properties.getProperty("ftp.localPath");
    remotePath = properties.getProperty("ftp.remotePath");
    心里有束光,眼里有片海
  • 相关阅读:
    php练习4——排序,查找
    php练习3——猜拳游戏,评委打分问题
    php练习2——乘法表,变量的使用
    php练习1——计算器
    php函数的初步使用
    php练习——打印半金字塔、金字塔、空心金字塔、菱形、空心菱形
    Discuz论坛下载与安装
    phpMyAdmin下载与安装
    mysql5.7下载与安装,php5.6与mysql5.7整合
    php5下载,apache2.4与php5整合
  • 原文地址:https://www.cnblogs.com/xhy-shine/p/8685914.html
Copyright © 2011-2022 走看看