ApplicationContext And BeanFactory PostProcessor : ApplicationContext « Spring « Java Tutorial






File: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

  <bean id="w" class="java.lang.String">
  </bean>

 
  <bean id="allBeansLister" class="AllBeansLister"/>

</beans>

File: Main.java

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");

    ctx.getBean("w");
  }
}

class AllBeansLister implements BeanFactoryPostProcessor {

  public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {

    System.out.println("The factory contains the followig beans:");
    String[] beanNames = factory.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; ++i)
      System.out.println(beanNames[i]);
  }
}
  Download:  Spring-ApplicationContextAndBeanFactoryPostProcessor.zip( 2,894 k)








28.2.ApplicationContext
28.2.1.ApplicationContext component scan
28.2.2.Component Filter Assignable
28.2.3.Search By Base Package
28.2.4.Implements ApplicationListener
28.2.5.Implements ApplicationEvent Publisher Aware
28.2.6.Filter By Annotation
28.2.7.ApplicationContext Aware
28.2.8.ApplicationContext And BeanFactory PostProcessor