覆盖 Spring Boot 自动配置
使用SpringSecurity
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
创建自定义的安全配置
- 扩展了WebSecurityConfigurerAdapter的配置类
通过属性文件外置配置
Spring Boot能从多种属性源获得属性【按照优先级排序】
- (1) 命令行参数
- (2) java:comp/env里的JNDI属性
- (3) JVM系统属性
- (4) 操作系统环境变量
- (5) 随机生成的带random.*前缀的属性(在设置其他属性时,可以引用它们,比如${random.
long})
- (6) 应用程序以外的application.properties或者appliaction.yml文件
- (7) 打包在应用程序内的application.properties或者appliaction.yml文件
- (8) 通过@PropertySource标注的属性源
- (9) 默认属性application.properties和application.yml文件能放在以下四个位置【按照优先级排序】
- (1) 外置,在相对于应用程序运行目录的/config子目录里。
- (2) 外置,在应用程序运行的目录里。
- (3) 内置,在config包内。
- (4) 内置,在Classpath根目录。
自动配置微调
- 禁用模板缓存
- spring.freemarker.cache(Freemarker)
- spring.groovy.template.cache(Groovy模板)
- spring.velocity.cache(Velocity)
- spring.thymeleaf.cache
> 2. 配置嵌入式服务器
server:
port: 8443
ssl:
key-store: file:///path/to/mykeys.jks
key-store-password: letmein
key-password: letmein
配置日志
- 日志yml配置
logging:
path: /var/logs/
file: BookWorm.log
level:
root: WARN
org:
springframework:
security: DEBUG
默认情况下,日志文件的大小达到10MB时会切分一次。
- 要完全掌握日志配置,可以在Classpath的根目录(src/main/resources)里创建logback.xml文件。
- 自定义配置
通过logging.config属性指定自定义的名字:
logging:
config:
classpath:logging-config.xml
配置数据源
spring:
datasource:
url: jdbc:mysql://localhost/readinglist
username: dbuser
password: dbpass
driver-class-name: com.mysql.jdbc.Driver
应用程序 Bean 的配置外置
- 在类前面加上
@ConfigurationProperties(prefix="amazon")
- yml
amazon:
associateId: habuma-20
- 开启配置属性
,@ConfigurationProperties注解不会生效,除
非先向Spring配置类添加@EnableConfigurationProperties注解。但通常无需这么
做,因为Spring Boot自动配置后面的全部配置类都已经加上了@EnableConfigurationProperties注解。
amazon.associateId = amazon.associate_id = amazon.associate-id
使用 Profile 进行配置
- Profile是一种条件化配置,基于运行时激活的Profile,会
使用或者忽略不同的Bean或配置类
spring:
profiles:
active: production
logging:
level:
root: INFO
---
spring:
profiles: development
logging:
level:
root: DEBUG
---
spring:
profiles: production
logging:
path: /tmp/
file: BookWorm.log
level:
root: WARN
使用一组三个连字符(---)作为分隔符
web项目打jar 支持运行jsp
- maven配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--目前1.4.2版本支持-->
<version>1.4.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<!-- 指定resources插件处理哪个目录下的资源文件 -->
<directory>src/main/webapp</directory>
<!--注意此次必须要放在此目录下才能被访问到-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>