spring boot约定优于配置的这种做法在如今越来越流行了
约定优于配置的这种做法在如今越来越流行了,它的特点是简单、快速、便捷。但是这是建立在程序员熟悉这些约定的前提上。而 Spring 拥有一个庞大的生态体系,刚开始转到 Spring Boot 完全舍弃 XML 时肯定是不习惯的,所以也会造成一些困扰。
运行方式
spring-boot-starter-web 包含了 Spring MVC 的相关依赖(包括 Json 支持的 Jackson 和数据校验的 Hibernate Validator)和一个内置的 Tomcat 容器,这使得在开发阶段可以直接通过 main方法或是 JAR 包独立运行一个 WEB 项目。而在部署阶段也可以打成 WAR 包放到生产环境运行。
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
在拥有 @SpringBootApplication 注解的类中,使用 SpringApplication 的 run 方法可以通过JAR启动项目。
继承 SpringBootServletInitializer 类并实现 configure 方法,使用 application 的 sources 方法可以通过WAR启动项目。
配置文件
Spring boot 的默认配置文件是 resources 下的 application.properties 和 application.yml。
配置文件
Spring boot 的默认配置文件是 resources 下的 application.properties 和 application.yml。
我曾在项目中遇到过 application.properties 出现中文乱码问题,当时尝试了很多办法都没有解决。Spring Boot 总是会以 iso-8859 的编码方式读取该文件,后来改用 YAML 了就再也没有出现过乱码了。并且它也拥有更简洁的语法,所以在此也更推荐使用 application.yml 作为默认的配置文件。
配置文件中可以定义一个叫做 spring.profiles.active 的属性,该属性可以根据运行环境自动读取不同的配置文件。例如将该属性定义为 dev 的话,Spring Boot 会额外从 application-dev.yml 文件中读取该环境的配置。
Spring Boot 注入配置文件属性的方法有两种,一种是通过 @Value 注解接受配置文件中的属性,另外一种是通过 @ConfigurationProperties 注解通过 set 方法自动为Bean注入对应的属性。
通过 @Value 注入属性,接收者既可以是方法参数,也可以是成员变量。例如配置文件为:
dataSource:
url: jdbc:mysql://127.0.0.1:3306/test
username: test
password: test
filters: stat,slf4j
redis:
host: 192.168.1.222
port: 6379
通过 @Value 接受方法参数初始化Bean:
@Bean
public JedisPool jedisPool(@Value("${redis.host}") String host,
@Value("${redis.port}") int port) {
return new JedisPool(host, port);
}
注入配置文件属性
注入配置文件属性
注入配置文件属性
通过 @ConfigurationProperties 读取配置初始化Bean,会直接调用对应的 set 方法注入:
@Bean(initMethod="init",destroyMethod="close")
@ConfigurationProperties(prefix="dataSource")
public DataSource dataSource() {
return new DruidDataSource();
}
Spring Boot 目前还无法直接注入的静态变量。我目前使用的方法是专门建立一个读取配置文件的Bean,然后使用 @PostConstruct 注解修饰的方法对这些静态属性进行初始化,例如:
@Configuration
public class ConstantsInitializer {
@Value("${paging_size}")
private String pagingSize;
@PostConstruct
public void initConstants() {
Constants.PAGING_SIZE = this.pagingSize;
}
}
Servlet
Servlet 中最重要的配置文件就是 web.xml ,它的主要用途是配置Servlet映射和过滤器。而在 Spring Boot 中这将简单很多,只需要将对应的 Servlet 和 Filter 定义为 Bean 即可。
声明一个映射根路径的 Servlet ,例如 Spring MVC 的 DispatcherServlet :
Spring MVC
Spring MVC 主要的配置都可以通过继承 WebMvcConfigurerAdapter (或者 WebMvcConfigurationSupport )类进行修改,这两个类的主要方法有:
Spring MVC 主要的配置都可以通过继承 WebMvcConfigurerAdapter (或者 WebMvcConfigurationSupport )类进行修改,这两个类的主要方法有:
Spring MVC 主要的配置都可以通过继承 WebMvcConfigurerAdapter (或者 WebMvcConfigurationSupport )类进行修改,这两个类的主要方法有:
addFormatters:增加格式化工具(用于接收参数)configureMessageConverters:配置消息转换器(用于@RequestBody和@ResponseBody)configurePathMatch:配置路径映射addArgumentResolvers:配置参数解析器(用于接收参数)addInterceptors:添加拦截器
总之几乎所有关于 Spring MVC 都可以在这个类中配置。之后只需要将其设为 @Configuration,Spring Boot 就会在运行时加载这些配置。
只需要将其设为 @Configuration,Spring Boot 就会在运行时加载这些配置。
只需要将其设为 @Configuration,Spring Boot 就会在运行时加载这些配置。
只需要将其设为 @Configuration,Spring Boot 就会在运行时加载这些配置。
还有一些常用的 Bean 默认会自动创建,但是可以通过自定义进行覆盖,例如负责 @RequestBody 和 @RequestBody 进行转换的 MappingJackson2HttpMessageConverter 和 ObjectMapper ,可以直接这样覆盖掉:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
return new CustomMappingJackson2HttpMessageConverter();
}
@Bean
public ObjectMapper jsonMapper(){
ObjectMapper objectMapper = new ObjectMapper();
//null输出空字符串
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString("");
}
});
return objectMapper;
}
DataSource
如果使用了 spring-boot-starter-data-jpa ,Spring Boot将会自动创建一个 DataSource Bean。可以直接在配置文件中定义它的属性,前缀是 spring.datasource 。并且无需指定数据库的方言,这个 Bean 会自动根据项目中依赖的数据库驱动判断使用的哪种数据库。
同样的,如果使用了 spring-boot-starter-data-redis ,也会自动创建 RedisTemplate 、 ConnectionFactory 等 Bean。也同样可以在配置文件中定义属性,前缀是 spring.redis 。
还有一些常用的 Bean 默认会自动创建,但是可以通过自定义进行覆盖,例如负责 @RequestBody 和 @RequestBody 进行转换的 MappingJackson2HttpMessageConverter 和 ObjectMapper ,可以直接这样覆盖掉:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
return new CustomMappingJackson2HttpMessageConverter();
}
@Bean
public ObjectMapper jsonMapper(){
ObjectMapper objectMapper = new ObjectMapper();
//null输出空字符串
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString("");
}
});
return objectMapper;
}
DataSource
如果使用了 spring-boot-starter-data-jpa ,Spring Boot将会自动创建一个 DataSource Bean。可以直接在配置文件中定义它的属性,前缀是 spring.datasource 。并且无需指定数据库的方言,这个 Bean 会自动根据项目中依赖的数据库驱动判断使用的哪种数据库。
同样的,如果使用了 spring-boot-starter-data-redis ,也会自动创建 RedisTemplate 、 ConnectionFactory 等 Bean。也同样可以在配置文件中定义属性,前缀是 spring.redis 。
springboot用来简化Spring框架带来的大量XML配置以及复杂的依赖管理,让开发人员可以更加关注业务逻辑的开发。
比如不使用springboot而使用SpringMVC作为web框架进行开发的时候,需要配置相关的SpringMVC配置以及对应的依赖,比较繁琐;而使用springboot的话只需要以下短短的几行代码就可以使用SpringMVC,可谓相当地方便:
@RestController
class App {
@RequestMapping("/")
String home() {
"hello"
}
}
其中maven配置如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
我们以使用SpringMVC并且视图使用freemarker为例,分析springboot内部是如何解析freemarker视图的。
如果要在springboot中使用freemarker视图框架,并且使用maven构建项目的时候,还需要加入以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
这个spring-boot-starter-freemarker依赖对应的jar包里的文件如下:
META-INF
├── MANIFEST.MF
├── maven
│ └── org.springframework.boot
│ └── spring-boot-starter-freemarker
│ ├── pom.properties
│ └── pom.xml
└── spring.provides
这个spring-boot-starter-parent的pom文件在http://central.maven.org/maven2/org/springframework/boot/spring-boot-starter-parent/1.3.5.RELEASE/spring-boot-starter-parent-1.3.5.RELEASE.pom 里。
它内部也有一个parent:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
这个spring-boot-dependencies的pom文件在http://central.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/1.3.5.RELEASE/spring-boot-dependencies-1.3.5.RELEASE.pom,内部有很多依赖。
其中spring-boot-starter-web内部依赖了spring的两个spring web依赖:spring-web和spring-webmvc。
spring-boot-starter-web内部还依赖spring-boot-starter,这个spring-boot-starter依赖了spring核心依赖spring-core;还依赖了spring-boot和spring-boot-autoconfigure这两个。
spring-boot定义了很多基础功能类,像运行程序的SpringApplication,Logging系统,一些tomcat或者jetty这些EmbeddedServlet容器,配置属性loader等等。
包括了这些包:

