Spring Boot简介
Emac
2016/7
Born for Microservice!
1 容器反转
2 简化配置
3 CoC
1 容器反转
1.1 容器切换
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
1.2 启动应用
$java -jar demo-0.0.1-SNAPSHOT.jar
$mvn spring-boot:run
OR
1.3 MANIFEST.MF
Manifest-Version: 1.0 Implementation-Title: demo Implementation-Version: 0.0.1-SNAPSHOT Archiver-Version: Plexus Archiver Built-By: emac Start-Class: com.example.DemoApplication Implementation-Vendor-Id: com.example Spring-Boot-Version: 1.3.6.RELEASE Created-By: Apache Maven 3.3.3 Build-Jdk: 1.8.0_60 Implementation-Vendor: Pivotal Software, Inc. Main-Class: org.springframework.boot.loader.JarLauncher
2.1 多级配置
- 命令行参数
- 环境变量
- 外部配置文件
- 内嵌配置文件
- @PropertySource
2.2 Profile
- -Dspring.profiles.active=foo
2 简化配置
@Autowired
private Environment env;
2.3 核心注解
- @SpringBootApplication
- @Configuration
- @Bean
2 简化配置
2.4 常用注解
注解扫描
- @ComponentScan
- @Import
条件加载
- @Profile
- @ConditionalOn***
- @Lazy
属性注入
- @Value("${foo}")
- @ConfigurationProperties
Pitfalls
3 Convention over Configuration
(Dependency-Driven-Configuration)
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true # JDBC url of the database.
spring.datasource.username=root
spring.datasource.password= # Login password of the database.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
1. 添加依赖
2. 配置属性
Demo
Spring Boot简介
By Emacoo Shen