在上篇文章中咱们了解到Spring Boot 的一些经常使用的外部化配置,在本篇中咱们将会继续对类的配置进行了解css
在 Spring Boot 中的约定大于配置 与 自动装配使咱们能够没必要去像以往同样配置各个框架之间的依赖与注入,可是有时Spring Boot 提供给咱们的默认配置并不能彻底知足咱们的需求,所以个性化的配置或者叫自定义装配内容便为咱们提供了这种便利。
这里咱们以一个简单的在Spring Boot 中自定义错误页面的例子作为展现:web
@Configuration public class ErrorPageConfig { @Bean public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){ return new MyCustomizer(); } private static class MyCustomizer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403")); container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404")); } } }
EmbeddedServletContainerCustomizer 正如名字同样 - 嵌入式Servlet容器定制器,咱们经过对其方法的覆盖从新达到了自定义错误页面的效果。
固然 若是使用的是Spring MVC 的话咱们也可使用 Spring MVC 提供的@ExceptionHandler方法和@ControllerAdvice。ErrorController进行异常的捕捉与错误信息页面的定制。 spring
备注:
@Configuration:表示这个类可使用 Spring IoC 容器做为 bean 定义的来源。
@Bean :告诉 Spring,这是一个bean对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。mongodb
这里以Spring Security 做为演示:shell
@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomAuthenticationProvider customAuthenticationProvider; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(customAuthenticationProvider); } @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/reg", "/login", "/css/**", "/js/**", "/img/**", "/music/**", "/plugins/**", "/upload/**", "/api/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/") .permitAll() .and() .logout() .permitAll(); // 默认状况下,CSRF保护已启用。你必须配置包含_csrf令牌的全部的网页来工做。 // 你能够随时禁用CSRF保护。若是在代码中配置: 解决post请没法提交 http .csrf().disable(); //in a frame because it set 'X-Frame-Options' to 'DENY'. http .headers() .frameOptions() .sameOrigin(); } }
在通常状况下咱们要对Spring Boot 集成的一些框架作些自定义配置时, 能够去实现 对应框架的XXXConfigurerAdapter,而后经过EnableXXX注解进行修饰 以后用 Configuration 注解修饰,最后对其方法进行覆盖从而到达自定义配置的目的,固然具体的配置仍是需求去翻阅对应的文档说明来了解。数据库
Starter POMs是能够包含到应用中的一个方便的依赖关系描述符集合。你能够获取全部Spring及相关技术的一站式服务,而不须要翻阅示例代码,拷贝粘贴大量的依赖描述符。例如,若是你想使用Spring和JPA进行数据库访问,只须要在你的项目中包含spring-boot-starter-data-jpa依赖,而后你就能够开始了。编程
该starters包含不少你搭建项目,快速运行所需的依赖,并提供一致的,管理的传递依赖集。api
名字有什么含义:全部的starters遵循一个类似的命名模式:spring-boot-starter-,在这里是一种特殊类型的应用程序。该命名结构旨在帮你找到须要的starter。不少IDEs集成的Maven容许你经过名称搜索依赖。例如,使用相应的Eclipse或STS插件,你能够简单地在POM编辑器中点击ctrl-space,而后输入"spring-boot-starter"能够获取一个完整列表。spring-mvc
下面的应用程序starters是Spring Boot在org.springframework.boot组下提供的:tomcat
spring-boot-starter 核心 POM,包含自动配置支持、日志库和对 YAML 配置文件的支持。
spring-boot-starter-amqp 经过 spring-rabbit 支持 AMQP。
spring-boot-starter-aop 包含 spring-aop 和 AspectJ 来支持面向切面编程(AOP)。
spring-boot-starter-batch 支持 Spring Batch,包含 HSQLDB。
spring-boot-starter-data-jpa 包含 spring-data-jpa、spring-orm 和 Hibernate 来支持 JPA。
spring-boot-starter-data-mongodb 包含 spring-data-mongodb 来支持 MongoDB。
spring-boot-starter-data-rest 经过 spring-data-rest-webmvc 支持以 REST 方式暴露 Spring Data 仓库。
spring-boot-starter-jdbc 支持使用 JDBC 访问数据库。
spring-boot-starter-security 包含 spring-security。
spring-boot-starter-test 包含经常使用的测试所需的依赖,如 JUnit、Hamcrest、Mockito 和 spring-test 等。
spring-boot-starter-velocity 支持使用 Velocity 做为模板引擎。
spring-boot-starter-web 支持 Web 应用开发,包含 Tomcat 和 spring-mvc。
spring-boot-starter-websocket 支持使用 Tomcat 开发 WebSocket 应用。
spring-boot-starter-ws 支持 Spring Web Services。
spring-boot-starter-actuator 添加适用于生产环境的功能,如性能指标和监测等功能。
spring-boot-starter-remote-shell 添加远程 SSH 支持。
spring-boot-starter-jetty 使用 Jetty 而不是默认的 Tomcat 做为应用服务器。
spring-boot-starter-log4j 添加 Log4j 的支持。
spring-boot-starter-logging 使用 Spring Boot 默认的日志框架 Logback。
spring-boot-starter-tomcat 使用 Spring Boot 默认的 Tomcat 做为应用服务器。
关于Spring Boot 经常使用的一些配置方式作了一个简要的介绍,从这些配置中咱们也能够看到Spring Boot 为咱们提供的各类默认配置的好处,当咱们不须要这些默认值时,咱们有两种经常使用的覆盖方式:1.经过 application.yml文件进行定义。2.经过实现对应的ConfigurerAdapter,并托管给Spring 容器来进行定义。