Spring-Bean postprocessor

Spring - Bean post-processors

Bean to Bean postprocessor allow additional processing before and after the initialization method is called.

BeanPostProcessor  interface defines callback methods, you can achieve the method to provide its instantiation logic, dependency parsing logic. You can also be achieved by insertion of one or more Spring BeanPostProcessor in the instantiation of the container, after the configure and initialize a bean implement custom logic callback method.

You can configure multiple BeanPostProcessor interface, implemented by setting BeanPostProcessor  Ordered  interface provides the  order attributes to control the execution order of these BeanPostProcessor interface.

BeanPostProcessor may be made to bean (or object) instance operation, which means that a Spring IoC container instantiates a bean instance, and then work BeanPostProcessor interface thereof.

ApplicationContext  automatically detected by  BeanPostProcessor  implementation-defined interface bean, the bean is registered post-processor, and then by creating a bean in the container, call it at the appropriate time.

Example:

Helloworld.java

package com.zk.myspring;

public class Helloworld {
	public String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public void init(){
		System.out.println("init-method");
	}
	
	public void destroy(){
		System.out.println("destroy-method");
	}
}

This is a very simple example to achieve BeanPostProcessor, which enter the name of the bean before any bean initialization and after. You can implement more complex logic in the bean before and after initialization, because you have two methods to access the built-in bean object post-processing program.

InitHelloworld.java

package com.zk.myspring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class InitHelloworld implements BeanPostProcessor{

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessAfterInitialization:"+beanName);
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessBeforeInitialization:"+beanName);
		return bean;
	}

}

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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean name="helloworld" class="com.zk.myspring.Helloworld" init-method="init" destroy-method="destroy">
<property name="message" value="message"></property>
</bean>
<bean class="com.zk.myspring.InitHelloworld" />
</beans>

Here is  MainApp.java  contents of the file. Here, you need to register close hook of a statement in AbstractApplicationContext class  registerShutdownHook ()  method. It will ensure that the normally closed, and calls related to destroy method.

Main.java

package TestMain;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zk.myspring.Helloworld;

public class Main {
	public static void main(String[]args)
	{
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		Helloworld hw=(Helloworld) ac.getBean("helloworld");
		System.out.println(hw.getMessage());
	}
}

 

 

Results are as follows:

 

Guess you like

Origin www.cnblogs.com/longlyseul/p/11333837.html
Recommended