BeanFactoryAware and BeanAware : XmlBeanFactory « Spring « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Spring » XmlBeanFactoryScreenshots 
BeanFactoryAware and BeanAware

       
File: context.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="logging" class="LoggingBean"/>

    <bean class="ShutdownHookBean"/>

</beans>


File: Main.java

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {
  public static void main(String[] argsthrows Exception {
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
        "context.xml"));

    factory.preInstantiateSingletons();
    LoggingBean lb = (LoggingBeanfactory.getBean("logging");
    lb.run();
  }

}

class LoggingBean implements BeanNameAware {
  private String name;

  public void setBeanName(String name) {
    this.name = name;
  }

  public void run() {
    System.out.println("Bean name is'" this.name + "'.");
  }
}
class ShutdownHookBean implements BeanFactoryAware, Runnable {
  private ConfigurableListableBeanFactory beanFactory;

  public void setBeanFactory(BeanFactory beanFactorythrows BeansException {
      if (beanFactory instanceof DefaultListableBeanFactory) {
          this.beanFactory = (ConfigurableListableBeanFactory)beanFactory;
          Runtime.getRuntime().addShutdownHook(new Thread(this));
      }
  }

  public void run() {
      if (this.beanFactory != null) {
          System.out.println("Destroying singletons.");
          this.beanFactory.destroySingletons();
      }
  }
}




           
       
Spring-BeanFactoryAware.zip( 2,599 k)
Related examples in the same category
1.Create XmlBeanFactory from ClassPathResource
2.XmlBeanFactory Demo
3.FactoryObject Integration
4.Spring factory method
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.