spring-boot-autoconfigure定义了很多自动配置的类,比如jpa,solr,redis,elasticsearch、mongo、freemarker、velocity,thymeleaf等等自动配置的类。
以freemarker为例,看一下它的自动化配置类:
@Configuration // 使用Configuration注解,自动构造一些内部定义的bean
@ConditionalOnClass({ freemarker.template.Configuration.class,
FreeMarkerConfigurationFactory.class }) // 需要freemarker.template.Configuration和FreeMarkerConfigurationFactory这两个类存在在classpath中才会进行自动配置
@AutoConfigureAfter(WebMvcAutoConfiguration.class) // 本次自动配置需要依赖WebMvcAutoConfiguration这个配置类配置之后触发。这个WebMvcAutoConfiguration内部会配置很多Wen基础性的东西,比如RequestMappingHandlerMapping、RequestMappingHandlerAdapter等
@EnableConfigurationProperties(FreeMarkerProperties.class) // 使用FreeMarkerProperties类中的配置
public class FreeMarkerAutoConfiguration {
private static final Log logger = LogFactory
.getLog(FreeMarkerAutoConfiguration.class);
@Autowired
private ApplicationContext applicationContext;
@Autowired
private FreeMarkerProperties properties;
@PostConstruct // 构造之后调用的方法,组要检查模板位置是否存在
public void checkTemplateLocationExists() {
if (this.properties.isCheckTemplateLocation()) {
TemplateLocation templatePathLocation = null;
List<TemplateLocation> locations = new ArrayList<TemplateLocation>();
for (String templateLoaderPath : this.properties.getTemplateLoaderPath()) {
TemplateLocation location = new TemplateLocation(templateLoaderPath);
locations.add(location);
if (location.exists(this.applicationContext)) {
templatePathLocation = location;
break;
}
}
if (templatePathLocation == null) {
logger.warn("Cannot find template location(s): " + locations
+ " (please add some templates, "
+ "check your FreeMarker configuration, or set "
+ "spring.freemarker.checkTemplateLocation=false)");
}
}
}
protected static class FreeMarkerConfiguration {
@Autowired
protected FreeMarkerProperties properties;
protected void applyProperties(FreeMarkerConfigurationFactory factory) {
factory.setTemplateLoaderPaths(this.properties.getTemplateLoaderPath());
factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
factory.setDefaultEncoding(this.properties.getCharsetName());
Properties settings = new Properties();
settings.putAll(this.properties.getSettings());
factory.setFreemarkerSettings(settings);
}
}
@Configuration
@ConditionalOnNotWebApplication // 非Web项目的自动配置
public static class FreeMarkerNonWebConfiguration extends FreeMarkerConfiguration {
@Bean
@ConditionalOnMissingBean
public FreeMarkerConfigurationFactoryBean freeMarkerConfiguration() {
FreeMarkerConfigurationFactoryBean freeMarkerFactoryBean = new FreeMarkerConfigurationFactoryBean();
applyProperties(freeMarkerFactoryBean);
return freeMarkerFactoryBean;
}
}
@Configuration // 自动配置的类
@ConditionalOnClass(Servlet.class) // 需要运行在Servlet容器下
@ConditionalOnWebApplication // 需要在Web项目下
public static class FreeMarkerWebConfiguration extends FreeMarkerConfiguration {
@Bean
@ConditionalOnMissingBean(FreeMarkerConfig.class)
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
applyProperties(configurer);
return configurer;
}
@Bean
public freemarker.template.Configuration freeMarkerConfiguration(
FreeMarkerConfig configurer) {
return configurer.getConfiguration();
}
@Bean
@ConditionalOnMissingBean(name = "freeMarkerViewResolver") // 没有配置freeMarkerViewResolver这个bean的话,会自动构造一个freeMarkerViewResolver
@ConditionalOnProperty(name = "spring.freemarker.enabled", matchIfMissing = true) // 配置文件中开关开启的话,才会构造
public FreeMarkerViewResolver freeMarkerViewResolver() {
// 构造了freemarker的ViewSolver,这就是一开始我们分析的为什么没有设置ViewResolver,但是最后却还是存在的原因
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
this.properties.applyToViewResolver(resolver);
return resolver;
}
}
}
freemarker对应的配置类:
@ConfigurationProperties(prefix = "spring.freemarker") // 使用配置文件中以spring.freemarker开头的配置
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/"; // 默认路径
public static final String DEFAULT_PREFIX = ""; // 默认前缀
public static final String DEFAULT_SUFFIX = ".ftl"; // 默认后缀
...
}
下面是官网上的freemarker配置:
# FREEMARKER (FreeMarkerAutoConfiguration)
spring.freemarker.allow-request-override=false # Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.allow-session-override=false # Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.cache=false # Enable template caching.
spring.freemarker.charset=UTF-8 # Template encoding.
spring.freemarker.check-template-location=true # Check that the templates location exists.
spring.freemarker.content-type=text/html # Content-Type value.
spring.freemarker.enabled=true # Enable MVC view resolution for this technology.
spring.freemarker.expose-request-attributes=false # Set whether all request attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-session-attributes=false # Set whether all HttpSession attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-spring-macro-helpers=true # Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".
spring.freemarker.prefer-file-system-access=true # Prefer file system access for template loading. File system access enables hot detection of template changes.
spring.freemarker.prefix= # Prefix that gets prepended to view names when building a URL.
spring.freemarker.request-context-attribute= # Name of the RequestContext attribute for all views.
spring.freemarker.settings.*= # Well-known FreeMarker keys which will be passed to FreeMarker's Configuration.
spring.freemarker.suffix= # Suffix that gets appended to view names when building a URL.
spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.
spring.freemarker.view-names= # White list of view names that can be resolved.
所以说一开始我们加入了一个spring-boot-starter-freemarker依赖,这个依赖中存在freemarker的lib,满足了FreeMarkerAutoConfiguration中的ConditionalOnClass里写的freemarker.template.Configuration.class这个类存在于classpath中。
所以就构造了FreeMarkerAutoConfiguration里的ViewResolver,这个ViewResolver被自动加入到SpringMVC中。
同样地,如果我们要使用velocity模板,springboot内部也有velocity的自动配置类VelocityAutoConfiguration,原理是跟freemarker一样的。
其他:
Mybatis的autoconfigure是Mybatis提供的springboot的自动配置模块,由于springboot官方没有提供mybatis的自动化配置模块,所以mybatis自己写了这么一个模块,观察它的源码,发现基本上跟freemarker的autoconfigure模块一样,只需要构造对应的实例即可。
总结:
springboot内部提供了很多自动化配置的类,这些类会判断classpath中是否存在自己需要的那个类,如果存在则会自动配置相关的配置,否则就不会自动配置。
如果我们需要使用一些框架,只需要加入依赖即可,这些依赖内部是没有代码的,只是一些对应框架需要的lib,有了这些lib就会触发自动化配置,于是就能使用框架了。
这一点跟当时看springmvc的时候对response进行json或xml渲染的原理相同。springmvc中的requestmapping注解加上responsebody注解后会返回xml或者json,如果依赖中加入jackson依赖就会转换成json,如果依赖中加入xstream依赖就会转换成xml。当然,前提是springmvc中有了这两种依赖的HttpMessageConverter代码,这个HttpMessageConverter代码就相当于springboot中的各种AutoConfiguration。
spring boot约定优于配置的这种做法在如今越来越流行了的更多相关文章
- 【面试普通人VS高手系列】Spring Boot的约定优于配置,你的理解是什么?
对于Spring Boot约定优于配置这个问题,看看普通人和高手是如何回答的? 普通人的回答: 嗯, 在Spring Boot里面,通过约定优于配置这个思想,可以让我们少写很多的配置, 然后就只需要关 ...
- spring boot中的约定优于配置
Spring Boot并不是一个全新的框架,而是将已有的Spring组件整合起来. Spring Boot可以说是遵循约定优于配置这个理念产生的.它的特点是简单.快速和便捷. 既然遵循约定优于配置,则 ...
- convention over configuration 约定优于配置 按约定编程 约定大于配置 PEP 20 -- The Zen of Python
为什么说 Java 程序员必须掌握 Spring Boot ?_知识库_博客园 https://kb.cnblogs.com/page/606682/ 为什么说 Java 程序员必须掌握 Spring ...
- SpringMVC介绍之约定优于配置
SpringMVC介绍之约定优于配置 所谓的约定优于配置就是指在程序开发过程中我们约定好一些规则可以使我们更少的进行配置和代码编写.就这么简单的一句话可能你还不是很懂什么是约定优于配置,没关系,看完后 ...
- Spring Boot 2.0 配置图文教程
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...
- spring boot多数据源配置(mysql,redis,mongodb)实战
使用Spring Boot Starter提升效率 虽然不同的starter实现起来各有差异,但是他们基本上都会使用到两个相同的内容:ConfigurationProperties和AutoConfi ...
- Spring Boot 外部化配置(一)- Environment、ConfigFileApplicationListener
目录 前言 1.起源 2.外部化配置的资源类型 3.外部化配置的核心 3.1 Environment 3.1.1.ConfigFileApplicationListener 3.1.2.关联 Spri ...
- Spring Boot 外部化配置(二) - @ConfigurationProperties 、@EnableConfigurationProperties
目录 3.外部化配置的核心 3.2 @ConfigurationProperties 3.2.1 注册 Properties 配置类 3.2.2 绑定配置属性 3.1.3 ConfigurationP ...
- spring boot web相关配置
spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何web相关的配置便能提供web服务,这还得归于spri ...
随机推荐
- BA-siemens-insight-event builder使用
event builder功能主要是用来给report使用的,作为一个独立的对象,这个对象的功能就是收集点位的信息,如果再使用report功能就可以显示或输出点位的信息.
- gem update --system
gem update --system 修改完gem sources之后,进行gem update: gem update --system 之后的输出: C:\Sites\test01>gem ...
- 积跬步,聚小流------Bootstrap学习记录(1)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- 继承的综合运用《Point类派生出Circle类而且进行各种操作》
类的组合与继承 (1)先建立一个Point(点)类.包括数据成员x,y(坐标点). (2)以Point为基类.派生出一个Circle(圆)类,添加数据成员(半径),基类的成员表示圆心: (3)编写上述 ...
- 深入理解 C 指针阅读笔记 -- 第五章
Chapter5.h #ifndef __CHAPTER_5_ #define __CHAPTER_5_ /*<深入理解C指针>学习笔记 -- 第五章*/ /*不应该改动的字符串就应该用 ...
- 多线程编程TSL相关的技术文档
线程本地存储 (TLS) https://msdn.microsoft.com/zh-cn/library/6yh4a9k1(v=vs.80).aspx Using Thread Local Stor ...
- 2015.05.04,外语,读书笔记-《Word Power Made Easy》 14 “如何谈论日常现象” SESSION 41
1. people are the craziest animals bovine(['bәuvain] adj. (似)牛的, 迟钝的),像牛一样placid(['plæsid] adj. 安静的, ...
- MAME 0.201 发布,重温童年的街机模拟器
MAME 0.201 已发布,MAME 最初是街机模拟器,随着时间的推移,MAME 吸收了姊妹项目 MESS(多机种模拟器超级系统),所以 MAME 现在还记录了各种各样的(大多是老式的)电脑游戏.掌 ...
- ”危险“的RESTRICT与GCC的编译优化(编程者对编译器所做的一个“承诺”:使用restrict修饰过的指针,它所指向的内容只能经由该指针修改)
restrict是C99标准中新添加的关键字,对于从C89标准开始起步学习C语言的同学来说(包括我),第一次看到restrict还是相当陌生的.Wikipedia给出的解释如下: In the C p ...
- h5-弹出层layer,提示,顶部横条,
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYkAAAI7CAIAAACWVfAJAAAgAElEQVR4nOy9f1ATWb733z3uOA4kIC ...