GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add spring publish and sub event example · Qunzer/learningJava@8cf384b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • designPatterns/src/test/java/com/javalearning
    • ModelTest.java
    • TestMain.java
  • pom.xml
  • springmvcexample
    • pom.xml
    • src
      • main
        • java/com/spring/mvc/evnentnotfy
          • EventPublishService.java
          • NotifyEvent.java
          • NotifyEventListener.java
        • resources
          • applicationContext.xml
        • webapp/WEB-INF
          • web.xml
      • test/com/spring/mvc/evnentnotfy
        • EventPublishServiceTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎designPatterns/src/test/java/com/javalearning/ModelTest.java‎

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.javalearning;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by renqun.yuan on 2016/3/2.
7+
*/
8+
public class ModelTest {
9+
private String key;
10+
private List<ModelTest> modelTestList;
11+
12+
public String getKey() {
13+
return key;
14+
}
15+
16+
public void setKey(String key) {
17+
this.key = key;
18+
}
19+
20+
public List<ModelTest> getModelTestList() {
21+
return modelTestList;
22+
}
23+
24+
public void setModelTestList(List<ModelTest> modelTestList) {
25+
this.modelTestList = modelTestList;
26+
}
27+
}
Collapse file

‎designPatterns/src/test/java/com/javalearning/TestMain.java‎

Copy file name to clipboard
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.javalearning;
2+
3+
import com.google.common.collect.Lists;
4+
import org.apache.commons.collections4.CollectionUtils;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Created by renqun.yuan on 2016/3/2.
10+
*/
11+
public class TestMain {
12+
public static void main(String[] args) {
13+
ModelTest modelTest = new ModelTest();
14+
modelTest.setKey("0MOde0");
15+
ModelTest modelTest1 = new ModelTest();
16+
modelTest1.setKey("0MOdel");
17+
modelTest1.setModelTestList(Lists.newArrayList(modelTest));
18+
ModelTest modelTest2 = new ModelTest();
19+
modelTest2.setKey("0MOde2");
20+
modelTest2.setModelTestList(Lists.<ModelTest>newArrayList(modelTest1));
21+
List<String> urls = Lists.newArrayList();
22+
23+
TestMain testMain = new TestMain();
24+
testMain.build(urls, modelTest2);
25+
System.out.println(urls);
26+
27+
}
28+
29+
private void build(List<String> urls , ModelTest modelTest) {
30+
if (modelTest == null) {
31+
return;
32+
}
33+
34+
urls.add(modelTest.getKey());
35+
36+
List<ModelTest> children = modelTest.getModelTestList();
37+
38+
if (CollectionUtils.isEmpty(children)) {
39+
return;
40+
}
41+
42+
for (ModelTest child : children) {
43+
build(urls, child);
44+
}
45+
}
46+
}
Collapse file

‎pom.xml‎

Copy file name to clipboardExpand all lines: pom.xml
+1-7Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<properties>
2424
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2525
<junit.version>3.8.1</junit.version>
26-
<guava.version>18.0</guava.version>
26+
<guava.version>19.0</guava.version>
2727
<spring-data-elasticsearch.version>1.2.0.RELEASE</spring-data-elasticsearch.version>
2828
<org.springframework.version>4.1.1.RELEASE</org.springframework.version>
2929
<commons-collections4.version>4.0</commons-collections4.version>
@@ -99,37 +99,31 @@
9999
<groupId>org.slf4j</groupId>
100100
<artifactId>slf4j-api</artifactId>
101101
<version>${org.slf4j.version}</version>
102-
<scope>runtime</scope>
103102
</dependency>
104103
<dependency>
105104
<groupId>org.slf4j</groupId>
106105
<artifactId>jcl-over-slf4j</artifactId>
107106
<version>${org.slf4j.version}</version>
108-
<scope>runtime</scope>
109107
</dependency>
110108
<dependency>
111109
<groupId>org.slf4j</groupId>
112110
<artifactId>jul-to-slf4j</artifactId>
113111
<version>${org.slf4j.version}</version>
114-
<scope>runtime</scope>
115112
</dependency>
116113
<dependency>
117114
<groupId>org.slf4j</groupId>
118115
<artifactId>log4j-over-slf4j</artifactId>
119116
<version>${org.slf4j.version}</version>
120-
<scope>runtime</scope>
121117
</dependency>
122118
<dependency>
123119
<groupId>ch.qos.logback</groupId>
124120
<artifactId>logback-classic</artifactId>
125121
<version>${logback.version}</version>
126-
<scope>runtime</scope>
127122
</dependency>
128123
<dependency>
129124
<groupId>ch.qos.logback</groupId>
130125
<artifactId>logback-core</artifactId>
131126
<version>${logback.version}</version>
132-
<scope>runtime</scope>
133127
</dependency>
134128
<dependency>
135129
<groupId>javax.servlet</groupId>
Collapse file

‎springmvcexample/pom.xml‎

Copy file name to clipboardExpand all lines: springmvcexample/pom.xml
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,11 @@
8585
<groupId>javax.servlet</groupId>
8686
<artifactId>jstl</artifactId>
8787
</dependency>
88+
<dependency>
89+
<groupId>junit</groupId>
90+
<artifactId>junit</artifactId>
91+
<version>4.12</version>
92+
<scope>test</scope>
93+
</dependency>
8894
</dependencies>
8995
</project>
Collapse file

