spring開發_BeanPostProcessor_Bean后處理器


項目結構:

http://www.cnblogs.com/hongten/gallery/image/112581.html

/spring_1600_Bean后處理器/src/com/b510/app/test/SpringTest.java

 1 package com.b510.app.test;
2
3 import org.springframework.beans.factory.xml.XmlBeanFactory;
4 import org.springframework.context.ApplicationContext;
5 import org.springframework.context.support.ClassPathXmlApplicationContext;
6 import org.springframework.core.io.ClassPathResource;
7
8 import com.b510.app.util.AppBeanPostProcessor;
9 import com.b510.service.AnimalService;
10
11 /**
12 * 測試類,這里使用了ApplicationContext作為容器,無需手動注冊BeanPostProcessor
13 * registerManually()方法提供的是手動注冊BeanPostProcessor
14 *
15 * @author Hongten
16 *
17 */
18 public class SpringTest {
19 public static void main(String[] args) {
20 ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
21 // 獲取id為animaleServiceOfDog的Bean
22 AnimalService animalServiceOfDog = (AnimalService) act
23 .getBean("animaleServiceOfDog");
24 animalServiceOfDog.getInfo();
25 // 獲取id為animaleServiceOfCat的Bean
26 AnimalService animalServiceOfCat = (AnimalService) act
27 .getBean("animaleServiceOfCat");
28 animalServiceOfCat.getInfo();
29 System.out
30 .println("*************手動注冊BeanPostProcessor處理結果********************");
31 registerManually();
32 }
33
34 /**
35 * 手動注冊BeanPostProcessor
36 */
37 public static void registerManually() {
38 // CLASSPATH路徑下的bean.xml文件創建Resource對象
39 ClassPathResource isr = new ClassPathResource("beans.xml");
40 // 以Resource對象作為參數,創建BeanFactory的實例
41 XmlBeanFactory factory = new XmlBeanFactory(isr);
42 // 獲取Bean后處理器實例
43 AppBeanPostProcessor prr = (AppBeanPostProcessor) factory.getBean(
44 "appBeanPostProcessor", AppBeanPostProcessor.class);
45 // 注冊BeanPostProcessor實例
46 factory.addBeanPostProcessor(prr);
47 // 獲取id為animaleServiceOfDog的Bean
48 AnimalService animalServiceOfDog = (AnimalService) factory
49 .getBean("animaleServiceOfDog");
50 animalServiceOfDog.getInfo();
51 // 獲取id為animaleServiceOfCat的Bean
52 AnimalService animalServiceOfCat = (AnimalService) factory
53 .getBean("animaleServiceOfCat");
54 animalServiceOfCat.getInfo();
55 }
56 }

/spring_1600_Bean后處理器/src/com/b510/app/util/AppBeanPostProcessor.java

 1 package com.b510.app.util;
2
3 import org.springframework.beans.BeansException;
4 import org.springframework.beans.factory.config.BeanPostProcessor;
5
6 import com.b510.service.impl.CatServiceBean;
7 import com.b510.service.impl.DogServiceBean;
8 import com.b510.service.impl.FishServiceBean;
9 import com.b510.service.impl.PorkServiceBean;
10
11 public class AppBeanPostProcessor implements BeanPostProcessor {
12
13 /**
14 * 對容器中的Bean實例進行后處理 參數一:需要進行后處理的原Bean實例 參數二:需要進行處理的Bean實例的名字 返回:處理完成后的Bean
15 */
16 @Override
17 public Object postProcessAfterInitialization(Object bean, String beanName)
18 throws BeansException {
19 System.out.println("postProcessAfterInitialization方法,被處理的Bean的名稱為:" + beanName);
20 return bean;
21 }
22
23 /**
24 * 對容器中的Bean實例進行后處理,給方法是在postProcessAfterInitialization()之前調用
25 * 參數一:需要進行后處理的原Bean實例 參數二:需要進行處理的Bean實例的名字 返回:處理完成后的Bean
26 */
27 @Override
28 public Object postProcessBeforeInitialization(Object bean, String beanName)
29 throws BeansException {
30 // 如果該Bean是FishServiceBean的實例
31 if (bean instanceof FishServiceBean) {
32 System.out.println("魚肉Bean被后處理器初始化");
33 }
34 if (bean instanceof PorkServiceBean) {
35 System.out.println("豬肉Bean被后處理器初始化");
36 }
37 if (bean instanceof DogServiceBean) {
38 System.out.println("狗類Bean被后處理器初始化");
39 }
40 if (bean instanceof CatServiceBean) {
41 System.out.println("貓類Bean被后處理器初始化");
42 }
43 return bean;
44 }
45 }

