Beanpostprocessor Publishing processor for the Spring framework parsing Java _java

Source: Internet
Author: User
Tags getmessage

Beanpostprocessor interface definitions can be implemented to provide their own instantiation logic, rely on parsing logic, and so on, or later in the spring container instantiation, Configuring and initializing a Bean implements some custom logical callback method implementations by inserting one or more beanpostprocessor.

You can configure multiple Beanpostprocessor interfaces, control these beanpostprocessor interfaces, and implement the ordered interface by setting the beanpostprocessor provided by the order in which the property is executed.

Beanpostprocessor can manipulate instances of a bean (or object), which means that the spring IOC container instantiates a bean instance and then beanpostprocessor the interface to do its job.

ApplicationContext automatically detects the Beanpostprocessor interface for the defined implementation and registers the bean classes as a back processor, and then invokes any bean at the appropriate time by creating a bean in the container.

Example:
The following example shows how to write, register and use beanpostprocessor can be in a applicationcontext context.

Use the Eclipse IDE, and then follow these steps to create a spring application:

Here are the contents of the Helloworld.java file:

Package Com.yiibai;

public class HelloWorld {
  private String message;

  public void Setmessage (String message) {
   this.message = message;
  }

  public void GetMessage () {
   System.out.println (' Your message: ' + message);
  }

  public void init () {
   System.out.println ("The Bean is going through init.");
  }

  public void Destroy () {
   System.out.println ("Bean would destroy now.");
  }


This is a very simple example of the initialization of any bean that implements Beanpostprocessor, before and after it prints a bean's name. More complex logic can be performed after the internal bean object is accessed and instantiated after a bean is available for two post processor methods.

Here are the contents of the Inithelloworld.java file:

Package Com.yiibai;

Import Org.springframework.beans.factory.config.BeanPostProcessor;
Import org.springframework.beans.BeansException;

public class Inithelloworld implements Beanpostprocessor {public
 
  object Postprocessbeforeinitialization (object Bean,
         String beanname) throws Beansexception {
   System.out.println ("beforeinitialization:" + beanname);
   return bean; You can return the other object

  as OK} public object Postprocessafterinitialization (object bean,
         strin G Beanname) throws Beansexception {
   System.out.println ("afterinitialization:" + beanname);
   return bean; Can return any other object as a



The following is the contents of the Mainapp.java file. Here, the need to register a shutdown Hook Registershutdownhook () is a method declared in the Abstractapplicationcontext class. This will ensure a graceful shutdown and call the associated destroy method.

Package Com.yiibai;

Import Org.springframework.context.support.AbstractApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mainapp {public
  static void Main (string[] args) {

   Abstractapplicationcontext context = 
             new Clas Spathxmlapplicationcontext ("Beans.xml");

   HelloWorld obj = (HelloWorld) context.getbean ("HelloWorld");
   Obj.getmessage ();
   Context.registershutdownhook ();
  }


The following are the configuration file Beans.xml files required by the Init and Destroy methods:

<?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-3.0.xsd ">

  <bean id= "HelloWorld" class= "Com.yiibai.HelloWorld" init-method= "
    Init" destroy-method= "destroy" >
    <property name= "message" value= "Hello world!" />
  </bean>

  <bean class= "Com.yiibai.InitHelloWorld"/>

</beans>

After creating the source code and the bean configuration file, let's run the application. If all goes well, this will print the following information:

Beforeinitialization:helloworld
Bean is going through init.
Afterinitialization:helloworld
Your Message:hello world!
Bean would destroy now.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.