‎springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/EventPublishService.java‎

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.spring.mvc.evnentnotfy;
2+
3+
import org.springframework.context.ApplicationEventPublisher;
4+
import org.springframework.context.ApplicationEventPublisherAware;
5+
import org.springframework.stereotype.Service;
6+
7+
/**
8+
* Created by renqun.yuan on 2016/3/14.
9+
*/
10+
@Service
11+
public class EventPublishService implements ApplicationEventPublisherAware {
12+
private ApplicationEventPublisher applicationEventPublisher;
13+
14+
public void doSomething(String userName) {
15+
// do something
16+
applicationEventPublisher.publishEvent(new NotifyEvent(userName));
17+
}
18+
19+
/**
20+
* Set the ApplicationEventPublisher that this object runs in.
21+
* <p>Invoked after population of normal bean properties but before an init
22+
* callback like InitializingBean's afterPropertiesSet or a custom init-method.
23+
* Invoked before ApplicationContextAware's setApplicationContext.
24+
*
25+
* @param applicationEventPublisher event publisher to be used by this object
26+
*/
27+
@Override
28+
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
29+
this.applicationEventPublisher = applicationEventPublisher;
30+
}
31+
}
Collapse file

‎springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/NotifyEvent.java‎

Copy file name to clipboard
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.spring.mvc.evnentnotfy;
2+
3+
import org.springframework.context.ApplicationEvent;
4+
5+
/**
6+
* Created by renqun.yuan on 2016/3/14.
7+
*/
8+
public class NotifyEvent extends ApplicationEvent {
9+
private String userName;
10+
11+
/**
12+
* Create a new ApplicationEvent.
13+
*
14+
* @param source the component that published the event (never {@code null})
15+
*/
16+
public NotifyEvent(String source) {
17+
super(source);
18+
this.userName = source;
19+
}
20+
21+
public String getUserName() {
22+
return userName;
23+
}
24+
25+
public void setUserName(String userName) {
26+
this.userName = userName;
27+
}
28+
}
Collapse file

‎springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/NotifyEventListener.java‎

Copy file name to clipboard
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.spring.mvc.evnentnotfy;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.context.ApplicationListener;
6+
import org.springframework.stereotype.Service;
7+
8+
/**
9+
* Created by renqun.yuan on 2016/3/14.
10+
*/
11+
@Service
12+
public class NotifyEventListener implements ApplicationListener<NotifyEvent> {
13+
private static final Logger logger = LoggerFactory.getLogger(NotifyEventListener.class);
14+
15+
/**
16+
* Handle an application event.
17+
*
18+
* @param event the event to respond to
19+
*/
20+
@Override
21+
public void onApplicationEvent(NotifyEvent event) {
22+
String userName = event.getUserName();
23+
// todo something , maybe send email, here just logger some info
24+
logger.info("please say hello, {}", userName);
25+
}
26+
}
Collapse file

‎…in/webapp/WEB-INF/applicationContext.xml‎ ‎…rc/main/resources/applicationContext.xml‎springmvcexample/src/main/webapp/WEB-INF/applicationContext.xml renamed to springmvcexample/src/main/resources/applicationContext.xml springmvcexample/src/main/webapp/WEB-INF/applicationContext.xml renamed to springmvcexample/src/main/resources/applicationContext.xml

Copy file name to clipboard
File renamed without changes.
Collapse file

‎springmvcexample/src/main/webapp/WEB-INF/web.xml‎

Copy file name to clipboardExpand all lines: springmvcexample/src/main/webapp/WEB-INF/web.xml
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
55
version="3.1">
6-
<context-param>
7-
<param-name>contextConfigLocation</param-name>
8-
<param-value>/WEB-INF/applicationContext.xml</param-value>
9-
</context-param>
106
<filter>
117
<filter-name>encodingFilter</filter-name>
128
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
@@ -27,7 +23,10 @@
2723
<listener>
2824
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
2925
</listener>
30-
26+
<context-param>
27+
<param-name>contextConfigLocation</param-name>
28+
<param-value>classpath:applicationContext.xml</param-value>
29+
</context-param>
3130
<servlet>
3231
<servlet-name>dispatcher</servlet-name>
3332
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
Collapse file

‎springmvcexample/src/test/com/spring/mvc/evnentnotfy/EventPublishServiceTest.java‎

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.spring.mvc.evnentnotfy;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.test.context.ContextConfiguration;
6+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7+
8+
import javax.annotation.Resource;
9+
10+
/**
11+
* Created by renqun.yuan on 2016/3/14.
12+
*/
13+
@RunWith(SpringJUnit4ClassRunner.class)
14+
@ContextConfiguration({"classpath:applicationContext.xml"})
15+
public class EventPublishServiceTest {
16+
@Resource
17+
private EventPublishService eventPublishService;
18+
19+
@Test
20+
public void testPublishEvent() throws Exception {
21+
eventPublishService.doSomething("spring mvc ");
22+
}
23+
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.