springboot用法和与各种框架、组件等结合使用,包括springboot的注解分析、路径分析、取得内置对象、项目打包、配置环境属性、读取资源文件、Bean配置、模板渲染、基于yml的多profile配置、基于properties的多profile、添加数据验证、配置错误页、全局异常处理、配置Tomcat运行、配置https访问路径、信息显示、内置对象、显示对象信息、文件上传、使用文件服务器、拦截器、配置druid数据库连接池、整合MyBatis框架、事务控制、Druid监控、整合ActiveMQ、整合RabbitMQ、整合kafka、邮件发送、定时调度、Actuator监控、整合Redis数据库、整合Restfui框架、整合Shiro

This commit is contained in:
foreveriss
2017-06-29 23:46:06 +08:00
commit 0e66e801f4
1205 changed files with 17740 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/view">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
+35
View File
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>microboot-upload</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/main/view=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="cloudfoundry.standalone.app" version="1.0"/>
</faceted-project>
+58
View File
@@ -0,0 +1,58 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.mldn</groupId>
<artifactId>microboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microboot-upload</artifactId>
<name>microboot-upload</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.github.kischang</groupId>
<artifactId>fastdfs-client</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,11 @@
package cn.mldn.microboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.multipart.MultipartFile;
@SpringBootApplication // 启动SpringBoot程序,而后自带子包扫描
public class StartSpringBootMain {
public static void main(String[] args) throws Exception {
SpringApplication.run(StartSpringBootMain.class, args);
}
}
@@ -0,0 +1,19 @@
package cn.mldn.microboot.config;
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UploadConfig {
@Bean
public MultipartConfigElement getMultipartConfig() {
MultipartConfigFactory config = new MultipartConfigFactory() ;
config.setMaxFileSize("10MB"); // 设置上传文件的单个大小限制
config.setMaxRequestSize("100MB"); // 设置总的上传的大小限制
config.setLocation("/"); // 设置临时保存目录
return config.createMultipartConfig() ; // 创建一个上传配置
}
}
@@ -0,0 +1,103 @@
package cn.mldn.microboot.controller;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ProtoCommon;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import cn.mldn.microboot.util.controller.AbstractBaseController;
@Controller
public class UploadController extends AbstractBaseController {
@RequestMapping(value = "/uploadPre", method = RequestMethod.GET)
public String uploadPre() { // 通过model可以实现内容的传递
return "upload_page";
}
@RequestMapping(value = "/show", method = RequestMethod.GET)
public String show(String groupId, String fileId,Model model) throws Exception {
// 通过ClassPath路径获取要使用的配置文件
ClassPathResource classPathResource = new ClassPathResource(
"fastdfs_client.conf");
// 进行客户端访问的整体配置,需要知道配置文件的完整路径
ClientGlobal.init(classPathResource.getClassLoader()
.getResource("fastdfs_client.conf").getPath());
// FastDFS的核心操作在于tracker处理上,所以此时需要定义Tracker客户端
TrackerClient trackerClient = new TrackerClient();
// 定义TrackerServer的配置信息
TrackerServer trackerServer = trackerClient.getConnection();
int ts = (int) (System.currentTimeMillis() / 1000);// 时间参考
StringBuffer fileUrl = new StringBuffer();
fileUrl.append("http://");
fileUrl.append("fastdfs-tracker");
fileUrl.append("/" + groupId + "/").append(fileId);
fileUrl.append("?token=").append(
ProtoCommon.getToken(fileId, ts, ClientGlobal.g_secret_key));
fileUrl.append("&ts=").append(ts);
System.out.println(fileUrl);
trackerServer.close();
model.addAttribute("image", fileUrl) ;
return "upload_show" ;
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String upload(String name, HttpServletRequest request)
throws Exception {
if (request instanceof MultipartHttpServletRequest) { // 如果你现在是MultipartHttpServletRequest对象
MultipartHttpServletRequest mrequest = (MultipartHttpServletRequest) request;
List<MultipartFile> files = mrequest.getFiles("photo");
Iterator<MultipartFile> iter = files.iterator();
while (iter.hasNext()) {
MultipartFile photo = iter.next();
if (photo != null) { // 现在有文件上传
// 如果要想进行上传则一定需要获取到文件的扩展名称
String fileExtName = photo.getContentType().substring(
photo.getContentType().lastIndexOf("/") + 1);
// 通过ClassPath路径获取要使用的配置文件
ClassPathResource classPathResource = new ClassPathResource(
"fastdfs_client.conf");
// 进行客户端访问的整体配置,需要知道配置文件的完整路径
ClientGlobal.init(classPathResource.getClassLoader()
.getResource("fastdfs_client.conf").getPath());
// FastDFS的核心操作在于tracker处理上,所以此时需要定义Tracker客户端
TrackerClient trackerClient = new TrackerClient();
// 定义TrackerServer的配置信息
TrackerServer trackerServer = trackerClient.getConnection();
// 在整个FastDFS之中真正负责干活的就是Storage
StorageServer storageServer = null;
StorageClient1 storageClient = new StorageClient1(
trackerServer, storageServer);
// 定义上传文件的元数据
NameValuePair[] metaList = new NameValuePair[3];
metaList[0] = new NameValuePair("fileName",
photo.getOriginalFilename());
metaList[1] = new NameValuePair("fileExtName", fileExtName);
metaList[2] = new NameValuePair("fileLength",
String.valueOf(photo.getSize()));
// 如果要上传则使用trackerClient对象完成
String upload_file = storageClient.upload_file1(
photo.getBytes(), fileExtName, metaList);
System.out.println(upload_file);
trackerServer.close();
}
}
}
return "upload-file";
}
}
@@ -0,0 +1,15 @@
package cn.mldn.microboot.util.controller;
import java.util.Locale;
import javax.annotation.Resource;
import org.springframework.context.MessageSource;
public abstract class AbstractBaseController {
@Resource
private MessageSource messageSource; // 自动注入此资源对象
public String getMessage(String key, String... args) {
return this.messageSource.getMessage(key, args, Locale.getDefault());
}
}
@@ -0,0 +1,44 @@
package cn.mldn.microboot.vo;
import java.io.Serializable;
import java.util.Date;
@SuppressWarnings("serial")
public class Member implements Serializable {
private Long mid ;
private String name ;
private Integer age ;
private Date birthday ;
private Double salary ;
public Long getMid() {
return mid;
}
public void setMid(Long mid) {
this.mid = mid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
@@ -0,0 +1,12 @@
server:
port: 80
spring:
messages:
basename: i18n/Messages,i18n/Pages
# http:
# multipart:
# enabled: true # 可以启用http上传
# max-file-size: 1KB # 设置支持的单个上传文件的大小限制
# max-request-size: 20MB # 设置最大的请求的文件大小,设置总体大小请求
# file-size-threshold: 512KB # 当上传文件达到指定配置量的时候会将文件内容写入磁盘
# location: / # 设置上传的临时目录
@@ -0,0 +1,6 @@
# 配置上你tracker的连接地址
tracker_server=fastdfs-tracker:22122
# 表示现在要进行token检测
http.anti_steal_token=true
# 进行认证的密码处理
http.secret_key=mldnhelloworldhahheihanhheiehinajueduicaibudao
@@ -0,0 +1,2 @@
welcome.url=www.mldn.cn
welcome.msg=\u6b22\u8fce{0}\u5149\u4e34\uff01
@@ -0,0 +1,2 @@
member.add.page=/pages/back/admin/member/member_add.jsp
member.add.action=/pages/back/admin/member/member_add.action
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@@ -0,0 +1,11 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<script type="text/javascript" th:src="@{js/main.js}"></script>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<h1>www.mldn.cn</h1>
</body>
</html>
@@ -0,0 +1,3 @@
window.onload = function() {
console.log("****** www.mldn.cn ******") ;
}
@@ -0,0 +1,10 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<h1>www.mldn.cn</h1>
</body>
</html>
@@ -0,0 +1,21 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<script type="text/javascript" th:src="@{/js/main.js}"></script>
<link rel="icon" type="image/x-icon" href="/images/mldn.ico"/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<form th:action="@{/upload}" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="name"/><br/>
照片:<input type="file" name="photo"/><br/>
照片:<input type="file" name="photo"/><br/>
照片:<input type="file" name="photo"/><br/>
<input type="submit" value="上传"/>
</form>
<a th:href="@{/show(groupId='group1',fileId='M00/00/00/wKhEtVlTFc2ANT4kAALWT3AMif079.jpeg')}">查看照片</a>
<a th:href="@{/show(groupId='group1',fileId='M00/00/00/wKhEtVlTFc2AUVINAAA9nfksZs0713.png')}">查看照片</a>
<a th:href="@{/show(groupId='group1',fileId='M00/00/00/wKhEtVlTFc2AOgxGAC-AAARLh5I67.jpeg')}">查看照片</a>
</body>
</html>
@@ -0,0 +1,12 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<script type="text/javascript" th:src="@{/js/main.js}"></script>
<link rel="icon" type="image/x-icon" href="/images/mldn.ico"/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<img th:src="${image}"/>
</body>
</html>
@@ -0,0 +1,38 @@
package cn.mldn.microboot;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
@@ -0,0 +1,19 @@
package cn.mldn.microboot.test;
import javax.annotation.Resource;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import cn.mldn.microboot.StartSpringBootMain;
import cn.mldn.microboot.controller.UploadController;
@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestMessageController {
@Resource
private UploadController messageController;
}
@@ -0,0 +1,5 @@
Manifest-Version: 1.0
Built-By: foreveriss
Build-Jdk: 1.8.0_121
Created-By: Maven Integration for Eclipse
@@ -0,0 +1,7 @@
#Generated by Maven Integration for Eclipse
#Thu Jun 29 23:45:23 CST 2017
version=0.0.1-SNAPSHOT
groupId=cn.mldn
m2e.projectName=microboot-upload
m2e.projectLocation=/Users/foreveriss/workspace/microboot/microboot-upload
artifactId=microboot-upload
@@ -0,0 +1,58 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.mldn</groupId>
<artifactId>microboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microboot-upload</artifactId>
<name>microboot-upload</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.github.kischang</groupId>
<artifactId>fastdfs-client</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,12 @@
server:
port: 80
spring:
messages:
basename: i18n/Messages,i18n/Pages
# http:
# multipart:
# enabled: true # 可以启用http上传
# max-file-size: 1KB # 设置支持的单个上传文件的大小限制
# max-request-size: 20MB # 设置最大的请求的文件大小,设置总体大小请求
# file-size-threshold: 512KB # 当上传文件达到指定配置量的时候会将文件内容写入磁盘
# location: / # 设置上传的临时目录
@@ -0,0 +1,6 @@
# 配置上你tracker的连接地址
tracker_server=fastdfs-tracker:22122
# 表示现在要进行token检测
http.anti_steal_token=true
# 进行认证的密码处理
http.secret_key=mldnhelloworldhahheihanhheiehinajueduicaibudao
@@ -0,0 +1,2 @@
welcome.url=www.mldn.cn
welcome.msg=\u6b22\u8fce{0}\u5149\u4e34\uff01
@@ -0,0 +1,2 @@
member.add.page=/pages/back/admin/member/member_add.jsp
member.add.action=/pages/back/admin/member/member_add.action
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@@ -0,0 +1,11 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<script type="text/javascript" th:src="@{js/main.js}"></script>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<h1>www.mldn.cn</h1>
</body>
</html>
@@ -0,0 +1,3 @@
window.onload = function() {
console.log("****** www.mldn.cn ******") ;
}
@@ -0,0 +1,10 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<h1>www.mldn.cn</h1>
</body>
</html>
@@ -0,0 +1,21 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<script type="text/javascript" th:src="@{/js/main.js}"></script>
<link rel="icon" type="image/x-icon" href="/images/mldn.ico"/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<form th:action="@{/upload}" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="name"/><br/>
照片:<input type="file" name="photo"/><br/>
照片:<input type="file" name="photo"/><br/>
照片:<input type="file" name="photo"/><br/>
<input type="submit" value="上传"/>
</form>
<a th:href="@{/show(groupId='group1',fileId='M00/00/00/wKhEtVlTFc2ANT4kAALWT3AMif079.jpeg')}">查看照片</a>
<a th:href="@{/show(groupId='group1',fileId='M00/00/00/wKhEtVlTFc2AUVINAAA9nfksZs0713.png')}">查看照片</a>
<a th:href="@{/show(groupId='group1',fileId='M00/00/00/wKhEtVlTFc2AOgxGAC-AAARLh5I67.jpeg')}">查看照片</a>
</body>
</html>
@@ -0,0 +1,12 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<script type="text/javascript" th:src="@{/js/main.js}"></script>
<link rel="icon" type="image/x-icon" href="/images/mldn.ico"/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<img th:src="${image}"/>
</body>
</html>