commit 1567dc8fc9c5db434d012988063e205e6c3db209
Author: rouchen <3133657697@qq.com>
Date: Tue Jun 25 22:38:20 2024 +0800
feat 测试spring发布与监听
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..549e00a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..e155906
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,96 @@
+
+
+ 4.0.0
+ com.bwie
+ demo_spring
+ 0.0.1-SNAPSHOT
+ demo_spring
+ Demo project for Spring Boot
+
+ 1.8
+ UTF-8
+ UTF-8
+ 2.6.13
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.springframework
+ spring-context
+ 5.3.20
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.springframework.boot
+ spring-boot-test
+ 2.7.15
+
+
+ org.aspectj
+ aspectjweaver
+ 1.9.7
+
+
+ org.bouncycastle
+ bcprov-jdk15on
+ 1.70
+
+
+ org.apache.logging.log4j
+ log4j-to-slf4j
+ 2.17.2
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-dependencies
+ ${spring-boot.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.1
+
+ 1.8
+ 1.8
+ UTF-8
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+ ${spring-boot.version}
+
+ com.bwie.DemoSpringApplication
+ true
+
+
+
+ repackage
+
+ repackage
+
+
+
+
+
+
+
+
diff --git a/src/main/java/com/bwie/DemoSpringApplication.java b/src/main/java/com/bwie/DemoSpringApplication.java
new file mode 100644
index 0000000..8893101
--- /dev/null
+++ b/src/main/java/com/bwie/DemoSpringApplication.java
@@ -0,0 +1,17 @@
+package com.bwie;
+
+
+import com.bwie.demos.litent.CustomApplicationEventPublisher;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+import javax.annotation.Resource;
+
+@SpringBootApplication
+public class DemoSpringApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(DemoSpringApplication.class, args);
+ }
+
+}
diff --git a/src/main/java/com/bwie/demos/asyn/AsynCustomApplicationListener.java b/src/main/java/com/bwie/demos/asyn/AsynCustomApplicationListener.java
new file mode 100644
index 0000000..24ff390
--- /dev/null
+++ b/src/main/java/com/bwie/demos/asyn/AsynCustomApplicationListener.java
@@ -0,0 +1,19 @@
+package com.bwie.demos.asyn;
+
+import com.bwie.demos.litent.CustomApplicationEvent;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Component;
+
+
+@Component
+public class AsynCustomApplicationListener {
+
+ @Async
+ @EventListener(CustomApplicationEvent.class)
+ public void asyncListener(CustomApplicationEvent customApplicationEvent) {
+ System.out.println("customApplicationEvent "+customApplicationEvent);
+ System.out.println("异步事件监听,当前线程:{},消息为:{}" + Thread.currentThread().getName() + customApplicationEvent.getMessage());
+
+ }
+}
diff --git a/src/main/java/com/bwie/demos/asyn/AsyncTaskExecutorConfig.java b/src/main/java/com/bwie/demos/asyn/AsyncTaskExecutorConfig.java
new file mode 100644
index 0000000..c6fff6d
--- /dev/null
+++ b/src/main/java/com/bwie/demos/asyn/AsyncTaskExecutorConfig.java
@@ -0,0 +1,37 @@
+package com.bwie.demos.asyn;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.task.AsyncTaskExecutor;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.ThreadPoolExecutor;
+
+/**
+ * 线程池配置 AsyncTaskExecutorConfig
+ *
+ * @author Yangle
+ * Date 2024/6/25 19:59
+ */
+@EnableAsync
+@Configuration
+public class AsyncTaskExecutorConfig {
+
+ @Bean
+ public AsyncTaskExecutor taskExecutor(){
+ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+ executor.setCorePoolSize(10);
+ executor.setMaxPoolSize(20);
+ executor.setQueueCapacity(200);
+ executor.setKeepAliveSeconds(60);
+ executor.setThreadNamePrefix("taskExecutor-");
+ executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+ executor.setWaitForTasksToCompleteOnShutdown(true);
+ executor.setAwaitTerminationSeconds(60);
+ executor.initialize();
+ return executor;
+ }
+
+
+}
diff --git a/src/main/java/com/bwie/demos/litent/AnnotationCustomApplicationListener.java b/src/main/java/com/bwie/demos/litent/AnnotationCustomApplicationListener.java
new file mode 100644
index 0000000..dd134db
--- /dev/null
+++ b/src/main/java/com/bwie/demos/litent/AnnotationCustomApplicationListener.java
@@ -0,0 +1,15 @@
+package com.bwie.demos.litent;
+
+import org.springframework.context.event.EventListener;
+import org.springframework.stereotype.Component;
+
+
+@Component
+public class AnnotationCustomApplicationListener {
+
+ @EventListener(CustomApplicationEvent.class)
+ public void listener(CustomApplicationEvent customApplicationEvent) {
+ System.out.println("EventListener注解方式接收到的消息为:{}"+ customApplicationEvent.getMessage());
+ }
+
+}
diff --git a/src/main/java/com/bwie/demos/litent/CustomApplicationEvent.java b/src/main/java/com/bwie/demos/litent/CustomApplicationEvent.java
new file mode 100644
index 0000000..0df4b79
--- /dev/null
+++ b/src/main/java/com/bwie/demos/litent/CustomApplicationEvent.java
@@ -0,0 +1,18 @@
+package com.bwie.demos.litent;
+
+import org.springframework.context.ApplicationEvent;
+
+public class CustomApplicationEvent extends ApplicationEvent {
+
+ private String message;
+
+ public CustomApplicationEvent(Object source, String message) {
+ super(source);
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+}
diff --git a/src/main/java/com/bwie/demos/litent/CustomApplicationEventPublisher.java b/src/main/java/com/bwie/demos/litent/CustomApplicationEventPublisher.java
new file mode 100644
index 0000000..3a3a52d
--- /dev/null
+++ b/src/main/java/com/bwie/demos/litent/CustomApplicationEventPublisher.java
@@ -0,0 +1,21 @@
+package com.bwie.demos.litent;
+
+import org.springframework.context.ApplicationEventPublisher;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+
+
+@Component
+public class CustomApplicationEventPublisher {
+ @Resource
+ ApplicationEventPublisher applicationEventPublisher;
+
+ public void publishEvent(String message) {
+ System.out.println("开始发布自定义事件");
+ CustomApplicationEvent customApplicationEvent = new CustomApplicationEvent(this, message);
+ // 发布事件
+ applicationEventPublisher.publishEvent(customApplicationEvent);
+ System.out.println("发布自定义事件结束");
+ }
+}
diff --git a/src/main/java/com/bwie/demos/litent/CustomApplicationListener.java b/src/main/java/com/bwie/demos/litent/CustomApplicationListener.java
new file mode 100644
index 0000000..869c4ef
--- /dev/null
+++ b/src/main/java/com/bwie/demos/litent/CustomApplicationListener.java
@@ -0,0 +1,13 @@
+package com.bwie.demos.litent;
+
+import org.springframework.context.ApplicationListener;
+import org.springframework.stereotype.Component;
+
+
+@Component
+public class CustomApplicationListener implements ApplicationListener {
+ @Override
+ public void onApplicationEvent(CustomApplicationEvent event) {
+ System.out.println("onApplicationEvent方法接收到的消息:{} "+event.getMessage());
+ }
+}
diff --git a/src/main/java/com/bwie/demos/web/BasicController.java b/src/main/java/com/bwie/demos/web/BasicController.java
new file mode 100644
index 0000000..01de7ac
--- /dev/null
+++ b/src/main/java/com/bwie/demos/web/BasicController.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2013-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.bwie.demos.web;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+/**
+ * @author theonefx
+ */
+@Controller
+public class BasicController {
+
+ // http://127.0.0.1:8080/hello?name=lisi
+ @RequestMapping("/hello")
+ @ResponseBody
+ public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
+ return "Hello " + name;
+ }
+
+ // http://127.0.0.1:8080/user
+ @RequestMapping("/user")
+ @ResponseBody
+ public User user() {
+ User user = new User();
+ user.setName("theonefx");
+ user.setAge(666);
+ return user;
+ }
+
+ // http://127.0.0.1:8080/save_user?name=newName&age=11
+ @RequestMapping("/save_user")
+ @ResponseBody
+ public String saveUser(User u) {
+ return "user will save: name=" + u.getName() + ", age=" + u.getAge();
+ }
+
+ // http://127.0.0.1:8080/html
+ @RequestMapping("/html")
+ public String html(){
+ return "index.html";
+ }
+
+ @ModelAttribute
+ public void parseUser(@RequestParam(name = "name", defaultValue = "unknown user") String name
+ , @RequestParam(name = "age", defaultValue = "12") Integer age, User user) {
+ user.setName("zhangsan");
+ user.setAge(18);
+ }
+}
diff --git a/src/main/java/com/bwie/demos/web/PathVariableController.java b/src/main/java/com/bwie/demos/web/PathVariableController.java
new file mode 100644
index 0000000..461f81b
--- /dev/null
+++ b/src/main/java/com/bwie/demos/web/PathVariableController.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2013-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.bwie.demos.web;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+/**
+ * @author theonefx
+ */
+@Controller
+public class PathVariableController {
+
+ // http://127.0.0.1:8080/user/123/roles/222
+ @RequestMapping(value = "/user/{userId}/roles/{roleId}", method = RequestMethod.GET)
+ @ResponseBody
+ public String getLogin(@PathVariable("userId") String userId, @PathVariable("roleId") String roleId) {
+ return "User Id : " + userId + " Role Id : " + roleId;
+ }
+
+ // http://127.0.0.1:8080/javabeat/somewords
+ @RequestMapping(value = "/javabeat/{regexp1:[a-z-]+}", method = RequestMethod.GET)
+ @ResponseBody
+ public String getRegExp(@PathVariable("regexp1") String regexp1) {
+ return "URI Part : " + regexp1;
+ }
+}
diff --git a/src/main/java/com/bwie/demos/web/User.java b/src/main/java/com/bwie/demos/web/User.java
new file mode 100644
index 0000000..5898b93
--- /dev/null
+++ b/src/main/java/com/bwie/demos/web/User.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2013-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.bwie.demos.web;
+
+/**
+ * @author theonefx
+ */
+public class User {
+
+ private String name;
+
+ private Integer age;
+
+ 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;
+ }
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
new file mode 100644
index 0000000..e52b498
--- /dev/null
+++ b/src/main/resources/application.properties
@@ -0,0 +1,3 @@
+# 应用服务 WEB 访问端口
+server.port=8080
+
diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html
new file mode 100644
index 0000000..e2d94a2
--- /dev/null
+++ b/src/main/resources/static/index.html
@@ -0,0 +1,6 @@
+
+
+ hello word!!!
+ this is a html page
+
+
\ No newline at end of file
diff --git a/src/test/java/com/bwie/DemoSpringApplicationTests.java b/src/test/java/com/bwie/DemoSpringApplicationTests.java
new file mode 100644
index 0000000..271412c
--- /dev/null
+++ b/src/test/java/com/bwie/DemoSpringApplicationTests.java
@@ -0,0 +1,20 @@
+package com.bwie;
+
+import com.bwie.demos.litent.CustomApplicationEventPublisher;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import javax.annotation.Resource;
+
+@SpringBootTest
+class DemoSpringApplicationTests {
+
+ @Resource
+ private CustomApplicationEventPublisher eventPublisher;
+
+ @Test
+ public void publishTest() {
+ eventPublisher.publishEvent("999");
+ }
+
+}