当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

spring入门实例-Aware

发表于: 2012-07-26   作者:blackproof   来源:转载   浏览:
摘要: spring入门实例-Aware   实例: 配置文件: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=

spring入门实例-Aware

 

实例:

配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	
	<bean id="listener" class="com.myspring.aware.PropertyGettedListener"></bean>

	<bean id="helloBean" class="com.myspring.aware.HelloBean">
		<property name="helloWord">
			<value>oracle</value>
		</property>
	</bean>
</beans>

 bean实现Aware接口:(以applicationAware为例)

public class HelloBean implements ApplicationContextAware
{
	private ApplicationContext applicationContext;

	private String helloWord = "Hello!World";
	
	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException
	{
		// TODO Auto-generated method stub
		this.applicationContext = context;
	}

	public void setHelloWord(String helloWord)
	{
		this.helloWord = helloWord;
	}

	public String getHelloWord()
	{
		applicationContext.publishEvent(new PropertyGettedEvent("{"+helloWord+"} is getted"));
		return helloWord;
	}

}

applicationContext发出事件,在配置文件里的listener就会接受事件

监听者:

public class PropertyGettedListener implements ApplicationListener<ApplicationEvent>
{
	@Override
	public void onApplicationEvent(ApplicationEvent event)
	{
		// TODO Auto-generated method stub
		System.out.println(event.getSource().toString());
	}
}

 

 

spring入门实例-Aware

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号