From 61bd9d74cea19d90ee5b4c1c7e7a6044a6043aa3 Mon Sep 17 00:00:00 2001 From: yihui Date: Thu, 22 Aug 2019 19:39:29 +0800 Subject: [PATCH] add web beetl & context listener module --- spring-boot/012-context-listener/pom.xml | 25 +++++++++++ .../git/hui/boot/listener/Application.java | 21 +++++++++ .../com/git/hui/boot/listener/AutoConfig.java | 30 +++++++++++++ .../listener/context/MyContextAppHolder.java | 34 ++++++++++++++ .../listener/context/MyContextListener.java | 25 +++++++++++ .../git/hui/boot/listener/demo/DemoBean.java | 16 +++++++ .../boot/listener/demo/IndexController.java | 20 +++++++++ spring-boot/206-web-beetl/README.md | 10 +++++ spring-boot/206-web-beetl/pom.xml | 26 +++++++++++ .../com/git/hui/boot/beetl/Application.java | 16 +++++++ .../hui/boot/beetl/rest/IndexController.java | 44 +++++++++++++++++++ .../src/main/resources/application.yml | 8 ++++ .../src/main/resources/static/index.css | 16 +++++++ .../src/main/resources/templates/index.btl | 25 +++++++++++ .../src/main/resources/templates/show1.btl | 19 ++++++++ .../src/main/resources/templates/show2.btl | 19 ++++++++ spring-boot/pom.xml | 2 + 17 files changed, 356 insertions(+) create mode 100644 spring-boot/012-context-listener/pom.xml create mode 100644 spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/Application.java create mode 100644 spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/AutoConfig.java create mode 100644 spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/context/MyContextAppHolder.java create mode 100644 spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/context/MyContextListener.java create mode 100644 spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/demo/DemoBean.java create mode 100644 spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/demo/IndexController.java create mode 100644 spring-boot/206-web-beetl/README.md create mode 100644 spring-boot/206-web-beetl/pom.xml create mode 100644 spring-boot/206-web-beetl/src/main/java/com/git/hui/boot/beetl/Application.java create mode 100644 spring-boot/206-web-beetl/src/main/java/com/git/hui/boot/beetl/rest/IndexController.java create mode 100644 spring-boot/206-web-beetl/src/main/resources/application.yml create mode 100644 spring-boot/206-web-beetl/src/main/resources/static/index.css create mode 100644 spring-boot/206-web-beetl/src/main/resources/templates/index.btl create mode 100644 spring-boot/206-web-beetl/src/main/resources/templates/show1.btl create mode 100644 spring-boot/206-web-beetl/src/main/resources/templates/show2.btl diff --git a/spring-boot/012-context-listener/pom.xml b/spring-boot/012-context-listener/pom.xml new file mode 100644 index 00000000..a6c1637b --- /dev/null +++ b/spring-boot/012-context-listener/pom.xml @@ -0,0 +1,25 @@ + + + + spring-boot + com.git.hui.boot + 0.0.1-SNAPSHOT + + 4.0.0 + + 012-context-listener + + + javax.servlet + javax.servlet-api + + + org.springframework.boot + spring-boot-starter-web + + + + + \ No newline at end of file diff --git a/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/Application.java b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/Application.java new file mode 100644 index 00000000..4fee122c --- /dev/null +++ b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/Application.java @@ -0,0 +1,21 @@ +package com.git.hui.boot.listener; + +import com.git.hui.boot.listener.demo.DemoBean; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Created by @author yihui in 08:47 19/8/21. + */ +@SpringBootApplication +public class Application { + + public Application(DemoBean demoBean) { + System.out.println("init: " + demoBean.getNum()); + } + + public static void main(String[] args) { + SpringApplication.run(Application.class); + } + +} diff --git a/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/AutoConfig.java b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/AutoConfig.java new file mode 100644 index 00000000..c0a7dd32 --- /dev/null +++ b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/AutoConfig.java @@ -0,0 +1,30 @@ +package com.git.hui.boot.listener; + +import com.git.hui.boot.listener.context.MyContextAppHolder; +import com.git.hui.boot.listener.context.MyContextListener; +import com.git.hui.boot.listener.demo.DemoBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Created by @author yihui in 08:55 19/8/21. + */ +@Configuration +public class AutoConfig { + + @Bean + public MyContextAppHolder myContextAppHolder() { + return new MyContextAppHolder(); + } + + @Bean + public DemoBean demoBean(MyContextAppHolder myContextAppHolder) throws Exception { + return myContextAppHolder.getObject(); + } + + @Bean + public MyContextListener myContextListener() { + return new MyContextListener(); + } + +} diff --git a/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/context/MyContextAppHolder.java b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/context/MyContextAppHolder.java new file mode 100644 index 00000000..18affb09 --- /dev/null +++ b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/context/MyContextAppHolder.java @@ -0,0 +1,34 @@ +package com.git.hui.boot.listener.context; + +import com.git.hui.boot.listener.demo.DemoBean; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import java.util.UUID; + +/** + * Created by @author yihui in 08:49 19/8/21. + */ +public class MyContextAppHolder implements FactoryBean, ApplicationContextAware { + private ApplicationContext applicationContext; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + @Override + public DemoBean getObject() throws Exception { + System.out.println("init bean!" + UUID.randomUUID().toString()); + MyContextListener listener = (MyContextListener) applicationContext.getBean("myContextListener"); + return new DemoBean(listener.getNum()); + } + + @Override + public Class getObjectType() { + return DemoBean.class; + } +} diff --git a/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/context/MyContextListener.java b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/context/MyContextListener.java new file mode 100644 index 00000000..146bcd29 --- /dev/null +++ b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/context/MyContextListener.java @@ -0,0 +1,25 @@ +package com.git.hui.boot.listener.context; + +import lombok.Getter; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +/** + * Created by @author yihui in 08:48 19/8/21. + */ +public class MyContextListener implements ServletContextListener { + @Getter + private volatile int num = 0; + + @Override + public void contextInitialized(ServletContextEvent servletContextEvent) { + System.out.println("-------> context loader!"); + num += 1; + } + + @Override + public void contextDestroyed(ServletContextEvent servletContextEvent) { + System.out.println("===== context destroyed! ======"); + } +} diff --git a/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/demo/DemoBean.java b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/demo/DemoBean.java new file mode 100644 index 00000000..29cca128 --- /dev/null +++ b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/demo/DemoBean.java @@ -0,0 +1,16 @@ +package com.git.hui.boot.listener.demo; + +import lombok.Getter; + +/** + * Created by @author yihui in 08:52 19/8/21. + */ +public class DemoBean { + @Getter + private int num; + + public DemoBean(int num) { + this.num = num; + } + +} diff --git a/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/demo/IndexController.java b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/demo/IndexController.java new file mode 100644 index 00000000..94a96c63 --- /dev/null +++ b/spring-boot/012-context-listener/src/main/java/com/git/hui/boot/listener/demo/IndexController.java @@ -0,0 +1,20 @@ +package com.git.hui.boot.listener.demo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Created by @author yihui in 08:59 19/8/21. + */ +@RestController +public class IndexController { + @Autowired + private DemoBean demoBean; + + @GetMapping(path = {"/", "/index"}) + public String show() { + return "ans: " + demoBean.getNum(); + } + +} diff --git a/spring-boot/206-web-beetl/README.md b/spring-boot/206-web-beetl/README.md new file mode 100644 index 00000000..b42d218f --- /dev/null +++ b/spring-boot/206-web-beetl/README.md @@ -0,0 +1,10 @@ +## web-beetl +### 项目说明 + +本项目主要基于springboot + beetl,用于演示基本的web项目构建 + +### 博文说明 + +本项目对应的博文内容为 + +- [190822-SpringBoot系列教程web篇之Beetl环境搭建/](http://spring.hhui.top/spring-blog/2019/08/22/190822-SpringBoot%E7%B3%BB%E5%88%97%E6%95%99%E7%A8%8Bweb%E7%AF%87%E4%B9%8BBeetl%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/) \ No newline at end of file diff --git a/spring-boot/206-web-beetl/pom.xml b/spring-boot/206-web-beetl/pom.xml new file mode 100644 index 00000000..3bf84831 --- /dev/null +++ b/spring-boot/206-web-beetl/pom.xml @@ -0,0 +1,26 @@ + + + + spring-boot + com.git.hui.boot + 0.0.1-SNAPSHOT + + 4.0.0 + + 206-web-beetl + + + + org.springframework.boot + spring-boot-starter-web + + + com.ibeetl + beetl-framework-starter + 1.2.12.RELEASE + + + + \ No newline at end of file diff --git a/spring-boot/206-web-beetl/src/main/java/com/git/hui/boot/beetl/Application.java b/spring-boot/206-web-beetl/src/main/java/com/git/hui/boot/beetl/Application.java new file mode 100644 index 00000000..742f975d --- /dev/null +++ b/spring-boot/206-web-beetl/src/main/java/com/git/hui/boot/beetl/Application.java @@ -0,0 +1,16 @@ +package com.git.hui.boot.beetl; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Created by @author yihui in 18:50 19/8/22. + */ +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class); + } + +} diff --git a/spring-boot/206-web-beetl/src/main/java/com/git/hui/boot/beetl/rest/IndexController.java b/spring-boot/206-web-beetl/src/main/java/com/git/hui/boot/beetl/rest/IndexController.java new file mode 100644 index 00000000..23dba1f7 --- /dev/null +++ b/spring-boot/206-web-beetl/src/main/java/com/git/hui/boot/beetl/rest/IndexController.java @@ -0,0 +1,44 @@ +package com.git.hui.boot.beetl.rest; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.servlet.ModelAndView; + +import java.time.LocalDateTime; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +/** + * Created by @author yihui in 14:44 19/8/16. + */ +@Controller +public class IndexController { + + @GetMapping(path = {"", "/", "/index"}) + public ModelAndView index() { + Map data = new HashMap<>(2); + data.put("name", "YiHui Beetl"); + data.put("now", LocalDateTime.now().toString()); + return new ModelAndView("index.btl", data); + } + + private static String[] contents = + ("绿蚁浮觞香泛泛,黄花共荐芳辰。\n清霜天宇净无尘。\n登高宜有赋,拈笔戏成文。\n可奈园林摇落尽,悲秋意与谁论。\n眼中相识几番新。\n龙山高会处,落帽定何人。").split("\n"); + private static Random random = new Random(); + + @GetMapping(path = "show1") + public String showOne(Model model) { + model.addAttribute("title", "临江仙"); + model.addAttribute("content", contents[random.nextInt(6)]); + return "show1.btl"; + } + + @GetMapping(path = "show2") + public String showTow(Map data) { + data.put("name", "Show2---->"); + data.put("now", LocalDateTime.now().toString()); + return "show2.btl"; + } +} diff --git a/spring-boot/206-web-beetl/src/main/resources/application.yml b/spring-boot/206-web-beetl/src/main/resources/application.yml new file mode 100644 index 00000000..0af94b42 --- /dev/null +++ b/spring-boot/206-web-beetl/src/main/resources/application.yml @@ -0,0 +1,8 @@ +server: + port: 8080 + +beetl: + enabled: true + suffix: btl +beetl-beetlsql: + dev: true # 即自动检查模板变化 diff --git a/spring-boot/206-web-beetl/src/main/resources/static/index.css b/spring-boot/206-web-beetl/src/main/resources/static/index.css new file mode 100644 index 00000000..ab6d2928 --- /dev/null +++ b/spring-boot/206-web-beetl/src/main/resources/static/index.css @@ -0,0 +1,16 @@ +.title { + color: #c00; + font-weight: normal; + font-size: 2em; +} + +.content { + color: darkblue; + font-size: 1.2em; +} + +.sign { + color: lightgray; + font-size: 0.8em; + font-style: italic; +} \ No newline at end of file diff --git a/spring-boot/206-web-beetl/src/main/resources/templates/index.btl b/spring-boot/206-web-beetl/src/main/resources/templates/index.btl new file mode 100644 index 00000000..015655b8 --- /dev/null +++ b/spring-boot/206-web-beetl/src/main/resources/templates/index.btl @@ -0,0 +1,25 @@ + + + + + + + + + YiHui's SpringBoot Beetl Demo + + + + +
+
hello world!
+
+
欢迎访问 ${name}
+
+
当前时间 ${now}
+
+ 传参2测试      + 传参3测试 +
+ + diff --git a/spring-boot/206-web-beetl/src/main/resources/templates/show1.btl b/spring-boot/206-web-beetl/src/main/resources/templates/show1.btl new file mode 100644 index 00000000..cfe138ed --- /dev/null +++ b/spring-boot/206-web-beetl/src/main/resources/templates/show1.btl @@ -0,0 +1,19 @@ + + + + + + + + + YiHui's SpringBoot Beetl Demo + + + + +
+
${title}
+
${content}
+
+ + diff --git a/spring-boot/206-web-beetl/src/main/resources/templates/show2.btl b/spring-boot/206-web-beetl/src/main/resources/templates/show2.btl new file mode 100644 index 00000000..8c292c2d --- /dev/null +++ b/spring-boot/206-web-beetl/src/main/resources/templates/show2.btl @@ -0,0 +1,19 @@ + + + + + + + + + YiHui's SpringBoot Beetl Demo + + + + +
+
${name}
+
${now}
+
+ + diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 7f43af12..3e83cb79 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -40,6 +40,8 @@ 131-influxdb-java 204-web-freemaker 205-web-thymeleaf + 012-context-listener + 206-web-beetl spring-boot