File tree Expand file tree Collapse file tree 10 files changed +216
-14
lines changed
main/java/com/hand/observe Expand file tree Collapse file tree 10 files changed +216
-14
lines changed Original file line number Diff line number Diff line change 31
31
<groupId >org.springframework.boot</groupId >
32
32
<artifactId >spring-boot-autoconfigure</artifactId >
33
33
</dependency >
34
+ <dependency >
35
+ <groupId >org.springframework.boot</groupId >
36
+ <artifactId >spring-boot-starter-web</artifactId >
37
+ </dependency >
34
38
</dependencies >
35
39
36
40
<build >
Original file line number Diff line number Diff line change 1
- package com .hand .event ;
1
+ package com .hand .observe ;
2
2
3
3
import org .springframework .boot .SpringApplication ;
4
4
import org .springframework .boot .autoconfigure .SpringBootApplication ;
Original file line number Diff line number Diff line change
1
+ package com .hand .observe .bean ;
2
+
3
+ /**
4
+ * @author zijian.zeng@hand-china.com
5
+ * @since 2023-01-30
6
+ */
7
+ public class Order {
8
+
9
+ private long id ;
10
+
11
+ private String name ;
12
+
13
+ public Order () {
14
+ }
15
+
16
+ public Order (long id , String name ) {
17
+ this .id = id ;
18
+ this .name = name ;
19
+ }
20
+
21
+ public long getId () {
22
+ return id ;
23
+ }
24
+
25
+ public void setId (long id ) {
26
+ this .id = id ;
27
+ }
28
+
29
+ public String getName () {
30
+ return name ;
31
+ }
32
+
33
+ public void setName (String name ) {
34
+ this .name = name ;
35
+ }
36
+
37
+ @ Override
38
+ public String toString () {
39
+ return "Order{" +
40
+ "id=" + id +
41
+ ", name='" + name + '\'' +
42
+ '}' ;
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ package com .hand .observe .controller ;
2
+
3
+ import com .hand .observe .bean .Order ;
4
+ import com .hand .observe .service .OrderService ;
5
+ import org .springframework .beans .factory .annotation .Autowired ;
6
+ import org .springframework .web .bind .annotation .RequestMapping ;
7
+ import org .springframework .web .bind .annotation .RestController ;
8
+
9
+ /**
10
+ * @author zijian.zeng@hand-china.com
11
+ * @since 2023-01-30
12
+ */
13
+ @ RestController
14
+ @ RequestMapping ("/order" )
15
+ public class OrderController {
16
+
17
+
18
+ @ Autowired
19
+ private OrderService orderService ;
20
+
21
+
22
+ @ RequestMapping ("/buy" )
23
+ public Order getOrder () {
24
+ Order order = new Order (1 , "aaa" );
25
+ orderService .buy (order );
26
+ return order ;
27
+ }
28
+
29
+
30
+
31
+ }
Original file line number Diff line number Diff line change
1
+ package com .hand .observe .event ;
2
+
3
+ import com .hand .observe .bean .Order ;
4
+ import org .springframework .context .ApplicationEvent ;
5
+
6
+ /** 事件
7
+ * @author zijian.zeng@hand-china.com
8
+ * @since 2023-01-30
9
+ */
10
+ public class OrderSuccessEvent extends ApplicationEvent {
11
+
12
+ private Order order ;
13
+
14
+ /**
15
+ * Create a new {@code ApplicationEvent}.
16
+ *
17
+ * @param source the object on which the event initially occurred or with
18
+ * which the event is associated (never {@code null})
19
+ */
20
+ public OrderSuccessEvent (Object source ) {
21
+ super (source );
22
+ }
23
+
24
+ public OrderSuccessEvent (Object source , Order order ) {
25
+ super (source );
26
+ this .order = order ;
27
+ }
28
+
29
+ public Order getOrder () {
30
+ return order ;
31
+ }
32
+
33
+ public void setOrder (Order order ) {
34
+ this .order = order ;
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+ package com .hand .observe .listener ;
2
+
3
+ import com .hand .observe .event .OrderSuccessEvent ;
4
+ import org .slf4j .Logger ;
5
+ import org .slf4j .LoggerFactory ;
6
+ import org .springframework .context .ApplicationListener ;
7
+ import org .springframework .stereotype .Component ;
8
+
9
+
10
+ /**
11
+ * 发送短信的事件监听器
12
+ *
13
+ * @author zijian.zeng@hand-china.com
14
+ * @since 2023-01-30
15
+ */
16
+ @ Component
17
+ public class SmsListener implements ApplicationListener <OrderSuccessEvent > {
18
+
19
+
20
+ Logger logger = LoggerFactory .getLogger (SmsListener .class );
21
+
22
+ @ Override
23
+ public void onApplicationEvent (OrderSuccessEvent event ) {
24
+ logger .info ("短信发送成功....." + event .getOrder ().getName ());
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ package com .hand .observe .service ;
2
+
3
+ import com .hand .observe .bean .Order ;
4
+
5
+ /**
6
+ * @author zijian.zeng@hand-china.com
7
+ * @since 2023-01-30
8
+ */
9
+ public interface OrderService {
10
+
11
+ void buy (Order order );
12
+ }
Original file line number Diff line number Diff line change
1
+ package com .hand .observe .service .impl ;
2
+
3
+ import com .hand .observe .bean .Order ;
4
+ import com .hand .observe .event .OrderSuccessEvent ;
5
+ import com .hand .observe .service .OrderService ;
6
+ import org .slf4j .Logger ;
7
+ import org .slf4j .LoggerFactory ;
8
+ import org .springframework .context .ApplicationEventPublisher ;
9
+ import org .springframework .context .ApplicationEventPublisherAware ;
10
+ import org .springframework .stereotype .Service ;
11
+
12
+ /**
13
+ * @author zijian.zeng@hand-china.com
14
+ * @since 2023-01-30
15
+ */
16
+
17
+ @ Service
18
+ public class OrderServiceImpl implements OrderService , ApplicationEventPublisherAware {
19
+
20
+
21
+ Logger logger = LoggerFactory .getLogger (OrderServiceImpl .class );
22
+
23
+ private ApplicationEventPublisher publisher ;
24
+
25
+ @ Override
26
+ public void buy (Order order ) {
27
+ logger .info ("下单成功: " + order .getName ());
28
+ // 发布事件
29
+ publisher .publishEvent (new OrderSuccessEvent (this , order ));
30
+ }
31
+
32
+ @ Override
33
+ public void setApplicationEventPublisher (ApplicationEventPublisher applicationEventPublisher ) {
34
+ this .publisher = applicationEventPublisher ;
35
+ }
36
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ package com .hand .observe ;
2
+
3
+ import com .hand .observe .bean .Order ;
4
+ import com .hand .observe .service .OrderService ;
5
+ import com .hand .observe .service .impl .OrderServiceImpl ;
6
+ import org .junit .jupiter .api .Test ;
7
+ import org .junit .runner .RunWith ;
8
+ import org .springframework .beans .factory .annotation .Autowired ;
9
+ import org .springframework .boot .test .context .SpringBootTest ;
10
+ import org .springframework .test .context .junit4 .SpringRunner ;
11
+
12
+ @ SpringBootTest
13
+ @ RunWith (SpringRunner .class )
14
+ class ObserveApplicationTests {
15
+
16
+ @ Autowired
17
+ private OrderService orderServiceImpl ;
18
+
19
+
20
+ @ Test
21
+ void contextLoads () {
22
+ Order o = new Order (1 , "aaa" );
23
+ orderServiceImpl .buy (o );
24
+ }
25
+
26
+ }
You can’t perform that action at this time.
0 commit comments