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的哈希值一致,說明兩種方式都可以訪問到容器上下文對象

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章