(一)Spring容器相关操作

一、spring事件

spring的事件有如下两个成员。

1、ApplicationEvent,容器事件,由容器发布

2、ApplicationListener 监听器,可以由容器中的任何监听器Bean担任

(1)先顶一个spring的容器事件:

package cn.study.basic;

import org.springframework.context.ApplicationEvent;

public class EmailEvent extends ApplicationEvent {
	private String address;
	private String text;

	public EmailEvent(Object source) {
		super(source);
	}

	public EmailEvent(Object source, String address, String text) {
		super(source);
		this.address = address;
		this.text = text;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

}

(2)编写容器监听器代码:

package cn.study.basic;

import org.springframework.context.ApplicationListener;

public class EmailListener implements ApplicationListener<EmailEvent> {

	@Override
	public void onApplicationEvent(EmailEvent arg0) {
		System.out.println(arg0 instanceof EmailEvent);
		if (arg0 instanceof EmailEvent) {
			EmailEvent ee = (EmailEvent) arg0;
			System.out.println("address:" + ee.getAddress());
		} else {
			System.out.println("container:" + arg0);
		}
	}

}

(3)、bean.xml文件中加入如下配置:

<bean class="cn.study.basic.EmailListener"></bean>

(4)、测试方法

package cn.study.basic.test;

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

import cn.study.basic.EmailEvent;

public class TestAMain {
	@Test
	public void testApp() throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
		EmailEvent emailEvent = new EmailEvent("object", "address", "test");
		context.publishEvent(emailEvent);
	}
}

 运行代码,执行结果如下所示:

true
address:address

 

二、bean获取spring容器

在web开发的过程中,spring容器通常使用声明式方法配置产生,开发者只需要在web.xml中配置相应的Listener,在启动的时候就会初始化Spring容器,但是某些比较特殊的时候,容器中的Bean需要主动访问Spring容器,有如下两种方式。

(1)、实现BeanFactoryAware接口,在实现接口的同时,必须实现如下方法。

public void setBeanFactory(BeanFactory arg0) throws BeansException {

}

(2)、实现ApplicationContextAware接口,同时需要实现如下方法

        @Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {

	}

下面使用第二种方法来实现以下小示例,首先实现接口,代码如下:

package cn.study.basic;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class GetApContext implements ApplicationContextAware {

	private ApplicationContext ctx;

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		this.ctx = arg0;
	}

	public ApplicationContext getContext() {
		return ctx;
	}

}

还需要在bean.xml文件中配置bean,如下

<bean class="cn.study.basic.GetApContext"></bean>

测试方法如下:

package cn.study.basic.test;

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

import cn.study.basic.GetApContext;

public class TestContext {
	@Test
	public void testContext() throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
		GetApContext ctx = (GetApContext) context.getBean("spContext");
		System.out.println(ctx.getContext());
		System.out.println(context==ctx.getContext());
	}
}

测试结果如下所示:

--->org.springframework.context.support.ClassPathXmlApplicationContext@2c11b4c2: startup date [Fri Oct 03 14:04:27 CST 2014]; root of context hierarchy
true

三、几种后处理器

1、bean后处理器BeanPostProcessor,需要实现bean后处理,需要实现接口:BeanPostProcessor ,代码如下

(1)、实现接口BeanPostProcessor 

package cn.study.basic.test6;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("Bean后处理器在初始化之前对【" + beanName + "】进行了增强处理");
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("Bean后处理器在初始化之前对【" + beanName + "】进行了增强处理");
		if (bean instanceof Chinese) {
			Chinese chinese = (Chinese) bean;
			chinese.useAxe();
		}
		return bean;
	}

}

(2)、bean.xml配置文件中:

<bean id="myBeanPostProcessor" class="cn.study.basic.test6.MyBeanPostProcessor"></bean>

(3)、测试方法

package cn.study.basic.test6;

import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;

import cn.study.basic.ContextUtils;
import cn.study.basic.test4.Person;

public class TestMain {
	@Test
	public void testBeanFactory() throws Exception {
		ApplicationContext ctx=ContextUtils.getContext();
		ctx.getBean("dd");
	}
}

  

2、容器后处理BeanFactoryPostProcessor

 (1)、接口BeanFactoryPostProcessor

package cn.study.basic.test6;

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

public class MyFactoryBeanFactoryProcessor implements BeanFactoryPostProcessor {

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("the container of spring :" + beanFactory);
	}

}

(2)测试方法

package cn.study.basic.test6;

import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;

import cn.study.basic.ContextUtils;
import cn.study.basic.test4.Person;

public class TestMain {
	@Test
	public void testMain() throws Exception {
		ClassPathResource isr = new ClassPathResource("bean.xml");
		XmlBeanFactory factory = new XmlBeanFactory(isr);
		MyBeanPostProcessor prr = (MyBeanPostProcessor) factory.getBean("myBeanPostProcessor");
		factory.addBeanPostProcessor(prr);
		Person person = factory.getBean("dd", Person.class);
		person.getName();
	}
}

  

3、属性占位符配置器PropertyPlaceholderConfigurer,是一个容器后处理器,负责读取properties文件中的内容,并将这些数据设置为Spring配置文件的元数据

	<!-- PropertyPlaceholderConfigurer是一个Bean后处理器,它会读取
		属性文件信息,并将这些信息设置成Spring配置文件的元数据。 -->
	<bean class=
		"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>dbconn.properties</value>
			</list>
		</property>
	</bean>
	<!-- 定义数据源Bean,使用C3P0数据源实现 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<!-- 指定连接数据库的驱动 -->
		<property name="driverClass" value="${jdbc.driverClassName}"/>
		<!-- 指定连接数据库的URL -->
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<!-- 指定连接数据库的用户名 -->
		<property name="user" value="${jdbc.username}"/>
		<!-- 指定连接数据库的密码 -->
		<property name="password" value="${jdbc.password}"/>
	</bean>

  

 

posted @ 2014-10-03 14:06  开心学习吧  阅读(1004)  评论(0编辑  收藏  举报