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

部分 I. Spring Framework

目錄

4. Spring Tool Suite
4.1. 安裝
4.2. Dashboard
5. Spring Boot
5.1. Spring Boot Quick start
5.1.1. pom.xml
5.1.2. Controller
5.2. 運行 Spring boot 項目
5.2.1. Maven run
5.2.2. Linux systemd
5.2.3. 傳統 init.d 腳本
5.3. application.properties
5.3.1. PID FILE
5.3.2. 內嵌 tomcat server
5.3.2.1. server.tomcat.basedir
5.3.2.2. server
5.3.2.3. tomcat
5.3.2.4. logging
5.3.2.5. charset
5.3.2.6. Session 配置
5.3.2.7. cookie
5.3.2.8. error 路徑
5.3.2.9. 壓縮傳輸
5.3.2.10. server.servlet.context-path
5.3.2.11. ssl
5.3.3. JSON
5.3.4. SMTP 相關配置
5.3.5. Redis
5.3.6. MongoDB
5.3.7. MySQL
5.3.8. Oracle
5.3.9. default_schema
5.3.10. datasource
5.3.11. velocity
5.3.12.
5.4. SpringApplication
5.4.1. @SpringBootApplication
5.4.2. @EnableAutoConfiguration
5.4.3. @ComponentScan
5.4.4. @EntityScan 實體掃瞄
5.4.5. CharacterEncodingFilter
5.4.6. 隱藏 Banner
5.4.7. CORS 跨域請求
5.4.8. 實體與倉庫掃瞄
5.4.9. 列出 Beans
5.5. 編譯用於Tomcat的 War
5.6. Properties
5.6.1. @EnableConfigurationProperties 引用自定義 *.properties 配置檔案
5.6.2. spring.profiles.active 參數切換配置檔案
5.6.3. SpringApplicationBuilder.properties() 方法添加配置項
5.7. Service
5.7.1. Application
5.7.2. 定義介面
5.7.3. 實現介面
5.7.4. 調用 Service
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. String boot with RestTemplate
5.9.1. 在控製器中完整實例
5.10. Spring boot with Logging
5.10.1. 打印日誌
5.10.2. 日誌輸出級別
5.10.3. 禁止控制台輸出日誌
5.10.4. 標準輸出
5.10.5. 日誌分割
5.10.6. 日誌寫入 MongoDB
5.11. Spring boot with Session share
5.11.1. Redis
5.11.1.1. Maven
5.11.1.2. application.properties
5.11.1.3. Application
5.11.2. 測試 Session
5.11.3. JDBC
5.12. Connection and Statement Pooling
5.12.1. org.apache.tomcat.jdbc.pool.DataSource
5.12.2. druid
5.12.3. c3p0 - JDBC3 Connection and Statement Pooling
5.12.4. dbcp2
5.12.5. bonecp
5.12.6. dbcp2
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. Spring boot with Email
5.17.1. Maven
5.17.2. Resource
5.17.3. POJO
5.17.4. RestController
5.17.5. Test
5.18. Spring boot with Velocity template
5.18.1. Maven
5.18.2. Resource
5.18.3. Application
5.18.4. RestController
5.18.5. Test
5.19. Spring boot with Thymeleaf
5.19.1. Maven
5.19.2. application.properties
5.19.3. Controller
5.19.4. HTML5 Template
5.20. Spring boot with Spring security
5.20.1. Maven
5.20.2. Reource
5.20.3. Application
5.20.4. WebSecurityConfigurer
5.20.5. RestController
5.20.6. 測試
5.20.7. Spring + Security + MongoDB
5.20.7.1. Account
5.20.7.2. AccountRepository
5.20.7.3. WebSecurityConfiguration
5.21. Spring boot with Oauth2
5.21.1. Maven
5.21.2. Password tools
5.21.3. Server
5.21.3.1. Maven
5.21.3.2. application.properties
5.21.3.3. EnableAuthorizationServer
5.21.3.4. EnableResourceServer
5.21.3.5. Entity Table
5.21.3.6. UserRepository
5.21.3.7. UserService
5.21.3.8. TestRestController
5.21.3.9. 資料庫初始化
5.21.3.10. Test
5.21.4. Spring boot with Oauth2 RestTemplate
5.21.4.1. Maven
5.21.4.2. OAuth2ClientConfiguration.java
5.21.4.3. Application.java
5.21.4.4. application.properties
5.21.4.5. Controller
5.21.4.6. Test
5.22. Spring boot with Scheduling
5.22.1. Application.java
5.22.2. Component
5.22.3. 查看日誌
5.22.4. 計劃任務控制
5.23. Spring boot with Cache
5.23.1. Spring boot with Redis
5.23.1.1. maven
5.23.1.2. application.properties
5.23.1.3. JUnit
5.23.1.4. Controller
5.23.2. @Cacheable
5.23.2.1. maven
5.23.2.2. Controller
5.24. Spring boot with Hessian
5.24.1. Maven
5.24.2. Application
5.24.3. HessianServiceExporter
5.24.4. Service
5.24.5. RestController
5.25. Spring boot with Apache Kafka
5.25.1. 安裝 kafka
5.25.2. maven
5.25.3. Spring boot Application
5.25.4. EnableKafka
5.25.5. KafkaListener
5.25.6. 測試
5.25.7. 完整的發佈訂閲實例
5.26. Spring boot with Git version
5.26.1. CommonRestController 公共控製器
5.26.2. VersionRestController 測試控製器
5.26.3. 創建 .gitattributes 檔案
5.27. Spring boot with HTTPS SSL
5.27.1. 生成自簽名證書
5.27.2. application.properties 配置檔案
5.27.3. 啟動 Spring boot
5.27.4. restTemplate 調用實例
5.28. Spring boot with Elasticsearch 2.x
5.28.1. Maven
5.28.2. Application
5.28.3. application.properties
5.28.4. Domain
5.28.5. ElasticsearchRepository
5.29. Spring boot with Elasticsearch 5.5.x
5.29.1. Maven
5.29.2. Application
5.29.3. application.properties
5.29.4. ElasticsearchConfiguration
5.29.5. RestController
5.30. Spring boot with Apache Hive
5.30.1. Maven
5.30.2. application.properties
5.30.3. Configuration
5.30.4. CURD 操作實例
5.31. Spring boot with Phoenix
5.31.1. Maven
5.31.2. application.properties
5.31.3. Configuration
5.32. Spring boot with Data restful
5.32.1. Maven
5.33. Spring boot with ELK(Elasticsearch + Logstash + Kibana)
5.33.1. TCP 方案
5.33.2. Redis 方案
5.33.3. Kafka 方案
5.33.4. Other
5.34. Spring boot with Docker
5.34.1.
5.34.2.
5.34.3.
5.34.4.
5.35. SpringBootTest
5.35.1.
5.36. FAQ
5.36.1. application.properties 參考
5.36.2. org.hibernate.dialect.Oracle10gDialect does not support identity key generation
5.36.3. No identifier specified for entity
5.36.4. Could not read document: Invalid UTF-8 middle byte 0xd0
5.36.5. java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
5.36.6. Field javaMailSender in cn.netkiller.rest.EmailRestController required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
5.36.7. org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "172.16.0.3", user "test", database "test ", SSL off
5.36.8. Spring boot 怎樣顯示執行的SQL語句
6. Spring MVC
6.1. @Controller
6.1.1. @RequestMapping
6.1.1.1. @RequestMapping("/")
6.1.1.2. 映射多個URL
6.1.1.3. headers
6.1.1.4. @GetMapping
6.1.1.5. @PostMapping
6.1.2. RequestMapping with Path Variables - @PathVariable
6.1.2.1. URL 參數傳遞
6.1.2.2. URL 傳遞 Date 類型
6.1.2.3. 處理特殊字元
6.1.2.4. @PathVariable 注意事項
6.1.3. RequestMapping with Request Parameters - @RequestParam
6.1.3.1. HTTP GET
6.1.3.2. HTTP POST
6.1.3.3. @RequestParam 傳遞特殊字元串
6.1.3.4. 傳遞日期參數
6.1.4. @RequestBody
6.1.4.1. @RequestBody 傳遞 List
6.1.4.2. 傳遞 Map 數據
6.1.5. @ModelAttribute
6.1.6. @CookieValue
6.1.7. @RequestHeader
6.1.8. @@SessionAttributes
6.1.9. @ResponseBody
6.1.9.1. 直接返回HTML
6.1.10. ModelAndView
6.1.10.1. 變數傳遞
6.1.10.2. ModelMap 傳遞多個變數
6.1.10.3. redirect
6.1.10.4. ArrayList
6.1.10.5. HashMap
6.1.10.6. 傳遞對象
6.1.10.7.
6.1.11. @CrossOrigin
6.1.12. @SessionAttributes
6.1.13. HttpServletRequest / HttpServletResponse
6.1.13.1. 用於方法參數
6.1.13.2. 注入方式
6.2. @RestController
6.2.1. 返回實體
6.2.2. JSON
6.2.3. XML
6.2.4. 兼容傳統 json 介面
6.2.5. @PageableDefault 分頁
6.3. @Scheduled 計劃任務
6.3.1. 每3秒鐘一運行一次
6.3.2. 凌晨23點運行
6.4. View
6.4.1. Using Spring’s form tag library
6.4.1.1. css
6.4.1.1.1. cssClass
6.4.1.1.2. cssStyle
6.4.1.1.3. cssErrorClass
6.4.1.2. cssClass
6.4.2. Thymeleaf
6.4.2.1. Maven pom.xml
6.4.2.2. Spring 配置
6.4.2.3. controller
6.4.2.4. HTML5 Template
6.4.3. FreeMarker
6.5. Properties
6.5.1. 載入*.properties檔案
6.5.2. @Value 註解
6.5.3. @PropertySource 註解
6.6. FAQ
6.6.1. o.s.web.servlet.PageNotFound
6.6.2. HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
6.6.3. 同時使用 Thymeleaf 與 JSP
6.6.4. 排除靜態內容
6.6.5. HTTP Status 406
6.6.6. Caused by: java.lang.IllegalArgumentException: Not a managed type: class common.domain.Article
7. RestTemplate - Spring4 Restful
7.1. RestTemplate Example
7.1.1. pom.xml
7.1.2. web.xml
7.1.3. springframework.xml
7.1.4. RestController
7.1.5. POJO
7.1.6. 測試
7.2. getForObject
7.2.1. 返回字元串
7.2.2. 傳遞 GET 參數
7.3. postForObject
7.3.1. 傳遞對象
7.3.2. 傳遞資料結構 MultiValueMap
7.4. postForEntity
7.5. PUT
7.6. Delete
7.7. HTTP Auth
7.7.1. Client
7.8. PKCS12
8. Spring Data
8.1. Spring Data Redis
8.1.1. pom.xml
8.1.2. springframework-servlet.xml
8.1.3. Controller
8.1.4. index.jsp
8.1.5. 刪除 key
8.1.6. 測試
8.1.7. ZSET 數據類型
8.1.8. Hash
8.1.9. List
8.2. Spring Data MongoDB
8.2.1. Example Spring Data MongoDB
8.2.1.1. pom.xml
8.2.1.2. springframework-servlet.xml
8.2.1.3. POJO
8.2.1.4. Controller
8.2.1.5. 查看測試結果
8.2.1.6. 條件查詢
8.2.2. @Document
8.2.2.1. @Indexed
8.2.2.1.1. 普通索引
8.2.2.1.2. 唯一索引
8.2.2.2. @DateTimeFormat
8.2.2.3. @NumberFormat
8.2.2.4. 在 @Document 中使用 Enum 類型
8.2.2.5. 在 @Document 中定義資料結構 List/Map
8.2.3. MongoRepository
8.2.3.1. findAll()
8.2.3.2. deleteAll()
8.2.3.3. save()
8.2.3.4. count()
8.2.3.5. findByXXXX
8.2.3.6. StartingWith 和 EndingWith
8.2.3.7. Between
8.2.3.8. PageRequest
8.2.3.9. @Query
8.2.4. mongoTemplate
8.2.4.1. is
8.2.4.2. Regex 正則表達式搜索
8.2.4.3. lt 和 gt
8.2.4.4. between
8.2.4.5. Criteria
8.2.4.6. Sort
8.2.4.7. Query + PageRequest
8.2.4.8. newAggregation
8.3. Spring Data JPA
8.3.1. @Entity
8.3.1.1. @Id
8.3.1.2. @Table
8.3.1.3. @Column
8.3.1.3.1. 欄位長度
8.3.1.3.2. 浮點型
8.3.1.3.3. 創建於更新控制
8.3.1.4. @NotNull 不能為空聲明
8.3.1.5. @DateTimeFormat 處理日期時間格式
8.3.1.6. 預設時間規則
8.3.1.6.1. CreatedDate
8.3.1.6.2. 與時間日期有關的 hibernate 註解
8.3.1.6.2.1. 設置預設時間
8.3.1.6.2.2. 創建時間
8.3.1.6.2.3. 更新時間
8.3.1.6.3. 資料庫級別的預設創建日期時間定義
8.3.1.6.4. 資料庫級別的預設創建日期與更新時間定義
8.3.1.6.5. 最後修改時間
8.3.1.7. 索引
8.3.1.8. Enum 枚舉數據類型
8.3.1.8.1. 實體中處理 enum 類型
8.3.1.8.2. 資料庫枚舉類型
8.3.1.9. 整形數據類型
8.3.1.10. @JoinColumn
8.3.1.11. @OneToOne
8.3.1.12. OneToMany 一對多
8.3.1.13. ManyToMany 多對多
8.3.1.14. 外鍵級聯刪除
8.3.1.15. 其他
8.3.1.15.1. Cascade
8.3.1.15.2. @JsonIgnore
8.3.2. 實體整合
8.3.3. Repository
8.3.3.1. 傳遞枚舉參數
8.3.3.2. 翻頁操作
8.3.3.3. 排序操作操作
8.3.3.4. OrderBy
8.3.3.5. Query
8.3.3.5.1. 參數傳遞
8.3.3.6. @Transactional
8.3.3.6.1. 刪除更新需要 @Transactional 註解
8.3.3.6.2. 回滾操作
8.3.4. CrudRepository
8.3.5. JpaRepository
8.4. EntityManager
8.5. JdbcTemplate
8.5.1. execute
8.5.2. queryForInt
8.5.3. queryForObject
8.5.3.1. 返回整形與字元型
8.5.3.2. 查詢 Double 類型資料庫
8.5.3.3. 返回日期
8.5.3.4. 返回結果集
8.5.3.5. 通過 "?" 向SQL傳遞參數
8.5.3.6. RowMapper 記錄映射
8.5.4. queryForList
8.5.5. queryForMap
8.5.6. query
8.5.6.1. ResultSet
8.5.6.2. ResultSetExtractor
8.5.6.3. RowMapper
8.5.7. queryForRowSet
8.5.8. update
8.6. Elasticsearch
8.6.1. 內嵌 Elasticsearch
8.6.1.1. Maven
8.6.1.2. src/main/resources/application.properties
8.6.1.3. Domain Class
8.6.1.4. ElasticsearchRepository
8.6.1.5. SearchRestController
8.6.1.6. 測試
8.6.2. 集群模式
8.6.3. Document
8.6.4. Elasticsearch 刪除操作
8.6.5. FAQ
8.6.5.1. java.lang.IllegalStateException: Received message from unsupported version: [2.0.0] minimal compatible version is: [5.0.0]
8.7. Spring Data FAQ
8.7.1. No identifier specified for entity
8.7.2. Oracle Date 類型顯示日期和時間
8.7.3. java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
8.7.4. Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
9. Spring Security
9.1. 配置 Spring Security
9.1.1. 禁用 Security
9.2. EnableWebSecurity
9.2.1. 訪問控制列表(Access Control List,ACL)
9.2.2. WebSecurity
9.2.3. HTTP Auth
9.2.4. Rest
9.2.5. hasRole
9.2.6. Add User
9.3. 獲取當前用戶
9.4. HTTP 安全
10. Interceptor
10.1. WebMvcConfigurerAdapter
10.2. HandlerInterceptor
11. Spring Cloud
11.1. Spring Cloud Config
11.1.1. Git 倉庫
11.1.1.1. 分支
11.1.1.2. HTTP Auth
11.1.1.3. basedir
11.1.1.4. 本地git倉庫
11.1.2. Server
11.1.2.1. Maven
11.1.2.2. Application
11.1.2.3. application.properties
11.1.2.4. 測試伺服器
11.1.3. Client
11.1.3.1. Maven pom.xml
11.1.3.2. Application
11.1.3.3. bootstrap.properties
11.1.3.4. 測試 client
11.1.4. Config server 用戶認證
11.1.4.1. Server 配置
11.1.4.1.1. application.properties
11.1.4.1.2. Maven
11.1.4.1.3. 測試是否生效
11.1.4.2. Client 配置
11.1.5. 加密敏感數據
11.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>			
			
			
11.2. Spring Cloud Netflix
11.2.1. Eureka Server
11.2.1.1. Maven
11.2.1.2. Application
11.2.1.3. application.properties
11.2.1.4. 檢查註冊伺服器
11.2.1.5. 為 Eureka Server 增加用戶認證
11.2.1.5.1. Maven
11.2.1.5.2. application.properties
11.2.1.5.3. Eureka Client
11.2.1.5.4. Feign Client
11.2.2. Eureka Client
11.2.2.1. Maven
11.2.2.2. Application
11.2.2.3. RestController
11.2.2.4. application.properties
11.2.2.5. 測試
11.2.3. Feign client
11.2.3.1. Maven
11.2.3.2. Application
11.2.3.3. interface
11.2.3.4. application.properties
11.2.3.5. 測試
11.2.3.6. fallback
11.2.4. Zuul
11.2.4.1. Maven
11.2.4.2. EnableZuulProxy
11.2.4.3. application.yml
11.2.4.4. 負載均衡配置
11.3. FAQ
11.3.1. Feign請求超時
11.3.2. 已停止的微服務節點註銷慢或不註銷
11.3.3. Eureka Client (Dalston.SR1)
11.3.3.1. Maven
11.3.3.2. Application
11.3.3.3. RestController
11.3.3.4. application.properties
11.3.3.5. 測試
11.3.4. Feign 啟動出錯 PathVariable annotation was empty on param 0.
11.3.5. Feign 提示 Consider defining a bean of type 'common.feign.Cms' in your configuration.
12. Tomcat Spring 運行環境
12.1. Maven
12.2. Spring Boot Quick start
12.2.1. pom.xml
12.2.2. Controller
12.3. Spring MVC configuration
12.4. Tomcat
12.5. 整合 Mybatis
12.5.1. pom.xml
12.5.2. properties
12.5.3. dataSource
12.5.4. SqlSessionFactory
12.5.5. Mapper 掃瞄
12.5.6. Mapper 單一class映射
12.5.7. Service
12.5.8. 測試實例