diff --git a/README.md b/README.md index 43866ec..0d96705 100644 --- a/README.md +++ b/README.md @@ -46,28 +46,24 @@ spring-cloud:Finchley.SR2 ## 2. spring-boot samples -| samples | 描述 | 官方文档 | -| --------------------------- | ------------------------------ | ------------------------------------------------------------ | -| spring-boot-base | spring-boot 基础 | [spring boot 官方文档](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/) | -| spring-boot-aop | spring aop | | -| spring-boot-cache | spring-boot 缓存 | | -| spring-boot-profile | spring 场景切换 | | -| spring-boot-servlet | 整合servlet 3.0 | | -| spring-boot-test | spring-boot 单元测试 | | -| spring-boot-jpa | spring-boot jpa 的使用 | | -| spring-boot-freemarker | freemarker 的使用 | | -| spring-boot-jsp | spring-boot 整合 jsp | | -| spring-boot-mybatis | spring-boot 整合 mybatis | | -| spring-boot-druid-mybtais | spring-boot 整合druid、mybatis | | -| spring-boot-druid-redis | spring-boot 整合 redis | | -| spring-boot-druid-mongodb | spring-boot 整合 mongodb | | -| spring-boot-druid-memcached | spring-boot 整合 memcached | | -| spring-boot-druid-rabbitmq | spring-boot 整合 rabbitmq | | -| spring-boot-druid-kafka | spring-boot 整合 kafka | | -| 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 定时任务 | | +| samples | 描述 | 官方文档 | +| ----------------------- | ------------------------------ | ------------------------------------------------------------ | +| spring-boot-base | spring-boot 基础 | [spring boot 官方文档](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/)
[spring boot 中文官方文档](https://www.breakyizhan.com/springboot/3028.html) | +| spring-boot-cache | spring-boot 缓存 | | +| spring-boot-yml-profile | yml 语法和多配置切换 | | +| spring-boot-servlet | 整合servlet 3.0 | | +| spring-boot-jpa | spring-boot jpa 的使用 | | +| spring-boot-jsp | spring-boot 整合 jsp | | +| spring-boot-mybatis | spring-boot 整合 mybatis | | +| spring-boot-mybtais | spring-boot 整合druid、mybatis | | +| spring-boot-redis | spring-boot 整合 redis | | +| spring-boot-mongodb | spring-boot 整合 mongodb | | +| spring-boot-memcached | spring-boot 整合 memcached | | +| spring-boot-rabbitmq | spring-boot 整合 rabbitmq | | +| spring-boot-kafka | spring-boot 整合 kafka | | +| spring-boot-dubbo | spring-boot 整合 dubbo | | +| spring-boot-websocket | spring-boot 整合 websocket | | +| spring-boot-email | spring-boot 邮件发送 | | 更多的场景和用例可参阅 [spring-boot 官方samples ](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples) diff --git a/referenced documents/Thymeleaf中⽂参考⼿册.pdf b/referenced documents/Thymeleaf中⽂参考⼿册.pdf new file mode 100644 index 0000000..649efa1 Binary files /dev/null and b/referenced documents/Thymeleaf中⽂参考⼿册.pdf differ diff --git a/spring-boot/spring-boot-base/pom.xml b/spring-boot/spring-boot-base/pom.xml new file mode 100644 index 0000000..9c2aaa8 --- /dev/null +++ b/spring-boot/spring-boot-base/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + spring-boot-base + 0.0.1-SNAPSHOT + spring-boot-base + Demo project for Spring Boot + + + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-freemarker + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/SpringBootBaseApplication.java b/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/SpringBootBaseApplication.java new file mode 100644 index 0000000..547f498 --- /dev/null +++ b/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/SpringBootBaseApplication.java @@ -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); + } + +} + diff --git a/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/bean/Programmer.java b/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/bean/Programmer.java new file mode 100644 index 0000000..6d8efa4 --- /dev/null +++ b/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/bean/Programmer.java @@ -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; +} \ No newline at end of file diff --git a/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/controller/FreeMarkerController.java b/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/controller/FreeMarkerController.java new file mode 100644 index 0000000..98e1033 --- /dev/null +++ b/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/controller/FreeMarkerController.java @@ -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 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"; + } +} diff --git a/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/controller/RestfulController.java b/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/controller/RestfulController.java new file mode 100644 index 0000000..a458a4b --- /dev/null +++ b/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/controller/RestfulController.java @@ -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 getProgrammers() { + List 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; + } +} diff --git a/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/controller/ThymeleafController.java b/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/controller/ThymeleafController.java new file mode 100644 index 0000000..2b7a229 --- /dev/null +++ b/spring-boot/spring-boot-base/src/main/java/com/heibaiying/springbootbase/controller/ThymeleafController.java @@ -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 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"; + } +} diff --git a/spring-boot/.gitignore b/spring-boot/spring-boot-base/src/main/resources/application.properties similarity index 100% rename from spring-boot/.gitignore rename to spring-boot/spring-boot-base/src/main/resources/application.properties diff --git a/spring-boot/spring-boot-base/src/main/resources/templates/leafShow.html b/spring-boot/spring-boot-base/src/main/resources/templates/leafShow.html new file mode 100644 index 0000000..c823d1e --- /dev/null +++ b/spring-boot/spring-boot-base/src/main/resources/templates/leafShow.html @@ -0,0 +1,15 @@ + + + + + thymeleaf模板引擎 + + +
    +
  • + 姓名: + 薪水: +
  • +
+ + \ No newline at end of file diff --git a/spring-boot/spring-boot-base/src/main/resources/templates/markerShow.ftl b/spring-boot/spring-boot-base/src/main/resources/templates/markerShow.ftl new file mode 100644 index 0000000..7d4e21c --- /dev/null +++ b/spring-boot/spring-boot-base/src/main/resources/templates/markerShow.ftl @@ -0,0 +1,14 @@ + + + + + freemarker模板引擎 + + +
    + <#list programmers as programmer> +
  • 姓名: ${programmer.name} 年龄: ${programmer.age}
  • + +
+ + \ No newline at end of file diff --git a/spring-boot/spring-boot-base/src/test/java/com/heibaiying/springbootbase/SpringBootBaseApplicationTests.java b/spring-boot/spring-boot-base/src/test/java/com/heibaiying/springbootbase/SpringBootBaseApplicationTests.java new file mode 100644 index 0000000..724395c --- /dev/null +++ b/spring-boot/spring-boot-base/src/test/java/com/heibaiying/springbootbase/SpringBootBaseApplicationTests.java @@ -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() { + } + +} + diff --git a/spring-boot/spring-boot-servlet/pom.xml b/spring-boot/spring-boot-servlet/pom.xml new file mode 100644 index 0000000..3cb5319 --- /dev/null +++ b/spring-boot/spring-boot-servlet/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + spring-boot-servlet + 0.0.1-SNAPSHOT + spring-boot-servlet + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + + javax.servlet + servlet-api + 2.5 + provided + + + + org.springframework.boot + spring-boot-starter-test + test + + + + javax.servlet + javax.servlet-api + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/SpringBootServletApplication.java b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/SpringBootServletApplication.java new file mode 100644 index 0000000..b1c5450 --- /dev/null +++ b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/SpringBootServletApplication.java @@ -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); + } + +} + diff --git a/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/config/ServletConfig.java b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/config/ServletConfig.java new file mode 100644 index 0000000..857e37a --- /dev/null +++ b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/config/ServletConfig.java @@ -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(new CustomServlet(), "/servlet"); + } + + @Bean + public FilterRegistrationBean filterRegistrationBean() { + FilterRegistrationBean bean = new FilterRegistrationBean(); + bean.setFilter(new CustomFilter()); + bean.addUrlPatterns("/servlet"); + return bean; + } + + @Bean + public ServletListenerRegistrationBean listenerRegistrationBean() { + return new ServletListenerRegistrationBean(new CustomListen()); + } + +} diff --git a/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/filter/CustomFilter.java b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/filter/CustomFilter.java new file mode 100644 index 0000000..046ab8d --- /dev/null +++ b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/filter/CustomFilter.java @@ -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() { + + } +} diff --git a/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/filter/CustomFilterAnnotation.java b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/filter/CustomFilterAnnotation.java new file mode 100644 index 0000000..befcf3b --- /dev/null +++ b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/filter/CustomFilterAnnotation.java @@ -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() { + + } +} diff --git a/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/listen/CustomListen.java b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/listen/CustomListen.java new file mode 100644 index 0000000..00d3c01 --- /dev/null +++ b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/listen/CustomListen.java @@ -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("容器即将销毁"); + } +} diff --git a/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/listen/CustomListenAnnotation.java b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/listen/CustomListenAnnotation.java new file mode 100644 index 0000000..90085ca --- /dev/null +++ b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/listen/CustomListenAnnotation.java @@ -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"); + } +} diff --git a/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/servlet/CustomServlet.java b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/servlet/CustomServlet.java new file mode 100644 index 0000000..ad07e7d --- /dev/null +++ b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/servlet/CustomServlet.java @@ -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); + } +} diff --git a/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/servlet/CustomServletAnnation.java b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/servlet/CustomServletAnnation.java new file mode 100644 index 0000000..bd4d69e --- /dev/null +++ b/spring-boot/spring-boot-servlet/src/main/java/com/heibaiying/springbootservlet/servlet/CustomServletAnnation.java @@ -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); + } +} diff --git a/spring-boot/spring-boot-servlet/src/main/resources/application.properties b/spring-boot/spring-boot-servlet/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/spring-boot/spring-boot-servlet/src/test/java/com/heibaiying/springbootservlet/SpringBootServletApplicationTests.java b/spring-boot/spring-boot-servlet/src/test/java/com/heibaiying/springbootservlet/SpringBootServletApplicationTests.java new file mode 100644 index 0000000..9348857 --- /dev/null +++ b/spring-boot/spring-boot-servlet/src/test/java/com/heibaiying/springbootservlet/SpringBootServletApplicationTests.java @@ -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() { + } + +} + diff --git a/spring-boot/spring-boot-yml-profile/pom.xml b/spring-boot/spring-boot-yml-profile/pom.xml new file mode 100644 index 0000000..248e0ca --- /dev/null +++ b/spring-boot/spring-boot-yml-profile/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + spring-boot-yml-profile + 0.0.1-SNAPSHOT + spring-boot-yml-profile + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/SpringBootYmlApplication.java b/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/SpringBootYmlApplication.java new file mode 100644 index 0000000..8f80c7b --- /dev/null +++ b/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/SpringBootYmlApplication.java @@ -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); + } + +} + diff --git a/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/config/Programmer.java b/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/config/Programmer.java new file mode 100644 index 0000000..ee07476 --- /dev/null +++ b/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/config/Programmer.java @@ -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 skill; + private List company; + private School school; + +} diff --git a/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/config/School.java b/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/config/School.java new file mode 100644 index 0000000..98b85d7 --- /dev/null +++ b/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/config/School.java @@ -0,0 +1,12 @@ +package com.heibaiying.ymlprofile.config; + +import lombok.Data; + +/** + * @author : heibaiying + */ +@Data +public class School { + private String name; + private String location; +} diff --git a/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/controller/YmlController.java b/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/controller/YmlController.java new file mode 100644 index 0000000..bccbae3 --- /dev/null +++ b/spring-boot/spring-boot-yml-profile/src/main/java/com/heibaiying/ymlprofile/controller/YmlController.java @@ -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; + } +} diff --git a/spring-boot/spring-boot-yml-profile/src/main/resources/application-dev.yml b/spring-boot/spring-boot-yml-profile/src/main/resources/application-dev.yml new file mode 100644 index 0000000..ed86760 --- /dev/null +++ b/spring-boot/spring-boot-yml-profile/src/main/resources/application-dev.yml @@ -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 + + diff --git a/spring-boot/spring-boot-yml-profile/src/main/resources/application-prod.yml b/spring-boot/spring-boot-yml-profile/src/main/resources/application-prod.yml new file mode 100644 index 0000000..808c581 --- /dev/null +++ b/spring-boot/spring-boot-yml-profile/src/main/resources/application-prod.yml @@ -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 + + diff --git a/spring-boot/spring-boot-yml-profile/src/main/resources/application.yml b/spring-boot/spring-boot-yml-profile/src/main/resources/application.yml new file mode 100644 index 0000000..0b9e370 --- /dev/null +++ b/spring-boot/spring-boot-yml-profile/src/main/resources/application.yml @@ -0,0 +1,13 @@ +# ָ +spring: + profiles: + active: dev + +# õIJȼ devûage,Իõģproddev,ᰴվȷȣʹdevе +# õȼԲ鿴ĿREADME.md +programmer: + age: 999 + + + + diff --git a/spring-boot/spring-boot-yml-profile/src/test/java/com/heibaiying/ymlprofile/SpringBootYmlProfileApplicationTests.java b/spring-boot/spring-boot-yml-profile/src/test/java/com/heibaiying/ymlprofile/SpringBootYmlProfileApplicationTests.java new file mode 100644 index 0000000..46fe7c9 --- /dev/null +++ b/spring-boot/spring-boot-yml-profile/src/test/java/com/heibaiying/ymlprofile/SpringBootYmlProfileApplicationTests.java @@ -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() { + } + +} +