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

  • 相关阅读:
    发送邮件
    C#操作Excel总结
    注意!监控MySQL服务是否正常,懂这4种方法就可以了
    Linux磁盘空间爆满怎么办?定时文件清理脚本配置实现
    Linux 服务器必备的安全设置,建议收藏!
    MySQL入门到精通:MySQL 选择数据库
    TIOBE3月榜单公布!C 语言稳居第一,将新增功能,消除差异
    C++如何读取带空格字符串?这5种方法教会你
    C语言丨二分查找算法详解(含示例代码)
    线上故障了!居然是因为Linux磁盘缓存机制导致的
  • 原文地址:https://www.cnblogs.com/victorbu/p/10430307.html
Copyright © 2011-2022 走看看