Spring Boot 进阶
yaml/properties 文件
我们知道整个 Spring Boot 项目只有一个配置文件,那就是 application.yml,Spring Boot 在启动时,就会从 application.yml 中读取配置信息,并加载到内存中。上一篇我们只是粗略的列举了几个配置项,其实 Spring Boot 的配置项是很多的,本文我们将学习在实际项目中常用的配置项(注:为了方便说明,配置项均以 properties 文件的格式写出,后续的实际配置都会写成 yaml 格式)。
配置项 | 说明 | 举例 |
---|---|---|
server.port | 应用程序启动端口 | server.port=8080,定义应用程序启动端口为8080 |
server.context-path | 应用程序上下文 | server.port=/cx,则访问地址为:http://ip:port/cx |
spring.http.multipart.maxFileSize | 最大文件上传大小,-1为不限制 | spring.http.multipart.maxFileSize=-1 |
spring.jpa.database | 数据库类型 | spring.jpa.database=MYSQL,指定数据库为mysql |
spring.jpa.properties.hibernate.dialect | hql方言 | spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect |
spring.datasource.url | 数据库连接字符串 | spring.datasource.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=true |
spring.datasource.username | 数据库用户名 | spring.datasource.username=root |
spring.datasource.password | 数据库密码 | spring.datasource.password=root |
spring.datasource.driverClassName | 数据库驱动 | spring.datasource.driverClassName=com.mysql.jdbc.Driver |
spring.jpa.showSql | 控制台是否打印sql语句 | spring.jpa.showSql=true |
此处是我的application.yml 配置文件内容:
server:
port: 8080
context-path: /api
tomcat:
max-threads: 1000
min-spare-threads: 50
connection-timeout: 5000
spring:
profiles:
active: dev
http:
multipart:
maxFileSize: -1
datasource:
url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=true
username: root
password: root
driverClassName: com.mysql.jdbc.Driver
jpa:
database: MYSQL
showSql: true
hibernate:
namingStrategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
mybatis:
configuration:
#配置项:开启下划线到驼峰的自动转换. 作用:将数据库字段根据驼峰规则自动注入到对象属性。
map-underscore-to-camel-case: true
多环境配置
在一个企业级系统中,我们可能会遇到这样一个问题:开发时使用开发环境,测试时使用测试环境,上线时使用生产环境。每个环境的配置都可能不一样,比如开发环境的数据库是本地地址,而测试环境的数据库是测试地址。那我们在打包的时候如何生成不同环境的包呢?
这里的解决方案有很多:
- 每次编译之前手动把所有配置信息修改成当前运行的环境信息。这种方式导致每次都需要修改,相当麻烦,也容易出错。
- 利用 Maven,在 pom.xml 里配置多个环境,每次编译之前将 settings.xml 里面修改成当前要编译的环境 ID。这种方式会事先设置好所有环境,缺点就是每次也需要手动指定环境,如果环境指定错误,发布时是不知道的。
- 第三种方案就是本文重点介绍的,也是作者强烈推荐的方式。
首先,创建 application.yml 文件,在里面添加如下内容:
spring:
profiles:
active: dev
含义是指定当前项目的默认环境为 dev,即项目启动时如果不指定任何环境,Spring Boot 会自动从 dev 环境文件中读取配置信息。我们可以将不同环境都共同的配置信息写到这个文件中。
然后创建多环境配置文件,文件名的格式为:application-{profile}.yml,其中,{profile} 替换为环境名字,如 application-dev.yml。
这样,我们就实现了多环境的配置,每次编译打包我们无需修改任何东西,编译为 jar 文件后,运行命令:
java -jar api.jar --spring.profiles.active=dev
其中 --spring.profiles.active
就是我们要指定的环境。
常用注解
我们知道,Spring Boot 主要采用注解的方式,在上一篇的入门实例中,我们也用到了一些注解。
本文,我将详细介绍在实际项目中常用的注解。
@SpringBootApplication
我们可以注意到 Spring Boot 支持 main 方法启动,在我们需要启动的主类中加入此注解,告诉 Spring Boot,这个类是程序的入口。如:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
如果不加这个注解,程序是无法启动的。
我们查看下 SpringBootApplication 的源码,源码如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName")
String[] excludeName() default {};
/**
* Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
* for a type-safe alternative to String-based package names.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
/**
* Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
* scan for annotated components. The package of each class specified will be scanned.
* <p>
* Consider creating a special no-op marker class or interface in each package that
* serves no purpose other than being referenced by this attribute.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
}
在这个注解类上有3个注解,如下:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
因此,我们可以用这三个注解代替 SpringBootApplication,如:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
其中,SpringBootConfiguration 表示 Spring Boot 的配置注解,EnableAutoConfiguration 表示自动配置,ComponentScan 表示 Spring Boot 扫描 Bean 的规则,比如扫描哪些包。
@Configuration
加入了这个注解的类被认为是 Spring Boot 的配置类,我们知道可以在 application.yml 设置一些配置,也可以通过代码设置配置。
如果我们要通过代码设置配置,就必须在这个类上标注 Configuration 注解。如下代码:
@Configuration
public class WebConfig extends WebMvcConfigurationSupport{
@Override
protected void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
registry.addInterceptor(new ApiInterceptor());
}
}
不过 Spring Boot 官方推荐 Spring Boot 项目用 SpringBootConfiguration 来代替 Configuration。
@Bean
这个注解是方法级别上的注解,主要添加在 @Configuration
或 @SpringBootConfiguration
注解的类,有时也可以添加在 @Component
注解的类。它的作用是定义一个Bean。
请看下面代码:
@Bean
public ApiInterceptor interceptor(){
return new ApiInterceptor();
}
那么,我们可以在 ApiInterceptor 里面注入其他 Bean,也可以在其他 Bean 注入这个类。
@Value
通常情况下,我们需要定义一些全局变量,都会想到的方法是定义一个 public static 变量,在需要时调用,是否有其他更好的方案呢?答案是肯定的。下面请看代码:
@Value("${server.port}")
String port;
@RequestMapping("/hello")
public String home(String name) {
return "hi "+name+",i am from port:" +port;
}
其中,server.port 就是我们在 application.yml 里面定义的属性,我们可以自定义任意属性名,通过 @Value
注解就可以将其取出来。
它的好处不言而喻:
- 定义在配置文件里,变量发生变化,无需修改代码。
- 变量交给Spring来管理,性能更好。
注入任何类
本节通过一个实际的例子来讲解如何注入一个普通类,并且说明这样做的好处。
假设一个需求是这样的:项目要求使用阿里云的 OSS 进行文件上传。
我们知道,一个项目一般会分为开发环境、测试环境和生产环境。OSS 文件上传一般有如下几个参数:appKey、appSecret、bucket、endpoint 等。不同环境的参数都可能不一样,这样便于区分。按照传统的做法,我们在代码里设置这些参数,这样做的话,每次发布不同的环境包都需要手动修改代码。
这个时候,我们就可以考虑将这些参数定义到配置文件里面,通过前面提到的 @Value
注解取出来,再通过 @Bean
将其定义为一个 Bean,这时我们只需要在需要使用的地方注入该 Bean 即可。