前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring开发_BeanFactoryPostProcessor_容器后处理器

spring开发_BeanFactoryPostProcessor_容器后处理器

作者头像
Hongten
发布2018-09-13 16:48:15
3000
发布2018-09-13 16:48:15
举报
文章被收录于专栏:HongtenHongten

项目结构:

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

/spring_1700_容器后处理器/src/com/b510/app/test/SpringTest.java

代码语言:javascript
复制
 1 package com.b510.app.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.b510.service.AnimalService;
 7 
 8 /**
 9  * 测试类
10  * 
11  * @author Hongten
12  * 
13  */
14 public class SpringTest {
15     public static void main(String[] args) {
16         ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
17         // 获取id为animaleServiceOfDog的Bean
18         AnimalService animalServiceOfDog = (AnimalService) act
19                 .getBean("animaleServiceOfDog");
20         animalServiceOfDog.getInfo();
21         // 获取id为animaleServiceOfCat的Bean
22         AnimalService animalServiceOfCat = (AnimalService) act
23                 .getBean("animaleServiceOfCat");
24         animalServiceOfCat.getInfo();
25     }
26 }

/spring_1700_容器后处理器/src/com/b510/app/util/MyBeanFactoryPostProcessor.java

代码语言:javascript
复制
package com.b510.app.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

/**
 * 容器后处理类,实现BeanFactoryPostProcessor接口
 * 
 * @author Hongten
 * 
 */
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    /**
     * 重写该方法,对Spring进行后处理。
     * 
     * @param beanFactory
     *            Spring容器本身
     */
    public void postProcessBeanFactory(
            ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("比较一下上面信息********************");
        System.out.println("Spring容器是:" + beanFactory);
        System.out.println("比较一下下面信息********************");
    }
}

/spring_1700_容器后处理器/src/com/b510/service/AnimalService.java

代码语言:javascript
复制
 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_1700_容器后处理器/src/com/b510/service/MeatService.java

代码语言:javascript
复制
 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_1700_容器后处理器/src/com/b510/service/impl/CatServiceBean.java

代码语言:javascript
复制
 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_1700_容器后处理器/src/com/b510/service/impl/DogServiceBean.java

代码语言:javascript
复制
 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_1700_容器后处理器/src/com/b510/service/impl/FishServiceBean.java

代码语言:javascript
复制
 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_1700_容器后处理器/src/com/b510/service/impl/PorkServiceBean.java

代码语言:javascript
复制
 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_1700_容器后处理器/src/beans.xml

代码语言:javascript
复制
 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     <!-- 容器后处理器 -->
15     <bean id="appBeanPostProcessor" class="com.b510.app.util.MyBeanFactoryPostProcessor"></bean>
16 </beans>

运行结果:

代码语言:javascript
复制
2012-3-12 20:28:20 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9: display name [org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9]; startup date [Mon Mar 12 20:28:20 CST 2012]; root of context hierarchy
2012-3-12 20:28:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2012-3-12 20:28:24 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@15eb0a9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@4a63d8
比较一下上面信息********************
Spring容器是:org.springframework.beans.factory.support.DefaultListableBeanFactory@4a63d8: defining beans [meatServiceOfFish,meatServiceOfPork,animaleServiceOfDog,animaleServiceOfCat,appBeanPostProcessor]; root of factory hierarchy
比较一下下面信息********************
2012-3-12 20:28:24 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4a63d8: defining beans [meatServiceOfFish,meatServiceOfPork,animaleServiceOfDog,animaleServiceOfCat,appBeanPostProcessor]; root of factory hierarchy
鱼肉类被初始化了
猪肉类被初始化了
狗类被初始化了
猫类被初始化了
我是狗,我的年龄是:12,我很喜欢吃猪肉
我是猫,我的年龄是:3,我很喜欢吃鱼肉

我们可以看见:“比较一下上面信息********************”和“比较一下上面信息********************” 的中间内容和系统输出的信息,连个是相同的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-03-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档