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

部分 I. Spring Framework

目录

1. Spring Tool Suite
1.1. 安装
1.2. Dashboard
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. application.properties
2.4.1. 加载排除
2.4.2. PID FILE
2.4.3. 内嵌 tomcat server
2.4.3.1. server.tomcat.basedir
2.4.3.2. server
2.4.3.3. tomcat
2.4.3.4. logging
2.4.3.5. charset
2.4.3.6. Session 配置
2.4.3.7. cookie
2.4.3.8. error 路径
2.4.3.9. 压缩传输
2.4.3.10. server.servlet.context-path
2.4.3.11. ssl
2.4.4. JSON
2.4.5. SMTP 相关配置
2.4.6. Redis
2.4.7. MongoDB
2.4.8. MySQL
2.4.9. Oracle
2.4.10. default_schema
2.4.11. datasource
2.4.12. velocity
2.4.13. Security 相关配置
2.5. 编译用于Tomcat的 War
2.6. Properties
2.6.1. @EnableConfigurationProperties 引用自定义 *.properties 配置文件
2.6.2. spring.profiles.active 参数切换配置文件
2.6.3. SpringApplicationBuilder.properties() 方法添加配置项
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 Logging
2.10.1. 打印日志
2.10.2. 日志输出级别
2.10.3. 禁止控制台输出日志
2.10.4. 标准输出
2.10.5. 日志分割
2.10.6. 日志写入 MongoDB
2.11. Spring boot with Session share
2.11.1. Redis
2.11.1.1. Maven
2.11.1.2. application.properties
2.11.1.3. Application
2.11.2. 测试 Session
2.11.3. JDBC
2.12. Connection and Statement Pooling
2.12.1. org.apache.tomcat.jdbc.pool.DataSource
2.12.2. druid
2.12.3. c3p0 - JDBC3 Connection and Statement Pooling
2.12.4. dbcp2
2.12.5. bonecp
2.12.6. dbcp2
2.13. Spring boot with MongoDB
2.13.1. Maven
2.13.2. Application
2.13.3. MongoTemplate
2.13.4. Repository
2.14. Spring boot with MySQL
2.14.1. Maven
2.14.2. Resource
2.14.3. Application
2.14.4. JdbcTemplate
2.14.5. CrudRepository
2.15. Spring boot with Oracle
2.15.1. Maven
2.15.2. application.properties
2.15.3. Application
2.15.4. CrudRepository
2.15.5. JdbcTemplate
2.15.6. Controller
2.16. Spring boot with PostgreSQL
2.16.1. pom.xml
2.16.2. application.properties
2.16.3. Application
2.16.4. CrudRepository
2.16.5. JdbcTemplate
2.16.6. Controller
2.16.7. Test
2.17. Spring boot with Email
2.17.1. Maven
2.17.2. Resource
2.17.3. POJO
2.17.4. RestController
2.17.5. Test
2.18. Spring boot with Velocity template
2.18.1. Maven
2.18.2. Resource
2.18.3. Application
2.18.4. RestController
2.18.5. Test
2.19. Spring boot with Thymeleaf
2.19.1. Maven
2.19.2. application.properties
2.19.3. Controller
2.19.4. HTML5 Template
2.20. Spring boot with Spring security
2.20.1. Maven
2.20.2. Reource
2.20.3. Application
2.20.4. WebSecurityConfigurer
2.20.5. RestController
2.20.6. 测试
2.20.7. Spring + Security + MongoDB
2.20.7.1. Account
2.20.7.2. AccountRepository
2.20.7.3. WebSecurityConfiguration
2.21. Spring boot with Oauth2
2.21.1. Maven
2.21.2. Password tools
2.21.3. Server
2.21.3.1. Maven
2.21.3.2. application.properties
2.21.3.3. EnableAuthorizationServer
2.21.3.4. EnableResourceServer
2.21.3.5. Entity Table
2.21.3.6. UserRepository
2.21.3.7. UserService
2.21.3.8. TestRestController
2.21.3.9. 数据库初始化
2.21.3.10. Test
2.21.4. Spring boot with Oauth2 RestTemplate
2.21.4.1. Maven
2.21.4.2. OAuth2ClientConfiguration.java
2.21.4.3. Application.java
2.21.4.4. application.properties
2.21.4.5. Controller
2.21.4.6. Test
2.22. Spring boot with Scheduling
2.22.1. Application.java
2.22.2. Component
2.22.3. 查看日志
2.22.4. 计划任务控制
2.23. Spring boot with Cache
2.23.1. Spring boot with Redis
2.23.1.1. maven
2.23.1.2. application.properties
2.23.1.3. JUnit
2.23.1.4. Controller
2.23.2. @Cacheable
2.23.2.1. maven
2.23.2.2. Controller
2.24. Spring boot with Hessian
2.24.1. Maven
2.24.2. Application
2.24.3. HessianServiceExporter
2.24.4. Service
2.24.5. RestController
2.25. Spring boot with Apache Kafka
2.25.1. 安装 kafka
2.25.2. maven
2.25.3. Spring boot Application
2.25.4. EnableKafka
2.25.5. KafkaListener
2.25.6. 测试
2.25.7. 完整的发布订阅实例
2.26. Spring boot with Git version
2.26.1. CommonRestController 公共控制器
2.26.2. VersionRestController 测试控制器
2.26.3. 创建 .gitattributes 文件
2.27. Spring boot with HTTPS SSL
2.27.1. 生成自签名证书
2.27.2. application.properties 配置文件
2.27.3. 启动 Spring boot
2.27.4. restTemplate 调用实例
2.28. Spring boot with Elasticsearch 2.x
2.28.1. Maven
2.28.2. Application
2.28.3. application.properties
2.28.4. Domain
2.28.5. ElasticsearchRepository
2.29. Spring boot with Elasticsearch 5.5.x
2.29.1. Maven
2.29.2. Application
2.29.3. application.properties
2.29.4. ElasticsearchConfiguration
2.29.5. RestController
2.30. Spring boot with Apache Hive
2.30.1. Maven
2.30.2. application.properties
2.30.3. Configuration
2.30.4. CURD 操作实例
2.31. Spring boot with Phoenix
2.31.1. Maven
2.31.2. application.properties
2.31.3. Configuration
2.32. Spring boot with Data restful
2.32.1. Maven
2.33. Spring boot with ELK(Elasticsearch + Logstash + Kibana)
2.33.1. TCP 方案
2.33.2. Redis 方案
2.33.3. Kafka 方案
2.33.4. Other
2.34. Spring boot with Docker
2.34.1.
2.34.2.
2.34.3.
2.34.4.
2.35. SpringBootTest
2.35.1.
2.36. FAQ
2.36.1. application.properties 参考
2.36.2. org.hibernate.dialect.Oracle10gDialect does not support identity key generation
2.36.3. No identifier specified for entity
2.36.4. Could not read document: Invalid UTF-8 middle byte 0xd0
2.36.5. java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
2.36.6. Field javaMailSender in cn.netkiller.rest.EmailRestController required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
2.36.7. org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "172.16.0.3", user "test", database "test ", SSL off
2.36.8. Spring boot 怎样显示执行的SQL语句
2.36.9. Cannot determine embedded database driver class for database type NONE
3. Spring MVC
3.1. @Controller
3.1.1. @RequestMapping
3.1.1.1. @RequestMapping("/")
3.1.1.2. 映射多个URL
3.1.1.3. headers
3.1.1.4. @GetMapping
3.1.1.5. @PostMapping
3.1.2. RequestMapping with Path Variables - @PathVariable
3.1.2.1. URL 参数传递
3.1.2.2. URL 传递 Date 类型
3.1.2.3. 处理特殊字符
3.1.2.4. @PathVariable 注意事项
3.1.3. RequestMapping with Request Parameters - @RequestParam
3.1.3.1. HTTP GET
3.1.3.2. HTTP POST
3.1.3.3. @RequestParam 传递特殊字符串
3.1.3.4. 传递日期参数
3.1.4. @RequestBody
3.1.4.1. @RequestBody 传递 List
3.1.4.2. 传递 Map 数据
3.1.5. @ModelAttribute
3.1.6. @CookieValue
3.1.7. @RequestHeader
3.1.8. @@SessionAttributes
3.1.9. @ResponseBody
3.1.9.1. 直接返回HTML
3.1.10. ModelAndView
3.1.10.1. 变量传递
3.1.10.2. ModelMap 传递多个变量
3.1.10.3. redirect
3.1.10.4. ArrayList
3.1.10.5. HashMap
3.1.10.6. 传递对象
3.1.10.7.
3.1.11. @CrossOrigin
3.1.12. @SessionAttributes
3.1.13. HttpServletRequest / HttpServletResponse
3.1.13.1. 用于方法参数
3.1.13.2. 注入方式
3.2. @RestController
3.2.1. 返回实体
3.2.2. JSON
3.2.3. XML
3.2.4. 兼容传统 json 接口
3.2.5. @PageableDefault 分页
3.3. @Scheduled 计划任务
3.3.1. 每3秒钟一运行一次
3.3.2. 凌晨23点运行
3.4. View
3.4.1. Using Spring’s form tag library
3.4.1.1. css
3.4.1.1.1. cssClass
3.4.1.1.2. cssStyle
3.4.1.1.3. cssErrorClass
3.4.1.2. cssClass
3.4.2. Thymeleaf
3.4.2.1. Maven pom.xml
3.4.2.2. Spring 配置
3.4.2.3. controller
3.4.2.4. HTML5 Template
3.4.3. FreeMarker
3.5. Properties
3.5.1. 载入*.properties文件
3.5.2. @Value 注解
3.5.3. @PropertySource 注解
3.6. FAQ
3.6.1. o.s.web.servlet.PageNotFound
3.6.2. HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
3.6.3. 同时使用 Thymeleaf 与 JSP
3.6.4. 排除静态内容
3.6.5. HTTP Status 406
3.6.6. Caused by: java.lang.IllegalArgumentException: Not a managed type: class common.domain.Article
4. RestTemplate - Spring4 Restful
4.1. RestTemplate Example
4.1.1. pom.xml
4.1.2. web.xml
4.1.3. springframework.xml
4.1.4. RestController
4.1.5. POJO
4.1.6. 测试
4.2. getForObject
4.2.1. 返回字符串
4.2.2. 传递 GET 参数
4.3. postForObject
4.3.1. 传递对象
4.3.2. 传递数据结构 MultiValueMap
4.4. postForEntity
4.5. PUT
4.6. Delete
4.7. HTTP Auth
4.7.1. Client
4.8. PKCS12
5. Spring Data
5.1. Spring Data Redis
5.1.1. pom.xml
5.1.2. springframework-servlet.xml
5.1.3. Controller
5.1.4. index.jsp
5.1.5. 删除 key
5.1.6. 测试
5.1.7. ZSET 数据类型
5.1.8. Hash
5.1.9. List
5.2. Spring Data MongoDB
5.2.1. Example Spring Data MongoDB
5.2.1.1. pom.xml
5.2.1.2. springframework-servlet.xml
5.2.1.3. POJO
5.2.1.4. Controller
5.2.1.5. 查看测试结果
5.2.1.6. 条件查询
5.2.2. @Document
5.2.2.1. @Indexed
5.2.2.1.1. 普通索引
5.2.2.1.2. 唯一索引
5.2.2.2. @DateTimeFormat
5.2.2.3. @NumberFormat
5.2.2.4. 在 @Document 中使用 Enum 类型
5.2.2.5. 在 @Document 中定义数据结构 List/Map
5.2.3. MongoRepository
5.2.3.1. findAll()
5.2.3.2. deleteAll()
5.2.3.3. save()
5.2.3.4. count()
5.2.3.5. findByXXXX
5.2.3.6. StartingWith 和 EndingWith
5.2.3.7. Between
5.2.3.8. PageRequest
5.2.3.9. @Query
5.2.4. mongoTemplate
5.2.4.1. is
5.2.4.2. Regex 正则表达式搜索
5.2.4.3. lt 和 gt
5.2.4.4. between
5.2.4.5. Criteria
5.2.4.6. Sort
5.2.4.7. Query + PageRequest
5.2.4.8. newAggregation
5.3. Spring Data JPA
5.3.1. @Entity
5.3.1.1. @Id
5.3.1.2. @Table
5.3.1.3. @Column
5.3.1.3.1. 字段长度
5.3.1.3.2. 浮点型
5.3.1.3.3. 创建于更新控制
5.3.1.4. @NotNull 不能为空声明
5.3.1.5. @DateTimeFormat 处理日期时间格式
5.3.1.6. 默认时间规则
5.3.1.6.1. CreatedDate
5.3.1.6.2. 与时间日期有关的 hibernate 注解
5.3.1.6.2.1. 设置默认时间
5.3.1.6.2.2. 创建时间
5.3.1.6.2.3. 更新时间
5.3.1.6.3. 数据库级别的默认创建日期时间定义
5.3.1.6.4. 数据库级别的默认创建日期与更新时间定义
5.3.1.6.5. 最后修改时间
5.3.1.7. 索引
5.3.1.8. Enum 枚举数据类型
5.3.1.8.1. 实体中处理 enum 类型
5.3.1.8.2. 数据库枚举类型
5.3.1.9. 整形数据类型
5.3.1.10. @JoinColumn
5.3.1.11. @OneToOne
5.3.1.12. OneToMany 一对多
5.3.1.13. ManyToMany 多对多
5.3.1.14. 外键级联删除
5.3.1.15. 其他
5.3.1.15.1. Cascade
5.3.1.15.2. @JsonIgnore
5.3.2. 实体集成
5.3.3. Repository
5.3.3.1. 传递枚举参数
5.3.3.2. 翻页操作
5.3.3.3. 排序操作操作
5.3.3.4. OrderBy
5.3.3.5. Query
5.3.3.5.1. 参数传递
5.3.3.6. @Transactional
5.3.3.6.1. 删除更新需要 @Transactional 注解
5.3.3.6.2. 回滚操作
5.3.4. CrudRepository
5.3.5. JpaRepository
5.4. EntityManager
5.5. JdbcTemplate
5.5.1. execute
5.5.2. queryForInt
5.5.3. queryForObject
5.5.3.1. 返回整形与字符型
5.5.3.2. 查询 Double 类型数据库
5.5.3.3. 返回日期
5.5.3.4. 返回结果集
5.5.3.5. 通过 "?" 向SQL传递参数
5.5.3.6. RowMapper 记录映射
5.5.4. queryForList
5.5.5. queryForMap
5.5.6. query
5.5.6.1. ResultSet
5.5.6.2. ResultSetExtractor
5.5.6.3. RowMapper
5.5.7. queryForRowSet
5.5.8. update
5.6. Elasticsearch
5.6.1. 内嵌 Elasticsearch
5.6.1.1. Maven
5.6.1.2. src/main/resources/application.properties
5.6.1.3. Domain Class
5.6.1.4. ElasticsearchRepository
5.6.1.5. SearchRestController
5.6.1.6. 测试
5.6.2. 集群模式
5.6.3. Document
5.6.4. Elasticsearch 删除操作
5.6.5. FAQ
5.6.5.1. java.lang.IllegalStateException: Received message from unsupported version: [2.0.0] minimal compatible version is: [5.0.0]
5.7. Spring Data FAQ
5.7.1. No identifier specified for entity
5.7.2. Oracle Date 类型显示日期和时间
5.7.3. java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
5.7.4. Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
6. Spring Security
6.1. 配置 Spring Security
6.1.1. 禁用 Security
6.2. EnableWebSecurity
6.2.1. 访问控制列表(Access Control List,ACL)
6.2.2. WebSecurity
6.2.3. HTTP Auth
6.2.4. Rest
6.2.5. hasRole
6.2.6. Add User
6.3. 获取当前用户
6.4. HTTP 安全
7. Interceptor
7.1. WebMvcConfigurerAdapter
7.2. HandlerInterceptor
8. Spring Cloud
8.1. Spring Cloud Config
8.1.1. Git 仓库
8.1.1.1. 分支
8.1.1.2. HTTP Auth
8.1.1.3. basedir
8.1.1.4. 本地git仓库
8.1.2. Server
8.1.2.1. Maven
8.1.2.2. Application
8.1.2.3. application.properties
8.1.2.4. 测试服务器
8.1.3. Client
8.1.3.1. Maven pom.xml
8.1.3.2. Application
8.1.3.3. bootstrap.properties
8.1.3.4. 测试 client
8.1.4. Config server 用户认证
8.1.4.1. Server 配置
8.1.4.1.1. application.properties
8.1.4.1.2. Maven
8.1.4.1.3. 测试是否生效
8.1.4.2. Client 配置
8.1.5. 加密敏感数据
8.1.6. Old

