Skip to content

Commit 6bdc1dc

Browse files
committed
【Spring】Spring常用配置-Bean的初始化和销毁(生命周期)
1 parent 0742845 commit 6bdc1dc

14 files changed

Lines changed: 606 additions & 164 deletions

File tree

‎springBoot/.idea/libraries/Maven__javax_annotation_jsr250_api_1_0.xml‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎springBoot/.idea/workspace.xml‎

Lines changed: 124 additions & 162 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎springBoot/pom.xml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<version>4.2.3.RELEASE</version>
1818
</dependency>
1919

20-
<!-- spring aop 支持-->
20+
<!-- spring aop 支持 p1_3_3_aop-->
2121
<dependency>
2222
<groupId>org.springframework</groupId>
2323
<artifactId>spring-aop</artifactId>
@@ -35,13 +35,21 @@
3535
<version>1.8.9</version>
3636
</dependency>
3737

38-
<!--简化文件操作-commons-io-->
38+
<!--简化文件操作-commons-io p2_2_2SpringEL-->
3939
<dependency>
4040
<groupId>commons-io</groupId>
4141
<artifactId>commons-io</artifactId>
4242
<version>2.4</version>
4343
</dependency>
4444

45+
<!--JSR250支持 p2_3_2BeanLifecycle-->
46+
<dependency>
47+
<groupId>javax.annotation</groupId>
48+
<artifactId>jsr250-api</artifactId>
49+
<version>1.0</version>
50+
</dependency>
51+
52+
4553
</dependencies>
4654
<build>
4755
<plugins>

