Spring Bean--Aware接口

Aware接口
這裏寫圖片描述
常見的Aware:

  • ApplicationContextAware:向實現了這個接口的bean提供ApplicationContext(IOC容器的上下文信息),實現了這個接口的bean必須配置到Spring的bean配置文件中去,並且由Spring的bean容器去加載。
  • BeanNameAware:提供一個關於BeanName的定義的內容。
  • ApplicationEventPublisherAware:事件的發佈
  • BeanClassLoaderAware:找到相關的類加載器

這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
例:
spring-aware.xml創建兩個bean:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd" >

    <bean id="moocApplicationContext" class="com.aware.MoocApplicationContext"></bean>

    <bean id="moocBeanName" class="com.aware.MoocBeanName"></bean>  
 </beans>

MoocApplicationContext.java:

package com.aware;

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


public class MoocApplicationContext implements ApplicationContextAware{

    //可以進行聲明
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) //參數applicationContext是加載了這個bean的IOC容器的上下文信息
            throws BeansException {
        this.applicationContext=applicationContext;
        //在applicationContext中獲取bean的實例
        System.out.println("MoocApplicationContext:"+applicationContext.getBean("moocApplicationContext").hashCode()); //判斷是不是同一個bean
    }   
}

MoocBeanName.java:

package com.aware;

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


public class MoocBeanName implements BeanNameAware,ApplicationContextAware { //同時實現BeanNameAware,ApplicationContextAware

    private String beanName;

    @Override
    public void setBeanName(String name) {
        this.beanName=name;
        System.out.println("MoocBeanName:"+name); //name的值就是在配置文件中bean的id
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        //根據bean名稱從applicationContext中得到相應的bean的實例
        System.out.println("setApplicationContext:"+applicationContext.getBean(this.beanName).hashCode());
    }



}

測試類TestAware:

package com.test.aware;

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

import com.imooc.test.base.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {

    public TestAware() {
        super("classpath:spring-aware.xml");
    }

    @Test
    public void testMoocApplicationContext(){
        //從IOC容器中得到bean的實例
        System.out.println("testMoocApplicationContext:"+super.getBean("moocApplicationContext").hashCode());
    }

    @Test
    public void testMoocBeanName(){
        System.out.println("testMoocBeanName:"+super.getBean("moocBeanName").hashCode()); //通過id來得到bean的實例
    }

}

運行測試方法testMoocApplicationContext:
這裏寫圖片描述
運行testMoocBeanName:
這裏寫圖片描述

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