zoukankan      html  css  js  c++  java
  • Example of BeanFactoryAware in Spring--转

    原文地址:http://www.concretepage.com/spring/example_beanfactoryaware_spring

    If a bean in spring implements BeanFactoryAware then that bean has to implement a method that issetBeanFactory. And when that bean is loaded in spring container, setBeanFactory is called. BeanFactoryAware belongs to the package org.springframework.beans.factory. BeanFactoryAware awares the bean for its BeanFactory. 

    A.java

    package com.concretepage;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.BeanFactoryAware;
    public class A  implements BeanFactoryAware{
    
    	@Override
    	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    	    System.out.println("setBeanFactory:"+beanFactory);
    	}
    	public A(){
    		System.out.println("Bean A is Initialized.");
    	}
    }


    app-conf.xml

    <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-3.0.xsd ">
      
        <bean id="testA" class="com.concretepage.A"/>
          
    </beans>



    SpringTest.java

    package com.concretepage;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class SpringTest {
    	public static void main(String[] args) {
    		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-conf.xml");		
    		context.registerShutdownHook();
    	}
    }



    Output

    Bean A is Initialized.
    setBeanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@16df1832: defining beans [testA]; root of factory hierarchy
     
  • 相关阅读:
    正则表达式学习笔记(3)字符类
    一个Service/Functional/Persistence的简单框架
    在Visual Studio 2008环境下安装Boost
    你需要掌握的三种编程语言
    abstract、virtual、override 和 new
    WINCE下编译STLPort
    VS2008环境下编译使用SGI STL(using stlport 5.2.1)
    QT For WinCE, Visual Studio 2008环境的搭建
    VC6.0、VS2005、VS2008安装STLport5.2.1
    Ubuntu 11.10 安装nginx+php5+mysql 的web服务器
  • 原文地址:https://www.cnblogs.com/davidwang456/p/5555374.html
Copyright © 2011-2022 走看看