spring beanPostProcesser
来源:互联网 发布:好用的眼部精华 知乎 编辑:程序博客网 时间:2024/06/06 12:29
The BeanPostProcessor interface defines callback methods that you can implement to provide your own instantiation logic, dependency-resolution logic etc. You can also implement some custom logic after the Spring container finishes instantiating, configuring, and initializing a bean by plugging in one or more BeanPostProcessor implementations.
You can configure multiple BeanPostProcessor interfaces and you can control the order in which these BeanPostProcessor interfaces execute by setting the order property provided the BeanPostProcessor implements the Ordered interface.
The BeanPostProcessors operate on bean (or object) instances which means that the Spring IoC container instantiates a bean instance and then BeanPostProcessor interfaces do their work.
An ApplicationContext automatically detects any beans that are defined with implementation of theBeanPostProcessor interface and registers these beans as post-processors, to be then called appropriately by the container upon bean creation.
Example:
The following examples show how to write, register, and use BeanPostProcessors in the context of an ApplicationContext.
Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:
Here is the content of HelloWorld.java file:
package com.tutorialspoint;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("Bean is going through init."); } public void destroy(){ System.out.println("Bean will destroy now."); }}
This is very basic example of implementing BeanPostProcessor, which prints a bean name before and after initialization of any bean. You can implement more complex logic before and after instantiating a bean because you have access on bean object inside both the post processor methods.
Here is the content of InitHelloWorld.java file:
package com.tutorialspoint;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 any other object as well } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("AfterInitialization : " + beanName); return bean; // you can return any other object as well }}
Following is the content of the MainApp.java file. Here you need to register a shutdown hookregisterShutdownHook() method that is declared on the AbstractApplicationContext class. This will ensures a graceful shutdown and calls the relevant destroy methods.
package com.tutorialspoint;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); context.registerShutdownHook(); }}
Following is the configuration file Beans.xml required for 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.tutorialspoint.HelloWorld" init-method="init" destroy-method="destroy"> <property name="message" value="Hello World!"/> </bean> <bean class="com.tutorialspoint.InitHelloWorld" /></beans>
Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following message:
BeforeInitialization : helloWorldBean is going through init.AfterInitialization : helloWorldYour Message : Hello World!Bean will destroy now.
- spring beanPostProcesser
- Spring源码学习笔记之一:从BeanPostProcesser开始
- Spring
- Spring
- spring
- spring
- spring
- spring
- Spring
- spring
- spring
- Spring
- Spring
- spring
- Spring
- Spring
- spring
- spring
- IOS真机测试教程
- 用python进行图片处理和特征提取
- 视频监控那点事儿(复制别人的)
- Android开发——Intent中的各种FLAG
- [leetcode] Palindrome Number
- spring beanPostProcesser
- PS 颜色混合模式深层计算
- Windows下安装Flask
- {转}Hadoop之使用python实现数据集合间join操作
- 机器学习与智能优化 之 排序与选择特征
- UVA 10271 Chopsticks(dp)
- USACO2.4.2 Overfencing (maze1)
- Hive 研发笔记---LEFT JOIN 中的坑
- Java枚举和注解