/spring_1600_Bean后處理器/src/com/b510/service/AnimalService.java

 1 package com.b510.service;
2
3 /**
4 * 動物抽象類
5 *
6 * @author Hongten
7 *
8 */
9 public interface AnimalService {
10
11 /**
12 * 獲取相關信息
13 */
14 public abstract void getInfo();
15
16 }

/spring_1600_Bean后處理器/src/com/b510/service/MeatService.java

 1 package com.b510.service;
2
3 /**
4 * 定義抽象類MeatService肉類
5 *
6 * @author Hongten
7 *
8 */
9 public interface MeatService {
10
11 /**
12 * 定義方法whatMeat
13 *
14 * @return 返回一個字符串
15 */
16 public abstract String whatMeat();
17 }

/spring_1600_Bean后處理器/src/com/b510/service/impl/CatServiceBean.java

 1 package com.b510.service.impl;
2
3 import com.b510.service.AnimalService;
4 import com.b510.service.MeatService;
5
6 /**
7 * 定義貓類實現AnimaleService抽象類
8 *
9 * @author Hongten
10 *
11 */
12 public class CatServiceBean implements AnimalService {
13 private int age;
14 private MeatService meatService;
15
16 public CatServiceBean(){
17 System.out.println("貓類被初始化了");
18 }
19
20 public int getAge() {
21 return age;
22 }
23
24 @Override
25 public void getInfo() {
26 System.out.println("我是貓,我的年齡是:"+age+",我很喜歡吃"+meatService.whatMeat());
27 }
28 public MeatService getMeatService() {
29 return meatService;
30 }
31
32 public void setAge(int age) {
33 this.age = age;
34 }
35
36 public void setMeatService(MeatService meatService) {
37 this.meatService = meatService;
38 }
39
40
41 }

/spring_1600_Bean后處理器/src/com/b510/service/impl/DogServiceBean.java

 1 package com.b510.service.impl;
2
3 import com.b510.service.AnimalService;
4 import com.b510.service.MeatService;
5
6 /**
7 * 定義DogServiceBean類
8 *
9 * @author Hongten
10 *
11 */
12 public class DogServiceBean implements AnimalService {
13 private int age;
14 private MeatService meatService;
15
16 public DogServiceBean() {
17 System.out.println("狗類被初始化了");
18 }
19
20 public int getAge() {
21 return age;
22 }
23
24 @Override
25 public void getInfo() {
26 System.out.println("我是狗,我的年齡是:" + age + ",我很喜歡吃"
27 + meatService.whatMeat());
28 }
29
30 public MeatService getMeatService() {
31 return meatService;
32 }
33
34 public void setAge(int age) {
35 this.age = age;
36 }
37
38 public void setMeatService(MeatService meatService) {
39 this.meatService = meatService;
40 }
41
42 }

/spring_1600_Bean后處理器/src/com/b510/service/impl/FishServiceBean.java

 1 package com.b510.service.impl;
2
3 import com.b510.service.MeatService;
4
5 /**
6 * 定義魚肉實現MeatService抽象類
7 *
8 * @author Hongten
9 *
10 */
11 public class FishServiceBean implements MeatService {
12
13 public FishServiceBean(){
14 System.out.println("魚肉類被初始化了");
15 }
16 @Override
17 public String whatMeat() {
18 return "魚肉";
19 }
20
21 }

/spring_1600_Bean后處理器/src/com/b510/service/impl/PorkServiceBean.java

 1 package com.b510.service.impl;
2
3 import com.b510.service.MeatService;
4
5 /**
6 * 定義豬肉實現MeatService抽象類
7 *
8 * @author Hongten
9 *
10 */
11 public class PorkServiceBean implements MeatService {
12
13 public PorkServiceBean(){
14 System.out.println("豬肉類被初始化了");
15 }
16 @Override
17 public String whatMeat() {
18 return "豬肉";
19 }
20
21 }