Maven pom.xml 请使用最新版本 1.3.1, 下面的 maven 是早期 Camden.SR5 版本的配置

			
<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>netkiller.cn</groupId>
	<artifactId>cloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Neo</name>
	<description>http://www.netkiller.cn</description>
	<packaging>jar</packaging>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
			
			

Maven pom.xml Camden.SR5 为早期版本,尽可以使用新版

			
<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>netkiller.cn</groupId>
	<artifactId>cloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>			
			
			
8.2. Spring Cloud Netflix
8.2.1. Eureka Server
8.2.1.1. Maven
8.2.1.2. Application
8.2.1.3. application.properties
8.2.1.4. 检查注册服务器
8.2.1.5. 为 Eureka Server 增加用户认证
8.2.1.5.1. Maven
8.2.1.5.2. application.properties
8.2.1.5.3. Eureka Client
8.2.1.5.4. Feign Client
8.2.2. Eureka Client
8.2.2.1. Maven
8.2.2.2. Application
8.2.2.3. RestController
8.2.2.4. application.properties
8.2.2.5. 测试
8.2.3. Feign client
8.2.3.1. Maven
8.2.3.2. Application
8.2.3.3. interface
8.2.3.4. application.properties
8.2.3.5. 测试
8.2.3.6. fallback
8.2.4. Zuul
8.2.4.1. Maven
8.2.4.2. EnableZuulProxy
8.2.4.3. application.yml
8.2.4.4. 负载均衡配置
8.3. FAQ
8.3.1. Feign请求超时
8.3.2. 已停止的微服务节点注销慢或不注销
8.3.3. Eureka Client (Dalston.SR1)
8.3.3.1. Maven
8.3.3.2. Application
8.3.3.3. RestController
8.3.3.4. application.properties
8.3.3.5. 测试
8.3.4. Feign 启动出错 PathVariable annotation was empty on param 0.
8.3.5. Feign 提示 Consider defining a bean of type 'common.feign.Cms' in your configuration.
9. Tomcat Spring 运行环境
9.1. Maven
9.2. Spring Boot Quick start
9.2.1. pom.xml
9.2.2. Controller
9.3. Spring MVC configuration
9.4. Tomcat
9.5. 集成 Mybatis
9.5.1. pom.xml
9.5.2. properties
9.5.3. dataSource
9.5.4. SqlSessionFactory
9.5.5. Mapper 扫描
9.5.6. Mapper 单一class映射
9.5.7. Service
9.5.8. 测试实例