AOP实现
Spring AOP
与之前一篇一样的接口和实现类
- 接口
package cn.edu.zucc.blog;
/**
* 定义一个用户接口
* @author xyj
*/
public interface UserDao {
public void addUser();
public void removeUser();
}
- 实现类
package cn.edu.zucc.blog;
/**
* userDao接口的实现类
* @author xyj
*/
public class UserDaoImpl implements UserDao{
@Override
public void addUser() {
System.out.println("this is addUser");
}
@Override
public void removeUser() {
System.out.println("this is removeUser");
}
}
3.切面类
package cn.edu.zucc.blog.SpringAspect;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* 切面类
* @author xyj
*/
public class SpringProxy {
/**
* 切面方法 arroud
* 还有befor,after等其他可选方法
*/
public Object myArround(ProceedingJoinPoint joinPoint) throws Throwable {
//前置操作
System.out.println("before method");
//通过反射调用原有方法
Object obj = joinPoint.proceed();
//后置操作
System.out.println("after method");
return obj;
}
}
- aop.xml
关于expression的公式可以参考https://blog.csdn.net/w605283073/article/details/82999927
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--被代理的类-->
<bean id="userdaoimpl" class="cn.edu.zucc.blog.UserDaoImpl"/>
<!--切面类-->
<bean id="springproxy" class="cn.edu.zucc.blog.SpringAspect.SpringProxy"/>
<aop:config>
<!--关联到切面方法-->
<aop:aspect ref="springproxy">
<!--设置切入点-->
<aop:pointcut id="myPointcut" expression="execution(* cn.edu.zucc.blog.UserDao.*(..))"/>
<!--设置切面方法这里的方法名要与切面类中定义的方法名一致-->
<aop:around method="myArround" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
- Test测试类
package cn.edu.zucc.blog.SpringAspect;
import cn.edu.zucc.blog.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//导入xml配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop.xml");
//通过Ioc获取代理对象
UserDao user = (UserDao) applicationContext.getBean("userdaoimpl");
//调用方法
user.addUser();
user.removeUser();
}
}
注解写入
- 接口与实现类不变
- 切面类加入注解
package cn.edu.zucc.blog.SpringAspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
/**
* 切面类
* @author xyj
*/
@Aspect
public class SpringProxy2 {
/**
* 切面方法 arroud
* 还有befor,after等其他可选方法
*/
@Around("execution(* cn.edu.zucc.blog.UserDao.*(..))")
public Object myArround(ProceedingJoinPoint joinPoint) throws Throwable {
//前置操作
System.out.println("before method");
//通过反射调用原有方法
Object obj = joinPoint.proceed();
//后置操作
System.out.println("after method");
return obj;
}
}
- 配置文件删除重复配置
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--被代理的类-->
<bean id="userdaoimpl" class="cn.edu.zucc.blog.UserDaoImpl"/>
<!--切面类-->
<bean id="springproxy2" class="cn.edu.zucc.blog.SpringAspect.SpringProxy2"/>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
- Test测试
package cn.edu.zucc.blog.SpringAspect;
import cn.edu.zucc.blog.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test2 {
public static void main(String[] args) {
//导入xml配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop2.xml");
//通过Ioc获取代理对象
UserDao user = (UserDao) applicationContext.getBean("userdaoimpl");
//调用方法
user.addUser();
user.removeUser();
}
}
还是通过注解最简单啊!在这里只实现了around,其他的before,after等大同小异,可自行了解