|
| 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 | + |
| 161 | + |
0 commit comments