会调用以下11个方法:
一、创建service、serviceImpl以及processor如下:
public interface UserService {
boolean login(String username, String password);
}
package com.mj.service.impl;
import com.mj.service.UserService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class UserServiceImpl implements UserService, BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {
private Integer age;
public UserServiceImpl() {
System.out.println("01 ---- UserServiceImpl ");
}
public void setAge(Integer age) {
this.age = age;
System.out.println("02 ---- setAge ");
}
/**
* 在初始化完毕之后调用
*/
public void init() {
System.out.println("07 ---- init-method ");
}
public void dealloc() {
System.out.println("11 ---- destroy-method ");
}
public boolean login(String username, String password) {
System.out.println("09 -----login ");
return false;
}
public void setBeanName(String s) {
System.out.println("03 -----BeanNameAware - setBeanName " + s);
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("04 -----ApplicationContextAware - setApplicationContext " + applicationContext);
}
public void destroy() throws Exception {
System.out.println("10 -----DisposableBean - destroy");
}
public void afterPropertiesSet() throws Exception {
System.out.println("06 -----InitializingBean - afterPropertiesSet");
}
}
package com.mj.processor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("05 ---- BeanPostProcessor - before" + beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("08 ---- BeanPostProcessor - after" + beanName);
return bean;
}
}
二、配置文件设置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd">
<bean id="userService"
class="com.mj.service.impl.UserServiceImpl"
p:age="23"
init-method="init"
destroy-method="dealloc"/>
<bean class="com.mj.processor.MyProcessor"/>
</beans>
三、测试类编写
public class UserTest {
@Test
public void test() {
// 创建容器
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = ctx.getBean("userService", UserService.class);
boolean result = service.login("小马哥","123456");
// 关闭容器
ctx.close();
}
}
注:以上效果需要bean的scope是sigleton并且在测试类中手动关闭容器。如果不关闭容器,容器不会销毁,不会打印10和11的步骤。如果scope是prototype则,生命周期不由容器控制,也不会打印10和11步骤。