重寫全局配置
如果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");