Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | About

第 2 章 Spring Boot

目錄

2.1. Spring Boot Quick start
2.1.1. pom.xml
2.1.2. Controller
2.2. 運行 Spring boot 項目
2.2.1. Maven run
2.2.2. Linux systemd
2.2.3. 傳統 init.d 腳本
2.3. SpringApplication
2.3.1. @SpringBootApplication
2.3.1.1. 排除 @EnableAutoConfiguration 加載項
2.3.2. @EnableAutoConfiguration
2.3.3. @ComponentScan
2.3.4. @EntityScan 實體掃瞄
2.3.5. @EnableJpaRepositories
2.3.6. CharacterEncodingFilter
2.3.7. 隱藏 Banner
2.3.8. CORS 跨域請求
2.3.9. 實體與倉庫掃瞄
2.3.10. 列出 Beans
2.4. 編譯用於Tomcat的 War
2.5. Properties
2.5.1. @EnableConfigurationProperties 引用自定義 *.properties 配置檔案
2.5.2. spring.profiles.active 參數切換配置檔案
2.5.3. SpringApplicationBuilder.properties() 方法添加配置項
2.6. Spring boot with Logging
2.6.1. 打印日誌
2.6.2. 日誌輸出級別
2.6.3. 禁止控制台輸出日誌
2.6.4. 標準輸出
2.6.5. 日誌分割
2.6.6. 日誌寫入 MongoDB
2.7. Service
2.7.1. Application
2.7.2. 定義介面
2.7.3. 實現介面
2.7.4. 調用 Service
2.8. Spring boot with Webpage
2.8.1. Maven
2.8.2. application.properties
2.8.3. Application
2.8.4. IndexController
2.8.5. src/main/webapp/WEB-INF/jsp/index.jsp
2.8.6. 整合模板引擎
2.9. String boot with RestTemplate
2.9.1. 在控製器中完整實例
2.10. Spring boot with Session share
2.10.1. Redis
2.10.1.1. Maven
2.10.1.2. application.properties
2.10.1.3. Application
2.10.2. 測試 Session
2.10.3. JDBC
2.11. Connection and Statement Pooling
2.11.1. org.apache.tomcat.jdbc.pool.DataSource
2.11.2. druid
2.11.3. c3p0 - JDBC3 Connection and Statement Pooling
2.11.4. dbcp2
2.11.5. bonecp
2.11.6. dbcp2
2.12. Spring boot with MongoDB
2.12.1. Maven
2.12.2. Application
2.12.3. MongoTemplate
2.12.4. Repository
2.13. Spring boot with MySQL
2.13.1. Maven
2.13.2. Resource
2.13.3. Application
2.13.4. JdbcTemplate
2.13.5. CrudRepository
2.14. Spring boot with Oracle
2.14.1. Maven
2.14.2. application.properties
2.14.3. Application
2.14.4. CrudRepository
2.14.5. JdbcTemplate
2.14.6. Controller
2.15. Spring boot with PostgreSQL
2.15.1. pom.xml
2.15.2. application.properties
2.15.3. Application
2.15.4. CrudRepository
2.15.5. JdbcTemplate
2.15.6. Controller
2.15.7. Test
2.16. Spring boot with Email
2.16.1. Maven
2.16.2. Resource
2.16.3. POJO
2.16.4. RestController
2.16.5. Test
2.17. Spring boot with Velocity template
2.17.1. Maven
2.17.2. Resource
2.17.3. Application
2.17.4. RestController
2.17.5. Test
2.18. Spring boot with Thymeleaf
2.18.1. Maven
2.18.2. application.properties
2.18.3. Controller
2.18.4. HTML5 Template
2.19. Spring boot with Spring security
2.19.1. Maven
2.19.2. Reource
2.19.3. Application
2.19.4. WebSecurityConfigurer
2.19.5. RestController
2.19.6. 測試
2.19.7. Spring + Security + MongoDB
2.19.7.1. Account
2.19.7.2. AccountRepository
2.19.7.3. WebSecurityConfiguration
2.20. Spring boot with Oauth2
2.20.1. Maven
2.20.2. Password tools
2.20.3. Server
2.20.3.1. Maven
2.20.3.2. application.properties
2.20.3.3. EnableAuthorizationServer
2.20.3.4. EnableResourceServer
2.20.3.5. Entity Table
2.20.3.6. UserRepository
2.20.3.7. UserService
2.20.3.8. TestRestController
2.20.3.9. 資料庫初始化
2.20.3.10. Test
2.20.4. Spring boot with Oauth2 RestTemplate
2.20.4.1. Maven
2.20.4.2. OAuth2ClientConfiguration.java
2.20.4.3. Application.java
2.20.4.4. application.properties
2.20.4.5. Controller
2.20.4.6. Test
2.21. Spring boot with Scheduling
2.21.1. Application.java
2.21.2. Component
2.21.3. 查看日誌
2.21.4. 計劃任務控制
2.22. Spring boot with Cache
2.22.1. Spring boot with Redis
2.22.1.1. maven
2.22.1.2. application.properties
2.22.1.3. JUnit
2.22.1.4. Controller
2.22.2. @Cacheable
2.22.2.1. maven
2.22.2.2. Controller
2.23. Spring boot with Hessian
2.23.1. Maven
2.23.2. Application
2.23.3. HessianServiceExporter
2.23.4. Service
2.23.5. RestController
2.24. Spring boot with Apache Kafka
2.24.1. 安裝 kafka
2.24.2. maven
2.24.3. Spring boot Application
2.24.4. EnableKafka
2.24.5. KafkaListener
2.24.6. 測試
2.24.7. 完整的發佈訂閲實例
2.24.7.1. Consumer
2.24.7.2. Producer
2.24.7.3. Test
2.24.8. Spring cloud with Kafka
2.24.8.1. Application 主檔案
2.24.8.2. 資源配置檔案
2.24.8.2.1. application.properties
2.24.8.2.2. bootstrap.properties
2.24.8.2.3. Git 倉庫
2.24.8.3. 啟用 kafka
2.24.8.4. 消息發佈主程序
2.25. Spring boot with Git version
2.25.1. CommonRestController 公共控製器
2.25.2. VersionRestController 測試控製器
2.25.3. 創建 .gitattributes 檔案
2.26. Spring boot with HTTPS SSL
2.26.1. 生成自簽名證書
2.26.2. application.properties 配置檔案
2.26.3. 啟動 Spring boot
2.26.4. restTemplate 調用實例
2.27. Spring boot with Elasticsearch 2.x
2.27.1. Maven
2.27.2. Application
2.27.3. application.properties
2.27.4. Domain
2.27.5. ElasticsearchRepository
2.28. Spring boot with Elasticsearch 5.5.x
2.28.1. Maven
2.28.2. Application
2.28.3. application.properties
2.28.4. ElasticsearchConfiguration
2.28.5. RestController
2.29. Spring boot with Apache Hive
2.29.1. Maven
2.29.2. application.properties
2.29.3. Configuration
2.29.4. CURD 操作實例
2.30. Spring boot with Phoenix
2.30.1. Maven
2.30.2. application.properties
2.30.3. Configuration
2.31. Spring boot with Data restful
2.31.1. Maven
2.32. Spring boot with ELK(Elasticsearch + Logstash + Kibana)
2.32.1. TCP 方案
2.32.2. Redis 方案
2.32.3. Kafka 方案
2.32.4. Other
2.33. Spring boot with Docker
2.33.1.
2.33.2.
2.33.3.
2.33.4.
2.34. SpringBootTest
2.34.1.
2.35. application.properties
2.35.1. 加載排除
2.35.2. PID FILE
2.35.3. 內嵌 tomcat server
2.35.3.1. server.tomcat.basedir
2.35.3.2. server
2.35.3.3. access.log
2.35.3.4. logging
2.35.3.5. charset
2.35.3.6. Session 配置
2.35.3.7. cookie
2.35.3.8. error 路徑
2.35.3.9. 壓縮傳輸
2.35.3.10. server.servlet.context-path
2.35.3.11. ssl
2.35.4. JSON 輸出與日期格式化
2.35.5. SMTP 相關配置
2.35.6. Redis
2.35.7. MongoDB
2.35.8. MySQL
2.35.9. Oracle
2.35.10. default_schema
2.35.11. datasource
2.35.12. velocity
2.35.13. Security 相關配置
2.35.14. MVC 配置
2.35.15. Kafka 相關配置

注意以下使用 Spring boot 1.5.2

2.1. Spring Boot Quick start

2.1.1. pom.xml

		
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>api.netkiller.cn</groupId>
	<artifactId>api.netkiller.cn</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Skyline</name>
	<description>skylinechencf@gmail.com</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.0.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source />
					<target />
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>		
		
		

2.1.2. Controller

		
package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}
		
		

測試

curl http://127.0.0.1:8080/