Skip to content

Commit ababaef

Browse files
committed
【Spring】Spring常用配置-事件(Application Event)
1 parent 2ccb6cc commit ababaef

12 files changed

Lines changed: 399 additions & 125 deletions

File tree

‎springBoot/.idea/workspace.xml‎

Lines changed: 162 additions & 125 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.hncu.p2_5_2ApplicationEvent;
2+
3+
import org.springframework.context.ApplicationEvent;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: 陈浩翔.
8+
* Date: 2016/11/15.
9+
* Time: 下午 8:39.
10+
* Explain:自定义事件
11+
*/
12+
public class DemoEvent extends ApplicationEvent{
13+
private static final long serialVersionUID = 1L;
14+
private String msg;
15+
16+
public DemoEvent(Object source,String msg) {
17+
super(source);
18+
this.msg=msg;
19+
System.out.println(this.getClass()+",构造方法");
20+
}
21+
22+
public String getMsg() {
23+
return msg;
24+
}
25+
26+
public void setMsg(String msg) {
27+
this.msg = msg;
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.hncu.p2_5_2ApplicationEvent;
2+
3+
import org.springframework.context.ApplicationListener;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* Created with IntelliJ IDEA.
8+
* User: 陈浩翔.
9+
* Date: 2016/11/15.
10+
* Time: 下午 8:50.
11+
* Explain:事件监听器
12+
*/
13+
@Component
14+
public class DemoListener implements ApplicationListener<DemoEvent> {//实现ApplicationListener接口,并指定监听的事件类型
15+
@Override
16+
public void onApplicationEvent(DemoEvent demoEvent) {//使用onApplicationEvent方法对消息进行接受处理
17+
String msg = demoEvent.getMsg();
18+
System.out.println(this.getClass()+"监听到了bean-demoPublisher发布的消息:"+msg);
19+
//监听到之后可以做其他事
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.hncu.p2_5_2ApplicationEvent;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.stereotype.Component;
6+
7+
/**
8+
* Created with IntelliJ IDEA.
9+
* User: 陈浩翔.
10+
* Date: 2016/11/15.
11+
* Time: 下午 10:39.
12+
* Explain:事件发布类
13+
*/
14+
@Component
15+
public class DemoPublisher {
16+
@Autowired
17+
ApplicationContext applicationContext;//注入ApplicationContext用来发布事件
18+
19+
public void publish(String msg){
20+
applicationContext.publishEvent(new DemoEvent(this,msg));
21+
//使用ApplicationContext的publishEvent方法来发布
22+
}
23+
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.hncu.p2_5_2ApplicationEvent;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
/**
7+
* Created with IntelliJ IDEA.
8+
* User: 陈浩翔.
9+
* Date: 2016/11/15.
10+
* Time: 下午 10:46.
11+
* Explain:配置类
12+
*/
13+
@Configuration
14+
@ComponentScan("cn.hncu.p2_5_2ApplicationEvent")
15+
public class EventConfig {
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.hncu.p2_5_2ApplicationEvent;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: 陈浩翔.
8+
* Date: 2016/11/15.
9+
* Time: 下午 10:47.
10+
* Explain:运行类
11+
*/
12+
public class Main {
13+
public static void main(String[] args) {
14+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
15+
DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
16+
demoPublisher.publish("hello 你好...");
17+
context.close();
18+
}
19+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#分析
2+
对于Profile先做一个简单的介绍:
3+
单讲profile就是一组配置,不同profile提供不同组合的配置,程序运行时可以选择使用哪些profile来适应环境。
4+
5+
也就是Profile为在不同环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的,例如:数据库的配置)
6+
7+
8+
Spring 为我们提供了大量的激活 profile 的方法,可以通过代码来激活,也可以通过系统环境变量、JVM参数、servlet上下文参数来定义 spring.profiles.active 参数激活 profile,下面说下3种方法:
9+
1、通过设定Environment的ActiveProfiles来设定当前context需要使用的配置环境。在开发中使用@profile注解类或者方法,达到在不同情况下选择实例化不同的Bean。
10+
2、通过设定jvm的spring.profile.active参数来设置配置环境。
11+
3、Web项目设置在Service的context parameter中。
12+
13+
14+
进行本示例的演示,需要先配置好Maven和Spring哦、
15+
见:
16+
<a href="http://blog.csdn.net/qq_26525215/article/details/53010442" target='_blank'>【Spring】基于IntelliJ IDEA搭建Maven</a>
17+
18+
在这里的示例只演示第一种方式的示例哦。
19+
20+
#示例
21+
22+
##示例Bean
23+
24+
```
25+
package cn.hncu.p2_4_2Profile;
26+
27+
/**
28+
* Created with IntelliJ IDEA.
29+
* User: 陈浩翔.
30+
* Date: 2016/11/14.
31+
* Time: 下午 8:37.
32+
* Explain:示例Bean
33+
*/
34+
public class DemoBean {
35+
public String content;
36+
37+
public DemoBean(String content) {
38+
super();
39+
this.content = content;
40+
}
41+
public String getContent(){
42+
return content;
43+
}
44+
public void setContent(String content) {
45+
this.content = content;
46+
}
47+
}
48+
49+
```
50+
51+
##Profile配置
52+
53+
```
54+
package cn.hncu.p2_4_2Profile;
55+
56+
import org.springframework.context.annotation.Bean;
57+
import org.springframework.context.annotation.Configuration;
58+
import org.springframework.context.annotation.Profile;
59+
60+
/**
61+
* Created with IntelliJ IDEA.
62+
* User: 陈浩翔.
63+
* Date: 2016/11/14.
64+
* Time: 下午 8:41.
65+
* Explain:Profile配置
66+
*/
67+
@Configuration
68+
public class ProfileConfig {
69+
70+
@Bean
71+
@Profile("dev")//Profile为dev时实例化devDemoBean
72+
public DemoBean devDemoBean(){
73+
return new DemoBean("from development profile");
74+
}
75+
76+
@Bean
77+
@Profile("prod")//Profile为prod时实例化prodDemoBean
78+
public DemoBean prodDemoBean(){
79+
return new DemoBean("from production profile");
80+
}
81+
}
82+
83+
```
84+
85+
##运行类
86+
87+
```
88+
package cn.hncu.p2_4_2Profile;
89+
90+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
91+
92+
/**
93+
* Created with IntelliJ IDEA.
94+
* User: 陈浩翔.
95+
* Date: 2016/11/14.
96+
* Time: 下午 8:51.
97+
* Explain:运行类
98+
*/
99+
public class Main {
100+
101+
public static void main(String[] args) {
102+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
103+
context.getEnvironment().setActiveProfiles("prod");//先将活动的Proofile设置为prod
104+
context.register(ProfileConfig.class);//后置注册Bean的配置类,不然会报Bean未定义的错误
105+
context.refresh();//刷新容器
106+
107+
DemoBean demoBean = context.getBean(DemoBean.class);
108+
109+
System.out.println(demoBean.getContent());
110+
111+
context.close();
112+
}
113+
}
114+
115+
```
116+
##运行结果
117+
118+
![](http://img.blog.csdn.net/20161114214508240)
119+
120+
现在来对Main类做一下改动,将
121+
context.getEnvironment().setActiveProfiles("prod")
122+
修改为
123+
context.getEnvironment().setActiveProfiles("dev")
124+
125+
126+
结果如下:
127+
128+
![](http://img.blog.csdn.net/20161114214722365)
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)