您的位置:首页 > 编程语言 > Java开发

spring开发_BeanFactoryPostProcessor_容器后处理器

2012-03-12 20:33 766 查看
项目结构:

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

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

package com.b510.app.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.b510.service.AnimalService;

/**
* 测试类
*
* @author Hongten
*
*/
public class SpringTest {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
// 获取id为animaleServiceOfDog的Bean
AnimalService animalServiceOfDog = (AnimalService) act
.getBean("animaleServiceOfDog");
animalServiceOfDog.getInfo();
// 获取id为animaleServiceOfCat的Bean
AnimalService animalServiceOfCat = (AnimalService) act
.getBean("animaleServiceOfCat");
animalServiceOfCat.getInfo();
}
}


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

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

package com.b510.service;

/**
* 动物抽象类
*
* @author Hongten
*
*/
public interface AnimalService {

/**
* 获取相关信息
*/
public abstract void getInfo();

}


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

package com.b510.service;

/**
* 定义抽象类MeatService肉类
*
* @author Hongten
*
*/
public interface MeatService {

/**
* 定义方法whatMeat
*
* @return 返回一个字符串
*/
public abstract String whatMeat();
}


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

package com.b510.service.impl;

import com.b510.service.AnimalService;
import com.b510.service.MeatService;

/**
* 定义猫类实现AnimaleService抽象类
*
* @author Hongten
*
*/
public class CatServiceBean implements AnimalService {
private int age;
private MeatService meatService;

public CatServiceBean(){
System.out.println("猫类被初始化了");
}

public int getAge() {
return age;
}

@Override
public void getInfo() {
System.out.println("我是猫,我的年龄是:"+age+",我很喜欢吃"+meatService.whatMeat());
}
public MeatService getMeatService() {
return meatService;
}

public void setAge(int age) {
this.age = age;
}

public void setMeatService(MeatService meatService) {
this.meatService = meatService;
}

}


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

package com.b510.service.impl;

import com.b510.service.AnimalService;
import com.b510.service.MeatService;

/**
* 定义DogServiceBean类
*
* @author Hongten
*
*/
public class DogServiceBean implements AnimalService {
private int age;
private MeatService meatService;

public DogServiceBean() {
System.out.println("狗类被初始化了");
}

public int getAge() {
return age;
}

@Override
public void getInfo() {
System.out.println("我是狗,我的年龄是:" + age + ",我很喜欢吃"
+ meatService.whatMeat());
}

public MeatService getMeatService() {
return meatService;
}

public void setAge(int age) {
this.age = age;
}

public void setMeatService(MeatService meatService) {
this.meatService = meatService;
}

}


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

package com.b510.service.impl;

import com.b510.service.MeatService;

/**
* 定义鱼肉实现MeatService抽象类
*
* @author Hongten
*
*/
public class FishServiceBean implements MeatService {

public FishServiceBean(){
System.out.println("鱼肉类被初始化了");
}
@Override
public String whatMeat() {
return "鱼肉";
}

}


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

package com.b510.service.impl;

import com.b510.service.MeatService;

/**
* 定义猪肉实现MeatService抽象类
*
* @author Hongten
*
*/
public class PorkServiceBean implements MeatService {

public PorkServiceBean(){
System.out.println("猪肉类被初始化了");
}
@Override
public String whatMeat() {
return "猪肉";
}

}


/spring_1700_容器后处理器/src/beans.xml

<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
<bean id="meatServiceOfFish" class="com.b510.service.impl.FishServiceBean"></bean>
<bean id="meatServiceOfPork" class="com.b510.service.impl.PorkServiceBean"></bean>
<bean id="animaleServiceOfDog" class="com.b510.service.impl.DogServiceBean"
p:age="12" p:meatService-ref="meatServiceOfPork" />
<bean id="animaleServiceOfCat" class="com.b510.service.impl.CatServiceBean"
p:age="3" p:meatService-ref="meatServiceOfFish" />
<!-- 容器后处理器 -->
<bean id="appBeanPostProcessor" class="com.b510.app.util.MyBeanFactoryPostProcessor"></bean>
</beans>


运行结果:

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,我很喜欢吃鱼肉


我们可以看见:“比较一下上面信息********************”和“比较一下上面信息********************”
的中间内容和系统输出的信息,连个是相同的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