‎springBoot/springBoot.iml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
<orderEntry type="library" name="Maven: org.aspectj:aspectjrt:1.8.9" level="project" />
2222
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.8.9" level="project" />
2323
<orderEntry type="library" name="Maven: commons-io:commons-io:2.4" level="project" />
24+
<orderEntry type="library" name="Maven: javax.annotation:jsr250-api:1.0" level="project" />
2425
</component>
2526
</module>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cn.hncu.p2_3_2BeanLifecycle;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: 陈浩翔.
6+
* Date: 2016/11/14.
7+
* Time: 上午 11:19.
8+
* Explain:使用@Bean形式的Bean
9+
*/
10+
public class BeanWayService {
11+
public BeanWayService() {
12+
super();
13+
System.out.println("初始化构造函数-BeanWayService:"+this.getClass());
14+
}
15+
16+
public void init(){
17+
System.out.println("BeanWayService-init方法");
18+
}
19+
20+
public void destroy(){
21+
System.out.println("BeanWayService-destroy方法");
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.hncu.p2_3_2BeanLifecycle;
2+
3+
import javax.annotation.PostConstruct;
4+
import javax.annotation.PreDestroy;
5+
6+
/**
7+
* Created with IntelliJ IDEA.
8+
* User: 陈浩翔.
9+
* Date: 2016/11/14.
10+
* Time: 上午 11:23.
11+
* Explain:使用JSR250形式的Bean
12+
*/
13+
public class JSR250WayService {
14+
public JSR250WayService() {
15+
super();
16+
System.out.println("初始化构造函数-JSR250WayService:"+this.getClass());
17+
}
18+
19+
@PostConstruct//@PostConstruct这个注解表明该方法在构造函数执行之后执行
20+
public void init(){
21+
System.out.println("jsr250-init方法");
22+
}
23+
24+
@PreDestroy//这个注解表明该方法在Bean销毁之前执行
25+
public void destroy(){
26+
System.out.println("jsr250-destroy方法");
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.hncu.p2_3_2BeanLifecycle;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: 陈浩翔.
8+
* Date: 2016/11/14.
9+
* Time: 上午 11:33.
10+
* Explain:运行类
11+
*/
12+
public class Main {
13+
public static void main(String[] args) {
14+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PrePostConfig.class);
15+
16+
BeanWayService beanWayService = context.getBean(BeanWayService.class);
17+
JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class);
18+
19+
context.close();
20+
}
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cn.hncu.p2_3_2BeanLifecycle;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
/**
8+
* Created with IntelliJ IDEA.
9+
* User: 陈浩翔.
10+
* Date: 2016/11/14.
11+
* Time: 上午 11:29.
12+
* Explain:配置类
13+
*/
14+
@Configuration
15+
@ComponentScan("cn.hncu.p2_3_2BeanLifecycle")
16+
public class PrePostConfig {
17+
18+
//为BeanWayService准备的配置方法
19+
//initMethod和destroyMethod指定BeanWayService类的init和destroy方法在构造之后、Bean销毁之前执行
20+
@Bean(initMethod = "init",destroyMethod = "destroy")
21+
BeanWayService beanWayService(){
22+
return new BeanWayService();
23+
}
24+
25+
//为JSR250WayService准备的配置方法
26+
@Bean
27+
JSR250WayService jsr250WayService(){
28+
return new JSR250WayService();
29+
}
30+
31+
32+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
2+
#分析
3+
4+
在我们实际开发的时候,经常会遇到在Bean使用之前或者之后做些必要的操作,Spring对Bean的生命周期的操作提供了支持。
5+
6+
有如下2种方式:
7+
1、Java配置方式:使用@Bean的initMethod和destroyMethod(相当于xml配置的init-method和destroy-method)
8+
9+
2、注解方式:
10+
利用JSR-250的@PostConstruct和@PreDestroy
11+
12+
友情提示:
13+
进行本示例的演示,需要先配置好Maven和Spring哦、
14+
见:<a href="http://blog.csdn.net/qq_26525215/article/details/53010442" target='_blank'>【Spring】基于IntelliJ IDEA搭建Maven</a>
15+
16+
17+
18+
#示例
19+
20+
##增加JSR250支持
21+
22+
```
23+
<dependency>
24+
<groupId>javax.annotation</groupId>
25+
<artifactId>jsr250-api</artifactId>
26+
<version>1.0</version>
27+
</dependency>
28+
```
29+
30+
##使用@Bean形式的Bean
31+
32+
```
33+
package cn.hncu.p2_3_2BeanLifecycle;
34+
35+
/**
36+
* Created with IntelliJ IDEA.
37+
* User: 陈浩翔.
38+
* Date: 2016/11/14.
39+
* Time: 上午 11:19.
40+
* Explain:使用@Bean形式的Bean
41+
*/
42+
public class BeanWayService {
43+
public BeanWayService() {
44+
super();
45+
System.out.println("初始化构造函数-BeanWayService:"+this.getClass());
46+
}
47+
48+
public void init(){
49+
System.out.println("BeanWayService-init方法");
50+
}
51+
52+
public void destroy(){
53+
System.out.println("BeanWayService-destroy方法");
54+
}
55+
}
56+
57+
```
58+
59+
##使用JSR250形式的Bean
60+
61+
```
62+
package cn.hncu.p2_3_2BeanLifecycle;
63+
64+
import javax.annotation.PostConstruct;
65+
import javax.annotation.PreDestroy;
66+
67+
/**
68+
* Created with IntelliJ IDEA.
69+
* User: 陈浩翔.
70+
* Date: 2016/11/14.
71+
* Time: 上午 11:23.
72+
* Explain:使用JSR250形式的Bean
73+
*/
74+
public class JSR250WayService {
75+
public JSR250WayService() {
76+
super();
77+
System.out.println("初始化构造函数-JSR250WayService:"+this.getClass());
78+
}
79+
80+
@PostConstruct//@PostConstruct这个注解表明该方法在构造函数执行之后执行
81+
public void init(){
82+
System.out.println("jsr250-init方法");
83+
}
84+
85+
@PreDestroy//这个注解表明该方法在Bean销毁之前执行
86+
public void destroy(){
87+
System.out.println("jsr250-destroy方法");
88+
}
89+
}
90+
91+
```
92+
93+
##配置类
94+
95+
```
96+
package cn.hncu.p2_3_2BeanLifecycle;
97+
98+
import org.springframework.context.annotation.Bean;
99+
import org.springframework.context.annotation.ComponentScan;
100+
import org.springframework.context.annotation.Configuration;
101+
102+
/**
103+
* Created with IntelliJ IDEA.
104+
* User: 陈浩翔.
105+
* Date: 2016/11/14.
106+
* Time: 上午 11:29.
107+
* Explain:配置类
108+
*/
109+
@Configuration
110+
@ComponentScan("cn.hncu.p2_3_2BeanLifecycle")
111+
public class PrePostConfig {
112+
113+
//为BeanWayService准备的配置方法
114+
//initMethod和destroyMethod指定BeanWayService类的init和destroy方法在构造之后、Bean销毁之前执行
115+
@Bean(initMethod = "init",destroyMethod = "destroy")
116+
BeanWayService beanWayService(){
117+
return new BeanWayService();
118+
}
119+
120+
//为JSR250WayService准备的配置方法
121+
@Bean
122+
JSR250WayService jsr250WayService(){
123+
return new JSR250WayService();
124+
}
125+
126+
127+
}
128+
129+
```
130+
131+
##运行类
132+
133+
```
134+
package cn.hncu.p2_3_2BeanLifecycle;
135+
136+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
137+
138+
/**
139+
* Created with IntelliJ IDEA.
140+
* User: 陈浩翔.
141+
* Date: 2016/11/14.
142+
* Time: 上午 11:33.
143+
* Explain:运行类
144+
*/
145+
public class Main {
146+
public static void main(String[] args) {
147+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PrePostConfig.class);
148+
149+
BeanWayService beanWayService = context.getBean(BeanWayService.class);
150+
JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class);
151+
152+
context.close();
153+
}
154+
}
155+
156+
```
157+
158+
#运行结果
159+
160+
![](http://img.blog.csdn.net/20161114192851789)
161+

0 commit comments

Comments
 (0)