Bean的初始化:【注:自动执行】
1.在目标类中定义初始化方法:
public interface UserService {
// 添加用户犯法
public void AddUser();
// 初始化Bean方法
public void Init();
// 销毁Bean方法
public void Destroy();
}
2.在Spring中配置初始化方法:
<?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.xsd">
<bean id="userServiceId" class="com.jishuai.bean_life.UserServiceImpl"
init-method="Init" destroy-method="Destroy"
scope="prototype"></bean>
</beans>
3.在测试类中实例化Bea,执行Bean对象方法:
@Test
public void f() throws Exception {
// spring配置文件路径
String xmlPath = "com/jishuai/bean_life/bean.xml";
// 实例化ApplicationContext对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
// 执行getBean方法 获取Bean对象
UserService userService = applicationContext.getBean("userServiceId",UserService.class);
// 执行Beam对象方法
userService.AddUser();
}
4.查看结果,Init()方法被执行
Bean的销毁:【注:销毁方法执行的前提是,spring容器必须关闭】
1.在目标类中定义销毁方法:
public interface UserService {
// 添加用户犯法
public void AddUser();
// 初始化Bean方法
public void Init();
// 销毁Bean方法
public void Destroy();
}
2. spring中配置销毁方法:
<?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.xsd">
<bean id="userServiceId" class="com.jishuai.bean_life.UserServiceImpl"
init-method="Init" destroy-method="Destroy"
scope="prototype"></bean>
</beans>
3. 在测试类中实例化Bean对象,执行销毁方法【这里有两种实现方法】
3.1 如果你定义的ApplicationContext对象,销毁方式如下:
@Test
public void f() throws Exception {
// spring配置文件路径
String xmlPath = "com/jishuai/bean_life/bean.xml";
// 实例化ApplicationContext对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
// 执行getBean方法 获取Bean对象
UserService userService = applicationContext.getBean("userServiceId",UserService.class);
// 执行Beam对象方法
userService.AddUser();
// 执行Bean的销毁方法
applicationContext.getClass().getMethod("close").invoke(applicationContext);
}
3.2 如果你定义的是ClassPathXmlApplicationContext对象,销毁方式如下:
@Test
public void f() throws Exception {
// spring配置文件路径
String xmlPath = "com/jishuai/bean_life/bean.xml";
// 实例化ApplicationContext对象
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(xmlPath);
// 执行getBean方法 获取Bean对象
UserService userService = classPathXmlApplicationContext.getBean("userServiceId",UserService.class);
// 执行Beam对象方法
userService.AddUser();
// 执行Bean的销毁方法
classPathXmlApplicationContext.close();
}
两者区别:
因为ApplicationContext是一个接口,在接口中未定义close()方法,所以得通过反射进行销毁,
而ClassPathXmlApplicationContext为ApplicationContext的实现类,在该类中定义了close()销毁方法,所以可以直接调用
注意:
destory方法只能销毁单例模式下的Bean对象,即作用域为singlot的Bean对象。
后处理Bean:【注:执行初始化前后执行的方法】
目录结构:
UserService接口:
public interface UserService {
public void AddUser();
}
UserServiceImp实现类:
public class UserServiceImpl implements UserService {
public void Init() {
System.out.println("Bean对象初始化");
}
public void Destory() {
System.out.println("Bean对象销毁");
}
@Override
public void AddUser() {
System.out.println("员工添加成功");
}
}
后处理bean:【MyProxy 实现接 BeanPostProcessor】:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyProxy implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// 初始化前
System.out.println("初始化方法前");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// 初始化后
System.out.println("初始化方法后");
return bean;
}
}
配置文件:
<?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.xsd">
<bean id="userServiceId" class="com.jishuai.bean_life3.UserServiceImpl"
init-method="Init" destroy-method="Destory"></bean>
<bean class="com.jishuai.bean_life3.MyProxy"></bean>
</beans>
测试类:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void f() throws Exception {
// 配置文件路径
String xmlPath = "com/jishuai/bean_life3/bean.xml";
// 加载spring
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
// 创建对象
UserService userService = applicationContext.getBean("userServiceId",UserService.class);
// 执行方法
userService.AddUser();
// 关闭spring容器 执行销毁方法
applicationContext.getClass().getMethod("close").invoke(applicationContext);
}
}
给Bean对象方法添加事务:
目录结构:
UserService【接口】:
public interface UserService {
// 添加用户方法
public void AddUser();
// 删除用户方法
public void DeleteUser();
}
UserServiceImpl:
public class UserServiceImpl implements UserService {
// bean初始化方法
public void Init() {
System.out.println("初始化执行");
}
// bean销毁
public void Destory() {
System.out.println("销毁完成");
}
@Override
public void AddUser() {
System.out.println("添加用户成功");
}
@Override
public void DeleteUser() {
System.out.println("删除用户成功");
}
}
代理实现:【MyProxy 实现BeanPostProcessor接口】
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyProxy implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化前方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化后方法");
System.out.println("-------------");
return Proxy.newProxyInstance(
MyProxy.class.getClassLoader(),
bean.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = "AddUser";
Object object = null;
if(methodName.equals(method.getName())) {
System.err.println("开启事务");
object = method.invoke(bean, args);
System.err.println("结束事务");
return object;
}else {
object = method.invoke(bean, args);
return object;
}
}
});
}
}
配置文件:
<?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.xsd">
<bean name="userServiceId" class="com.jishuai.bean_life5.UserServiceImpl"
init-method="Init" destroy-method="Destory"></bean>
<bean class="com.jishuai.bean_life5.MyProxy"></bean>
</beans>
测试类:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void f() {
String xmlPath = "com/jishuai/bean_life5/bean.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userService = applicationContext.getBean("userServiceId",UserService.class);
userService.AddUser();
userService.DeleteUser();
}
}
运行结构: