新增 spring boot 整合 servlet 用例
This commit is contained in:
parent
c45d1d3b7a
commit
85f4d1a591
40
README.md
40
README.md
@ -46,28 +46,24 @@ spring-cloud:Finchley.SR2
|
|||||||
|
|
||||||
## 2. spring-boot samples
|
## 2. spring-boot samples
|
||||||
|
|
||||||
| samples | 描述 | 官方文档 |
|
| samples | 描述 | 官方文档 |
|
||||||
| --------------------------- | ------------------------------ | ------------------------------------------------------------ |
|
| ----------------------- | ------------------------------ | ------------------------------------------------------------ |
|
||||||
| spring-boot-base | spring-boot 基础 | [spring boot 官方文档](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/) |
|
| spring-boot-base | spring-boot 基础 | [spring boot 官方文档](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/)<br>[spring boot 中文官方文档](https://www.breakyizhan.com/springboot/3028.html) |
|
||||||
| spring-boot-aop | spring aop | |
|
| spring-boot-cache | spring-boot 缓存 | |
|
||||||
| spring-boot-cache | spring-boot 缓存 | |
|
| spring-boot-yml-profile | yml 语法和多配置切换 | |
|
||||||
| spring-boot-profile | spring 场景切换 | |
|
| spring-boot-servlet | 整合servlet 3.0 | |
|
||||||
| spring-boot-servlet | 整合servlet 3.0 | |
|
| spring-boot-jpa | spring-boot jpa 的使用 | |
|
||||||
| spring-boot-test | spring-boot 单元测试 | |
|
| spring-boot-jsp | spring-boot 整合 jsp | |
|
||||||
| spring-boot-jpa | spring-boot jpa 的使用 | |
|
| spring-boot-mybatis | spring-boot 整合 mybatis | |
|
||||||
| spring-boot-freemarker | freemarker 的使用 | |
|
| spring-boot-mybtais | spring-boot 整合druid、mybatis | |
|
||||||
| spring-boot-jsp | spring-boot 整合 jsp | |
|
| spring-boot-redis | spring-boot 整合 redis | |
|
||||||
| spring-boot-mybatis | spring-boot 整合 mybatis | |
|
| spring-boot-mongodb | spring-boot 整合 mongodb | |
|
||||||
| spring-boot-druid-mybtais | spring-boot 整合druid、mybatis | |
|
| spring-boot-memcached | spring-boot 整合 memcached | |
|
||||||
| spring-boot-druid-redis | spring-boot 整合 redis | |
|
| spring-boot-rabbitmq | spring-boot 整合 rabbitmq | |
|
||||||
| spring-boot-druid-mongodb | spring-boot 整合 mongodb | |
|
| spring-boot-kafka | spring-boot 整合 kafka | |
|
||||||
| spring-boot-druid-memcached | spring-boot 整合 memcached | |
|
| spring-boot-dubbo | spring-boot 整合 dubbo | |
|
||||||
| spring-boot-druid-rabbitmq | spring-boot 整合 rabbitmq | |
|
| spring-boot-websocket | spring-boot 整合 websocket | |
|
||||||
| spring-boot-druid-kafka | spring-boot 整合 kafka | |
|
| spring-boot-email | spring-boot 邮件发送 | |
|
||||||
| spring-boot-druid-dubbo | spring-boot 整合 dubbo | |
|
|
||||||
| spring-boot-druid-websocket | spring-boot 整合 websocket | |
|
|
||||||
| spring-boot-druid-netty | spring-boot 整合 netty | |
|
|
||||||
| spring-boot-druid-scheduled | spring-boot 定时任务 | |
|
|
||||||
|
|
||||||
更多的场景和用例可参阅 [spring-boot 官方samples ](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples)
|
更多的场景和用例可参阅 [spring-boot 官方samples ](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples)
|
||||||
|
|
||||||
|
BIN
referenced documents/Thymeleaf中⽂参考⼿册.pdf
Normal file
BIN
referenced documents/Thymeleaf中⽂参考⼿册.pdf
Normal file
Binary file not shown.
59
spring-boot/spring-boot-base/pom.xml
Normal file
59
spring-boot/spring-boot-base/pom.xml
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.1.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.heibaiying</groupId>
|
||||||
|
<artifactId>spring-boot-base</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-base</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!--模板引擎-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!--web 启动器-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!--lombok 插件-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<!--测试相关依赖-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.heibaiying.springbootbase;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootBaseApplication {
|
||||||
|
|
||||||
|
// 启动器默认开启包扫描,扫描与主程序所在包及其子包,对于本工程而言 默认扫描 com.heibaiying.springbootbase
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootBaseApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.heibaiying.springbootbase.bean;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Programmer {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
private float salary;
|
||||||
|
|
||||||
|
private LocalDate birthday;
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.heibaiying.springbootbase.controller;
|
||||||
|
|
||||||
|
import com.heibaiying.springbootbase.bean.Programmer;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.ModelMap;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 跳转渲染模板引擎 默认模板的存放位置为classpath:templates
|
||||||
|
*/
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("freemarker")
|
||||||
|
public class FreeMarkerController {
|
||||||
|
|
||||||
|
@RequestMapping("show")
|
||||||
|
private String programmerShow(ModelMap modelMap){
|
||||||
|
List<Programmer> programmerList=new ArrayList<>();
|
||||||
|
programmerList.add(new Programmer("xiaoming",12,100000.00f,LocalDate.of(2019,Month.AUGUST,2)));
|
||||||
|
programmerList.add(new Programmer("xiaohong",23,900000.00f,LocalDate.of(2013,Month.FEBRUARY,2)));
|
||||||
|
modelMap.addAttribute("programmers",programmerList);
|
||||||
|
return "markerShow";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.heibaiying.springbootbase.controller;
|
||||||
|
|
||||||
|
import com.heibaiying.springbootbase.bean.Programmer;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : restful 控制器
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("restful")
|
||||||
|
public class RestfulController {
|
||||||
|
|
||||||
|
@GetMapping("programmers")
|
||||||
|
private List<Programmer> getProgrammers() {
|
||||||
|
List<Programmer> programmers = new ArrayList<>();
|
||||||
|
programmers.add(new Programmer("xiaoming", 12, 100000.00f, LocalDate.of(2019, Month.AUGUST, 2)));
|
||||||
|
programmers.add(new Programmer("xiaohong", 23, 900000.00f, LocalDate.of(2013, Month.FEBRUARY, 2)));
|
||||||
|
return programmers;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.heibaiying.springbootbase.controller;
|
||||||
|
|
||||||
|
import com.heibaiying.springbootbase.bean.Programmer;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.ModelMap;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.Month;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 跳转渲染模板引擎 默认模板的存放位置为classpath:templates
|
||||||
|
*/
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("thymeleaf")
|
||||||
|
public class ThymeleafController {
|
||||||
|
|
||||||
|
@RequestMapping("show")
|
||||||
|
private String programmerShow(ModelMap modelMap) {
|
||||||
|
List<Programmer> programmerList = new ArrayList<>();
|
||||||
|
programmerList.add(new Programmer("xiaoming", 12, 100000.00f, LocalDate.of(2019, Month.AUGUST, 2)));
|
||||||
|
programmerList.add(new Programmer("xiaohong", 23, 900000.00f, LocalDate.of(2013, Month.FEBRUARY, 2)));
|
||||||
|
modelMap.addAttribute("programmers", programmerList);
|
||||||
|
return "leafShow";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>thymeleaf模板引擎</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<ul th:each="programmer:${programmers}">
|
||||||
|
<li>
|
||||||
|
姓名:<span th:text="${programmer.name}"></span>
|
||||||
|
薪水:<span th:text="${programmer.salary}"></span>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,14 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>freemarker模板引擎</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<ul>
|
||||||
|
<#list programmers as programmer>
|
||||||
|
<li>姓名: ${programmer.name} 年龄: ${programmer.age}</li>
|
||||||
|
</#list>
|
||||||
|
</ul>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.springbootbase;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class SpringBootBaseApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
62
spring-boot/spring-boot-servlet/pom.xml
Normal file
62
spring-boot/spring-boot-servlet/pom.xml
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.1.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.heibaiying</groupId>
|
||||||
|
<artifactId>spring-boot-servlet</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-servlet</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
<!--排除依赖 使用外部tomcat容器启动-->
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<!--使用外置容器时候SpringBootServletInitializer 依赖此包 -->
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>servlet-api</artifactId>
|
||||||
|
<version>2.5</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--servlet api 注解依赖包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.heibaiying.springbootservlet;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
|
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||||
|
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@ServletComponentScan("com.heibaiying.springbootservlet") //在独立的容器(非内嵌)中@ServletComponentScan不起作用,可以不配置,取为代之的是容器内建的discovery机制自动发现。
|
||||||
|
public class SpringBootServletApplication extends SpringBootServletInitializer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||||
|
//传入SpringBoot应用的主程序
|
||||||
|
return application.sources(SpringBootServletApplication.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果用外置tomcat,启动报错java.lang.NoClassDefFoundError: javax/el/ELManager
|
||||||
|
* 是因为tomcat 7.0 el-api包中没有ELManager类 , 切换tomcat 为8.0 以上版本即可
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootServletApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.heibaiying.springbootservlet.config;
|
||||||
|
|
||||||
|
import com.heibaiying.springbootservlet.filter.CustomFilter;
|
||||||
|
import com.heibaiying.springbootservlet.listen.CustomListen;
|
||||||
|
import com.heibaiying.springbootservlet.servlet.CustomServlet;
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
|
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
|
||||||
|
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.ServletContextListener;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class ServletConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServletRegistrationBean registrationBean() {
|
||||||
|
return new ServletRegistrationBean<HttpServlet>(new CustomServlet(), "/servlet");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FilterRegistrationBean filterRegistrationBean() {
|
||||||
|
FilterRegistrationBean bean = new FilterRegistrationBean<Filter>();
|
||||||
|
bean.setFilter(new CustomFilter());
|
||||||
|
bean.addUrlPatterns("/servlet");
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServletListenerRegistrationBean listenerRegistrationBean() {
|
||||||
|
return new ServletListenerRegistrationBean<ServletContextListener>(new CustomListen());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.heibaiying.springbootservlet.filter;
|
||||||
|
|
||||||
|
import javax.servlet.*;
|
||||||
|
import javax.servlet.annotation.WebFilter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 自定义过滤器
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class CustomFilter implements Filter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig filterConfig) throws ServletException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||||
|
request.setAttribute("filterParam","我是filter传递的参数");
|
||||||
|
chain.doFilter(request,response);
|
||||||
|
response.getWriter().append(" CustomFilter ");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.heibaiying.springbootservlet.filter;
|
||||||
|
|
||||||
|
import javax.servlet.*;
|
||||||
|
import javax.servlet.annotation.WebFilter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 自定义过滤器
|
||||||
|
*/
|
||||||
|
|
||||||
|
@WebFilter(urlPatterns = "/servletAnn")
|
||||||
|
public class CustomFilterAnnotation implements Filter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig filterConfig) throws ServletException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||||
|
chain.doFilter(request,response);
|
||||||
|
response.getWriter().append(" CustomFilter Annotation");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.heibaiying.springbootservlet.listen;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContextEvent;
|
||||||
|
import javax.servlet.ServletContextListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 自定义监听器
|
||||||
|
*/
|
||||||
|
public class CustomListen implements ServletContextListener {
|
||||||
|
|
||||||
|
//Web应用程序初始化过程正在启动的通知
|
||||||
|
@Override
|
||||||
|
public void contextInitialized(ServletContextEvent sce) {
|
||||||
|
System.out.println("容器初始化启动");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* 通知servlet上下文即将关闭
|
||||||
|
* 这个地方如果我们使用的是spring boot 内置的容器 是监听不到销毁过程,所以我们使用了外置 tomcat 容器
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void contextDestroyed(ServletContextEvent sce) {
|
||||||
|
System.out.println("容器即将销毁");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.heibaiying.springbootservlet.listen;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContextEvent;
|
||||||
|
import javax.servlet.ServletContextListener;
|
||||||
|
import javax.servlet.annotation.WebListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description :自定义监听器
|
||||||
|
*/
|
||||||
|
@WebListener
|
||||||
|
public class CustomListenAnnotation implements ServletContextListener {
|
||||||
|
|
||||||
|
//Web应用程序初始化过程正在启动的通知
|
||||||
|
@Override
|
||||||
|
public void contextInitialized(ServletContextEvent sce) {
|
||||||
|
System.out.println("容器初始化启动 Annotation");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* 通知servlet上下文即将关闭
|
||||||
|
* 这个地方如果我们使用的是spring boot 内置的容器 是监听不到销毁过程,所以我们使用了外置 tomcat 容器
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void contextDestroyed(ServletContextEvent sce) {
|
||||||
|
System.out.println("容器即将销毁 Annotation");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.heibaiying.springbootservlet.servlet;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 自定义servlet
|
||||||
|
*/
|
||||||
|
public class CustomServlet extends HttpServlet {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
System.out.println("doGet 执行:" + req.getAttribute("filterParam"));
|
||||||
|
resp.getWriter().append("CustomServlet");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
doGet(req, resp);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.heibaiying.springbootservlet.servlet;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 自定义servlet
|
||||||
|
*/
|
||||||
|
@WebServlet(urlPatterns = "/servletAnn")
|
||||||
|
public class CustomServletAnnation extends HttpServlet {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
resp.getWriter().append("CustomServlet Annotation");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
doGet(req, resp);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.springbootservlet;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class SpringBootServletApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
54
spring-boot/spring-boot-yml-profile/pom.xml
Normal file
54
spring-boot/spring-boot-yml-profile/pom.xml
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.1.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.heibaiying</groupId>
|
||||||
|
<artifactId>spring-boot-yml-profile</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-yml-profile</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--配置文件处理器,导入后书写自定义配置时候会有提示-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.heibaiying.ymlprofile;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootYmlApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootYmlApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.heibaiying.ymlprofile.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.ToString;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
* @description : 属性配置映射类
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "programmer")
|
||||||
|
@Data
|
||||||
|
@ToString
|
||||||
|
public class Programmer {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
private boolean married;
|
||||||
|
private Date hireDate;
|
||||||
|
private float salary;
|
||||||
|
private int random;
|
||||||
|
private Map<String, String> skill;
|
||||||
|
private List company;
|
||||||
|
private School school;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.heibaiying.ymlprofile.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class School {
|
||||||
|
private String name;
|
||||||
|
private String location;
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.heibaiying.ymlprofile.controller;
|
||||||
|
|
||||||
|
import com.heibaiying.ymlprofile.config.Programmer;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : heibaiying
|
||||||
|
*/
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("yml")
|
||||||
|
public class YmlController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Programmer programmer;
|
||||||
|
|
||||||
|
@RequestMapping
|
||||||
|
public Programmer programmer(){
|
||||||
|
return programmer;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
# 字符串默认不用加上单引号或者双引号,但是如果字符串之中包含空格或特殊字符,需要放在引号之中。
|
||||||
|
# "":双引号;双引号不会对特殊字符转义
|
||||||
|
# '':单引号;会进行转义
|
||||||
|
|
||||||
|
programmer:
|
||||||
|
name: xiaoming-DEV
|
||||||
|
married: false
|
||||||
|
hireDate: 2018/12/23
|
||||||
|
salary: 66666.88
|
||||||
|
random: ${random.int[1024,65536]}
|
||||||
|
skill: {java: master, jquery: proficiency}
|
||||||
|
company: [baidu,tengxun,alibaba]
|
||||||
|
school:
|
||||||
|
name: unviersity
|
||||||
|
location: shanghai
|
||||||
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
|||||||
|
# 字符串默认不用加上单引号或者双引号,但是如果字符串之中包含空格或特殊字符,需要放在引号之中。
|
||||||
|
# "":双引号;双引号不会对特殊字符转义
|
||||||
|
# '':单引号;会进行转义
|
||||||
|
|
||||||
|
programmer:
|
||||||
|
name: xiaoming-PROD
|
||||||
|
age: 23
|
||||||
|
married: false
|
||||||
|
hireDate: 2018/12/23
|
||||||
|
salary: 66666.88
|
||||||
|
random: ${random.int[1024,65536]}
|
||||||
|
skill: {java: master, jquery: proficiency}
|
||||||
|
company: [baidu,tengxun,alibaba]
|
||||||
|
school:
|
||||||
|
name: unviersity
|
||||||
|
location: shanghai
|
||||||
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
|||||||
|
# 指定启动的配置
|
||||||
|
spring:
|
||||||
|
profiles:
|
||||||
|
active: dev
|
||||||
|
|
||||||
|
# 配置的补充与优先级 在dev中没有配置age,所以会采用主配置的,在prod中配置dev,会按照精确优先,使用dev中的配置
|
||||||
|
# 更多配置的优先级可以查看项目的README.md
|
||||||
|
programmer:
|
||||||
|
age: 999
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.ymlprofile;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class SpringBootYmlProfileApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user