Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | 云栖社区 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Github | Search | About

第 5 章 Spring Boot

目录

5.1. Spring Boot Quick start
5.1.1. pom.xml
5.1.2. Controller
5.2. Maven 插件
5.2.1. resource
5.3. 运行 Spring boot 项目
5.3.1. Maven run
5.3.2. Linux systemd
5.3.3. 传统 init.d 脚本
5.3.4. 编译用于Tomcat的 War
5.4. SpringApplication
5.4.1. @SpringBootApplication
5.4.1.1. 排除 @EnableAutoConfiguration 加载项
5.4.2. @EnableAutoConfiguration
5.4.3. @ComponentScan
5.4.4. @EntityScan 实体扫描
5.4.5. @EnableJpaRepositories
5.4.6. CharacterEncodingFilter
5.4.7. 隐藏 Banner
5.4.8. 实体与仓库扫描
5.4.9. 列出 Beans
5.5. application.properties
5.5.1. application.properties 参考
5.5.2. 启动指定参数
5.5.3. 加载排除
5.5.4. PID FILE
5.5.5. 内嵌 tomcat server
5.5.5.1. server.tomcat.basedir
5.5.5.2. server
5.5.5.3. access.log
5.5.5.4. logging
5.5.5.5. charset
5.5.5.6. Session 配置
5.5.5.7. cookie
5.5.5.8. error 路径
5.5.5.9. 压缩传输
5.5.5.10. server.servlet.context-path
5.5.5.11. ssl
5.5.5.12. 上传限制
5.5.6. JSON 输出与日期格式化
5.5.7. SMTP 相关配置
5.5.8. Redis
5.5.9. MongoDB
5.5.10. MySQL
5.5.11. Oracle
5.5.12. default_schema
5.5.13. datasource
5.5.14. velocity
5.5.15. Security 相关配置
5.5.16. MVC 配置
5.5.17. Kafka 相关配置
5.5.18.
5.6. Properties
5.6.1. @Value 注解
5.6.2. @PropertySource 注解载入 properties 文件
5.6.3. @EnableConfigurationProperties 引用自定义 *.properties 配置文件
5.6.4. 手工载入 *.properties 文件
5.6.5. spring.profiles.active 参数切换配置文件
5.6.6. SpringApplicationBuilder.properties() 方法添加配置项
5.7. Spring boot with Logging
5.7.1. 打印日志
5.7.2. 日志输出级别
5.7.3. 禁止控制台输出日志
5.7.4. 标准输出
5.7.5. 安天分隔日志
5.7.6. 按照文件尺寸分割日志
5.7.7. 指定Class过滤日志
5.7.8. 日志写入 MongoDB
5.7.9. logback 配置详解
5.7.9.1. configuration 属性配置
5.7.9.2. contextName 设置上下文名称
5.7.9.3. property 设置变量
5.7.9.4. encoder 日志格式设置
5.7.9.5. RollingFileAppender
5.8. Spring boot with Webpage
5.8.1. Maven
5.8.2. application.properties
5.8.3. Application
5.8.4. IndexController
5.8.5. src/main/webapp/WEB-INF/jsp/index.jsp
5.8.6. 集成模板引擎
5.9. Spring boot with Velocity template
5.9.1. Maven
5.9.2. Resource
5.9.3. Application
5.9.4. RestController
5.9.5. Test
5.10. Spring boot with Thymeleaf
5.10.1. Maven
5.10.2. application.properties
5.10.3. Controller
5.10.4. HTML5 Template
5.11. String boot with RestTemplate
5.11.1. 在控制器中完整实例
5.12. Spring boot with Session share
5.12.1. Redis
5.12.1.1. Maven
5.12.1.2. application.properties
5.12.1.3. Application
5.12.2. 测试 Session
5.12.3. JDBC
5.13. Spring boot with MongoDB
5.13.1. Maven
5.13.2. Application
5.13.3. MongoTemplate
5.13.4. Repository
5.14. Spring boot with MySQL
5.14.1. Maven
5.14.2. Resource
5.14.3. Application
5.14.4. JdbcTemplate
5.14.5. CrudRepository
5.15. Spring boot with Oracle
5.15.1. Maven
5.15.2. application.properties
5.15.3. Application
5.15.4. CrudRepository
5.15.5. JdbcTemplate
5.15.6. Controller
5.16. Spring boot with PostgreSQL
5.16.1. pom.xml
5.16.2. application.properties
5.16.3. Application
5.16.4. CrudRepository
5.16.5. JdbcTemplate
5.16.6. Controller
5.16.7. Test
5.17. 数据源配置
5.17.1. 多数据源配置
5.17.2. JPA 多数据源
5.17.3. Connection and Statement Pooling
5.17.3.1. org.apache.tomcat.jdbc.pool.DataSource
5.17.3.2. druid
5.17.3.3. c3p0 - JDBC3 Connection and Statement Pooling
5.17.3.4. dbcp2
5.17.3.5. bonecp
5.17.3.6. dbcp2
5.18. Spring boot with Redis
5.18.1. Spring boot with Redis
5.18.1.1. maven
5.18.1.2. application.properties
5.18.1.3. JUnit
5.18.1.4. Controller
5.18.2. Redis Pub/Sub
5.18.2.1. Redis配置类
5.18.2.2. 订阅和发布类
5.18.2.3. 消息发布演示
5.19. Spring boot with Caching
5.19.1. maven
5.19.2. 启用 Cache
5.19.3. 测试 Controller
5.19.4. @Cacheable 的用法
5.19.5. @CachePut 用法
5.19.6. 解决Expire 和 TTL 过期时间
5.19.7. SpEL表达式
5.20. Spring boot with Email
5.20.1. Maven
5.20.2. Resource
5.20.3. POJO
5.20.4. RestController
5.20.5. Test
5.21. Spring boot with Spring security
5.21.1. Maven
5.21.2. Reource
5.21.3. Application
5.21.4. WebSecurityConfigurer
5.21.5. RestController
5.21.6. 测试
5.21.7. Spring + Security + MongoDB
5.21.7.1. Account
5.21.7.2. AccountRepository
5.21.7.3. WebSecurityConfiguration
5.22. Spring boot with Oauth2
5.22.1. Maven
5.22.2. Password tools
5.22.3. Server
5.22.3.1. Maven
5.22.3.2. application.properties
5.22.3.3. EnableAuthorizationServer
5.22.3.4. EnableResourceServer
5.22.3.5. Entity Table
5.22.3.6. UserRepository
5.22.3.7. UserService
5.22.3.8. TestRestController
5.22.3.9. 数据库初始化
5.22.3.10. Test
5.22.4. Spring boot with Oauth2 RestTemplate
5.22.4.1. Maven
5.22.4.2. OAuth2ClientConfiguration.java
5.22.4.3. Application.java
5.22.4.4. application.properties
5.22.4.5. Controller
5.22.4.6. Test
5.23. Spring boot with Oauth2 jwt
5.23.1. Maven
5.23.2. Authorization Server
5.23.3. Resource Server
5.23.4. Web Security
5.23.5. 插入数据
5.23.6. 使用 CURL 测试 JWT
5.23.7. 测试 Shell
5.23.8. refresh_token
5.24. Spring boot with Oauth2 jwt 非对称证书
5.24.1. 创建证书
5.24.2. Authorization Server
5.24.3. Resource Server
5.25. Apple iOS 访问 Oauth2
5.26. Android Oauth2 + Jwt example
5.27. Spring boot with Scheduling
5.27.1. Application.java
5.27.2. Component
5.27.3. 查看日志
5.27.4. 计划任务控制开关
5.27.5. @Scheduled 详解
5.27.5.1. 每3秒钟一运行一次
5.27.5.2. 凌晨23点运行
5.28. Spring boot with Hessian
5.28.1. Maven
5.28.2. Application
5.28.3. HessianServiceExporter
5.28.4. Service
5.28.5. RestController
5.29. Spring boot with Apache Kafka
5.29.1. 安装 kafka
5.29.2. maven
5.29.3. Spring boot Application
5.29.4. EnableKafka
5.29.5. KafkaListener
5.29.6. 测试
5.29.7. 完整的发布订阅实例
5.29.7.1. Consumer
5.29.7.2. Producer
5.29.7.3. Test
5.29.8. Spring cloud with Kafka
5.29.8.1. Application 主文件
5.29.8.2. 资源配置文件
5.29.8.2.1. application.properties
5.29.8.2.2. bootstrap.properties
5.29.8.2.3. Git 仓库
5.29.8.3. 启用 kafka
5.29.8.4. 消息发布主程序
5.30. Spring boot with RabbitMQ(AMQP)
5.30.1. maven
5.30.2. RabbitMQConfig
5.30.3. 生产者
5.30.4. 消费者
5.31. Spring boot with Git version
5.31.1. CommonRestController 公共控制器
5.31.2. VersionRestController 测试控制器
5.31.3. 创建 .gitattributes 文件
5.32. Spring boot with HTTPS SSL
5.32.1. 生成自签名证书
5.32.2. application.properties 配置文件
5.32.3. 启动 Spring boot
5.32.4. restTemplate 调用实例
5.33. Spring boot with Elasticsearch 2.x
5.33.1. Maven
5.33.2. Application
5.33.3. application.properties
5.33.4. Domain
5.33.5. ElasticsearchRepository
5.34. Spring boot with Elasticsearch 5.5.x
5.34.1. Maven
5.34.2. Application
5.34.3. application.properties
5.34.4. ElasticsearchConfiguration
5.34.5. RestController
5.35. Spring boot with Apache Hive
5.35.1. Maven
5.35.2. application.properties
5.35.3. Configuration
5.35.4. CURD 操作实例
5.36. Spring boot with Phoenix
5.36.1. Maven
5.36.2. application.properties
5.36.3. Configuration
5.37. Spring boot with Data restful
5.37.1. Maven
5.38. Spring boot with ELK(Elasticsearch + Logstash + Kibana)
5.38.1. TCP 方案
5.38.2. Redis 方案
5.38.3. Kafka 方案
5.38.4. Other
5.39. Springboot with Ethereum (web3j)
5.39.1. Maven
5.39.2. application.properties
5.39.3. TestRestController
5.39.4. 测试
5.40. 异步执行
5.40.1. 最简单的配置
5.40.2.
5.41. Spring boot with export csv.
5.42. Spring boot with lombok

注意以下使用 Spring boot 2.0.2

5.1. Spring Boot Quick start

5.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>		
		
		

5.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/