Sping每次在创建一个bean的时候都会进行初始化 (init),在传统的老的SSH或者SSM的框架下,一般常见的有两种方法来进行初始化和销毁的重写。这两个方法类似于Servlet的方法。
我们必须明确bean的后置处理器其实跟【销毁】的方法可没多大的关系。它的作用域只是限于每一个bean的初始化【前后】进行程序设定。
来看一下简单的demo结构:
Bean1.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.org/schema/beans/spring-beans-3.0.xsd">
<!--【scope】 spring bean的“作用域”设置属性 【singleton】 为default的 在loc 容器中就是单例存在的 -->
<!-- HelloSpring1 -->
<bean id="helloSpring1" class="com.yc1.HelloSpring1" scope="singleton" init-method="init1" destroy-method="destroy1" >
<property name="massage" value="Hello Spring!"/>
</bean>
<bean class="com.yc1.InitHelloSpring1" />
</beans>
HelloSpring.java
package com.yc1;
public class HelloSpring1{
private String massage;
public void init1() {
System.out.println("xml配置的初始化方法...");
}
public void destroy1() {
System.out.println("xml配置的销毁方法...");
}
public void getMassage() {
System.out.println("Your Massage:"+massage);
}
public void setMassage(String massage) {
this.massage = massage;
}
}
InitHelloSpring.java
package com.yc1;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class InitHelloSpring1 implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("bean的后置处理器After执行..."+beanName);
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("bean的后置处理器Before执行..."+beanName);
return bean;
}
}
MainApp.java
package com.yc1;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp1 {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Bean1.xml");
HelloSpring1 helloSpring1 = (HelloSpring1) context.getBean("helloSpring1");
helloSpring1.getMassage();
context.registerShutdownHook();
}
}
执行MainApp.java中的main方法:
八月 26, 2019 1:55:34 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@20ad9418: startup date [Mon Aug 26 13:55:34 CST 2019]; root of context hierarchy
八月 26, 2019 1:55:34 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [Bean1.xml]
bean的后置处理器Before执行...helloSpring1
xml配置的初始化方法...
bean的后置处理器After执行...helloSpring1
Your Massage:Hello Spring!
八月 26, 2019 1:55:35 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@20ad9418: startup date [Mon Aug 26 13:55:34 CST 2019]; root of context hierarchy
xml配置的销毁方法...
可以看到HelloSpring1【初始化方法】前后执行和【后置器】的两个方法。分别是Before方法和After方法。
如果项目有需要可以系统进行设定。
相关的连接:
https://www.w3cschool.cn/wkspring/xs181ici.html