zoukankan      html  css  js  c++  java
  • 学习 Spring (五) Aware 接口

    Spring入门篇 学习笔记

    Spring 中提供了一些以 Aware 结尾的接口,实现了 Aware 接口的 bean 在被初始化之后可以获取相应资源

    • 通过 Aware 接口,可以对 Spring 相应资源进行操作(一定要慎重)
    • 为对 Spring 进行简单的扩展提供了方便的入口

    示例

    ApplicationContextAware

    添加配置文件 spring-aware-applicationcontext.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd" >
            
             <bean id="moocApplicationContext" class="com.karonda.aware.MoocApplicationContext" ></bean>
            
     </beans>
    

    实现接口:

    public class MoocApplicationContext implements ApplicationContextAware  {
    	
    	@Override
    	public void setApplicationContext(ApplicationContext applicationContext)
    			throws BeansException {
    		System.out.println("MoocApplicationContext : " + applicationContext.getBean("moocApplicationContext").hashCode());
    	}
    	
    }
    

    添加测试类:

    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestApplicationContextAware extends UnitTestBase {
    	
    	public TestApplicationContextAware() {
    		super("classpath:spring-aware-applicationcontext.xml");
    	}
    	
    	@Test
    	public void testMoocApplicationContext() {
    		System.out.println("testMoocApplicationContext : " + super.getBean("moocApplicationContext").hashCode());
    	}
    	
    }
    

    MoocBeanName

    添加配置文件 classpath:spring-aware-beanname.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd" >
    
    	<bean id="moocBeanName" class="com.karonda.aware.MoocBeanName" ></bean>
            
     </beans>
    

    实现接口:

    public class MoocBeanName implements BeanNameAware {
    	
    	@Override
    	public void setBeanName(String name) {
    		System.out.println("MoocBeanName : " + name);
    	}
    
    }
    

    添加测试类:

    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestBeanNameAware extends UnitTestBase {
    
    	public TestBeanNameAware() {
    		super("classpath:spring-aware-beanname.xml");
    	}
    	
    	@Test
    	public void textMoocBeanName() {
    		System.out.println("textMoocBeanName : " + super.getBean("moocBeanName"));
    	}
    	
    }
    

    同时实现 ApplicationContextAware 和 MoocBeanName

    修改 MoocBeanName:

    public class MoocBeanName implements BeanNameAware, ApplicationContextAware {
    
    	private String beanName;
    	
    	@Override
    	public void setBeanName(String name) {
    		this.beanName = name;
    		System.out.println("MoocBeanName : " + name);
    	}
    
    	@Override
    	public void setApplicationContext(ApplicationContext applicationContext)
    			throws BeansException {
    		System.out.println("setApplicationContext : " + applicationContext.getBean(this.beanName).hashCode());
    	}
    
    }
    

    修改 TestBeanNameAware:

    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestBeanNameAware extends UnitTestBase {
    
    	public TestBeanNameAware() {
    		super("classpath:spring-aware-beanname.xml");
    	}
    	
    	@Test
    	public void textMoocBeanName() {
    		System.out.println("textMoocBeanName : " + super.getBean("moocBeanName").hashCode());
    	}
    	
    }
    

    源码:learning-spring

  • 相关阅读:
    快速排序和归并排序的迭代实现
    Longest Increasing Subsequence Review
    IOCCC 1987 最佳单行代码解读
    C++类的成员函数对应的链接器符号的解析
    Scalable Global ID Generator Design
    欧拉回路 (Euler Circuit) POJ 1780
    深入理解函数内静态局部变量初始化
    memcached 线程模型
    类的加载与ClassLoader的理解
    字符集常见码表说明
  • 原文地址:https://www.cnblogs.com/victorbu/p/10430307.html
Copyright © 2011-2022 走看看