关于spring:Spring认证Spring-Bean-后处理器

该BeanPostProcessor的接口定义回调办法,你能够实现提供本人的实例化逻辑,依赖解析逻辑等,还能够实现后的Spring容器实现实例化,配置,并通过在一个或多个梗塞初始化豆一些自定义逻辑BeanPostProcessor 实现。

您能够配置多个 BeanPostProcessor 接口,并且您能够通过设置order属性来管制这些 BeanPostProcessor 接口的执行程序,前提是 BeanPostProcessor 实现了Ordered接口。

BeanPostProcessor 操作 bean(或对象)实例,这意味着 Spring IoC 容器实例化一个 bean 实例,而后 BeanPostProcessor 接口实现它们的工作。

一个ApplicationContext的自动检测与该执行中定义的任何豆的BeanPostProcessor接口,并注册这些豆类如后处理器,被而后通过在容器创立bean的适当调用。

示例
以下示例展现了如何在 ApplicationContext 的上下文中编写、注册和应用 BeanPostProcessors。

让咱们有一个工作的 Eclipse IDE 并采取以下步骤来创立一个 Spring 应用程序 –

这是HelloWorld.java文件的内容-

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.");

}
}
这是实现 BeanPostProcessor 的一个十分根本的示例,它在任何 bean 初始化之前和之后打印一个 bean 名称。您能够在初始化 bean 之前和之后实现更简单的逻辑,因为您能够拜访两个后处理器办法中的 bean 对象。

这是InitHelloWorld.java文件的内容-

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

}
}
以下是MainApp.java文件的内容。这里须要注册一个在 AbstractApplicationContext 类上申明的敞开钩子registerShutdownHook()办法。这将确保失常敞开并调用相干的销毁办法。

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();

}
}
以下是init 和 destroy 办法所需的配置文件Beans.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.or…d”>

<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>
实现创立源文件和 bean 配置文件后,让咱们运行应用程序。如果您的应用程序一切正常,它将打印以下音讯 –

BeforeInitialization : helloWorld
Bean is going through init.
AfterInitialization : helloWorld
Your Message : Hello World!
Bean will destroy now.

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理