spring中aware接口(5)

2016/1/16 12:45:20


1.Aware

  • spring中提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化之后,可以获取相应资源
  • 通过Aware接口,可以对Spring相应资源进行操作(一定要慎重)
  • 为对Spring进行简单的扩展提供了方便的入口

XML:spring-aware.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

        <bean id="applicationContext" class="com.zjx.aware.ApplicationContext"></bean>

</beans>

bean类中:

package com.zjx.aware;

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

public class ApplicationContext implements ApplicationContextAware{

    @Override
    public void setApplicationContext(
            org.springframework.context.ApplicationContext arg0)
            throws BeansException {
        System.out.println("ApplicationContext:"+arg0.getBean("applicationContext").hashCode());        
    }

}

测试类:

package com.zjx.interfaces.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
    public TestAware() {
        super("classpath*:spring-aware.xml");
    }

    @Test
    public void test() {
        System.out.println("test方法中的ApplicationContext:"
                + super.getBean("applicationContext").hashCode());
    }
}

结果打印出的两个类的哈希值一致,这说明实现了XXXAware接口,得到了输出

对于BeanName实现BeanNameAware,ApplicationContextAware接口的方式

XML:spring-aware.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

        <bean id="beanName" class="com.zjx.aware.BeanName"></bean>

</beans>

bean类

package com.zjx.aware;

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

public class BeanName implements BeanNameAware,ApplicationContextAware{
    private String beanName;

    @Override
    public void setApplicationContext(ApplicationContext arg0)
            throws BeansException {
        System.out.println("setApplicationContext中的实例哈希值:"+arg0.getBean(this.beanName).hashCode());
    }

    @Override
    public void setBeanName(String arg0) {
        this.beanName = arg0;
        System.out.println("BeanName:"+arg0);   
    }

}

测试

package com.zjx.interfaces.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
    public TestAware() {
        super("classpath*:spring-aware.xml");
    }

    @Test
    public void test() {
        System.out.println("test方法中的实例哈希值:"
                + super.getBean("beanName").hashCode());
    }
}

不管是通过test基类获取的applicationContext还是通过XXXaware接口获取的applicationContext的哈希值一致,说明两种方式都可以访问到容器上下文对象

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值