BeanPostProcessor的简单示例
BeanPostPressor主要作用是对类和方法进行增强,下面是个简单的示例,增强增、删、改、查中的增操作
目录结构
beanPostProcessor
- src
- - main
- - - java
- - - - com.zhangxin9727
- - - - - MyBeanPostProcess.java
- - - - - UserDao.java
- - - - - UserDaoImp.java
- - - resources
- - - - applicationContext.xml
- - test
- - - java
- - - - MyTest.java
- pom.xml
主要代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<groupId>com.zhangxin9727</groupId>
<artifactId>beanPosterProcessor</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
</dependency>
</dependencies>
</project>
UserDao.java
package com.zhangxin9727;
public interface UserDao {
void find();
void save();
void update();
void delete();
}
UserDaoImp.java
package com.zhangxin9727;
public class UserDaoImpl implements UserDao {
@Override
public void find() {
System.out.println("查询···");
}
@Override
public void save() {
System.out.println("增加···");
}
@Override
public void update() {
System.out.println("修改···");
}
@Override
public void delete() {
System.out.println("删除···");
}
}
MyBeanPostProcessor.java
package com.zhangxin9727;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if ("userDao".equals(beanName)) {
Object proxy = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("save".equals(method.getName())) {
System.out.println("===增加前权限校验===");
return method.invoke(bean, args);
}
return method.invoke(bean, args);
}
});
return proxy;
}
return null;
}
}
applicationContext.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.xsd">
<bean id="userDao" class="com.zhangxin9727.UserDaoImpl"/>
<bean class="com.zhangxin9727.MyBeanPostProcessor"/>
</beans>
MyTest.java
import com.zhangxin9727.UserDao;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.find();
userDao.save();
userDao.delete();
userDao.update();
}
}