Java 如何避免在每个bean之后执行BeanPostProcessor及其在实际用例中的使用
我已经阅读了Spring关于BeanPostProcessor的文档以及与之相关的现有stackoverflow文章。我可以理解BeanPostProcessor有两种方法Java 如何避免在每个bean之后执行BeanPostProcessor及其在实际用例中的使用,java,spring,spring-boot,Java,Spring,Spring Boot,我已经阅读了Spring关于BeanPostProcessor的文档以及与之相关的现有stackoverflow文章。我可以理解BeanPostProcessor有两种方法postProcessAfterInitialization和postProcessBeforInitialization。而postProcessBeforeInitialization在初始化Springbean之后调用,在调用init()方法之前调用,postProcessAfterInitialization在init
postProcessAfterInitialization
和postProcessBeforInitialization
。而postProcessBeforeInitialization在初始化Springbean之后调用,在调用init()方法之前调用,postProcessAfterInitialization在init()之后调用。然而,关于它的行为,我有几个问题
我已经编写了2个POJO类、1个主类、1个自定义BeanPostProcessor类和1个SpringbeanXML文件
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("I am init");
}
public void destroy() {
System.out.println("I am destroy");
}
}
波乔
主类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
((AbstractApplicationContext)context).registerShutdownHook();
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
NewHelloWorld newObj = (NewHelloWorld) context.getBean("newHelloWorld");
}
}
自定义BeanPostProcessor
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class DemoBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
System.out.println(arg1 + ": I am executing immediately after init method");
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException{
System.out.println(arg1 + ": I am executing after spring bean initialization but before init method");
return arg0;
}
}
bean 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:util="http://www.springframework.org/schema/util"
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 id="helloWorld" class="com.HelloWorld" init-method="init" destroy-method="destroy">
<property name="message" value="helloworld-bean" />
</bean>
<bean id="newHelloWorld" class="com.NewHelloWorld">
<property name="message" value="NewHelloworld-bean" />
</bean>
<bean class="com.tutorialspoint.DemoBeanPostProcessor" />
</beans>
问题#1
:-不管我在POJO中编写的init和destroy方法是什么,BeanPostProcessor都是在bean实例化之后执行的。在这种情况下,当定义声明它将在init方法之前和之后执行时,定义如何成立,因为我没有一个POJO的init,但它仍然会执行
问题#2
:-在BeanPostProcessor的方法中,我们有一些逻辑,例如连接到远程数据库、执行一些jms队列等。我们不希望在每个bean之后执行这些逻辑。在这种情况下,我们应该有一些机制,其中BeanPostProcessor不在bean之后运行。如何才能做到这一点。如果无法实现,那么在现实世界的用例中,我们通常在BeanPostProcessor方法中编写什么样的操作?BeanPostProcessor的主要思想是配置您的bean。常见的用例是用代理包装注释了特定注释的bean。
若您有一些逻辑,比如执行jms队列,那个么最好创建单独的bean,并在@PostConstruct方法中执行此逻辑
<?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:util="http://www.springframework.org/schema/util"
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 id="helloWorld" class="com.HelloWorld" init-method="init" destroy-method="destroy">
<property name="message" value="helloworld-bean" />
</bean>
<bean id="newHelloWorld" class="com.NewHelloWorld">
<property name="message" value="NewHelloworld-bean" />
</bean>
<bean class="com.tutorialspoint.DemoBeanPostProcessor" />
</beans>