/spring_1600_Bean后處理器/src/beans.xml

 1 <?xml version="1.0" encoding="GBK"?>
2 <!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd語義約束 -->
3 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
7
8 <bean id="meatServiceOfFish" class="com.b510.service.impl.FishServiceBean"></bean>
9 <bean id="meatServiceOfPork" class="com.b510.service.impl.PorkServiceBean"></bean>
10 <bean id="animaleServiceOfDog" class="com.b510.service.impl.DogServiceBean"
11 p:age="12" p:meatService-ref="meatServiceOfPork" />
12 <bean id="animaleServiceOfCat" class="com.b510.service.impl.CatServiceBean"
13 p:age="3" p:meatService-ref="meatServiceOfFish" />
14 <!-- 配置Bean后處理器的時候,無需指定id屬性,這里主要是為了展示手動注冊才需要配置id屬性 -->
15 <bean id="appBeanPostProcessor" class="com.b510.app.util.AppBeanPostProcessor"></bean>
16 </beans>

運行結果;

 1 2012-3-12 15:41:10 org.springframework.context.support.AbstractApplicationContext prepareRefresh
2 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19c26f5: display name [org.springframework.context.support.ClassPathXmlApplicationContext@19c26f5]; startup date [Mon Mar 12 15:41:10 CST 2012]; root of context hierarchy
3 2012-3-12 15:41:10 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
4 信息: Loading XML bean definitions from class path resource [beans.xml]
5 2012-3-12 15:41:13 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
6 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@19c26f5]: org.springframework.beans.factory.support.DefaultListableBeanFactory@4a63d8
7 2012-3-12 15:41:13 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
8 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4a63d8: defining beans [meatServiceOfFish,meatServiceOfPork,animaleServiceOfDog,animaleServiceOfCat,appBeanPostProcessor]; root of factory hierarchy
9 魚肉類被初始化了
10 魚肉Bean被后處理器初始化
11 postProcessAfterInitialization方法,被處理的Bean的名稱為:meatServiceOfFish
12 豬肉類被初始化了
13 豬肉Bean被后處理器初始化
14 postProcessAfterInitialization方法,被處理的Bean的名稱為:meatServiceOfPork
15 狗類被初始化了
16 狗類Bean被后處理器初始化
17 postProcessAfterInitialization方法,被處理的Bean的名稱為:animaleServiceOfDog
18 貓類被初始化了
19 貓類Bean被后處理器初始化
20 postProcessAfterInitialization方法,被處理的Bean的名稱為:animaleServiceOfCat
21 我是狗,我的年齡是:12,我很喜歡吃豬肉
22 我是貓,我的年齡是:3,我很喜歡吃魚肉
23 *************手動注冊BeanPostProcessor處理結果********************
24 2012-3-12 15:41:13 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
25 信息: Loading XML bean definitions from class path resource [beans.xml]
26 狗類被初始化了
27 豬肉類被初始化了
28 豬肉Bean被后處理器初始化
29 postProcessAfterInitialization方法,被處理的Bean的名稱為:meatServiceOfPork
30 狗類Bean被后處理器初始化
31 postProcessAfterInitialization方法,被處理的Bean的名稱為:animaleServiceOfDog
32 我是狗,我的年齡是:12,我很喜歡吃豬肉
33 貓類被初始化了
34 魚肉類被初始化了
35 魚肉Bean被后處理器初始化
36 postProcessAfterInitialization方法,被處理的Bean的名稱為:meatServiceOfFish
37 貓類Bean被后處理器初始化
38 postProcessAfterInitialization方法,被處理的Bean的名稱為:animaleServiceOfCat
39 我是貓,我的年齡是:3,我很喜歡吃魚肉

我們可以看到,用ApplicationContext作為容器注冊BeanPostProcessor的時候,他會全部裝入容器后,在進行其他操作;

而我們手動注冊BeanPostProcessor的時候,系統需要什么Bean的時候,就去調用相應的Bean;手動注冊的BeanPostProcessor

不會一下子就全部裝入所有的Bean。

這是我個人的理解,如果大家有不同的觀點或看法,請大家多多指教……

大家一起討論,這樣大家才會一起進步。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM