xml aop:config方式实现AOP
1.IUserService.java
package com.zy.aop04;
import java.util.List;
public interface IUserService {
/**
* 获取所有的用户对象列表
* @return
*/
List<Object> getAllUser();
/**
* 保存用户
* @param user
* @return
*/
boolean saveUser(Object user);
/**
* 根据用户uid删除该uid对应的用户信息
* @param uid
* @return
*/
boolean deleteUser(int uid);
/**
* 更新指定用户信息
* @param obj
* @return
*/
boolean updateUser(Object obj);
}
2.UserServiceImpl.java
package com.zy.aop04;
import java.util.ArrayList;
import java.util.List;
public class UserServiceImpl implements IUserService {
@Override
public List<Object> getAllUser() {
System.out.println("--------getAllUser----------");
return new ArrayList<>();
}
@Override
public boolean saveUser(Object user) {
System.out.println("--------saveUser----------");
return true;
}
@Override
public boolean deleteUser(int uid) {
System.out.println("--------deleteUser----------");
return false;
}
@Override
public boolean updateUser(Object obj) {
System.out.println("--------updateUser----------");
return true;
}
}
3.beans.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"
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="us" class="com.zy.aop04.UserServiceImpl"/>
<bean id="ma" class="com.zy.aop04.MyAspect" />
<!--
proxy-target-class设置为true,强制使用cglib方式来实现动态代理
-->
<aop:config proxy-target-class="true">
<!--
定义一个aop的切点
该包com.qfedu.aop04下所有的任意类,类下的任意含参不含参数的方法,返回值必须为List的方法将会被代理
<aop:pointcut id="pt" expression="execution(java.util.List com.zy.aop04.*.*(..))" />
该包com.qfedu.aop04下所有的任意类,类下的任意含参不含参数的方法,返回值必须为boolean的方法将会被代理
<aop:pointcut id="pt" expression="execution(boolean com.zy.aop04.*.*(..))" />
com.qfedu.aop04.UserServiceImpl.deleteUser(int)具体的方法了,参数为int,返回值任意
<aop:pointcut id="pt" expression="execution(* com.qfedu.aop04.UserServiceImpl.deleteUser(int))" />
多个条件
<aop:pointcut id="pt" expression="(execution(boolean com.zy.aop04.*.*(..)) or (execution(java.util.List com.zy.aop04.*.*(..))))" />
-->
<aop:pointcut id="pt" expression="(execution(boolean com.zy.aop04.*.*(..)) or (execution(java.util.List com.zy.aop04.*.*(..))))" />
<!--
通知,将MyAspect与切点关联起来
-->
<aop:advisor advice-ref="ma" pointcut-ref="pt" />
</aop:config>
</beans>
4.MyAspect.java
package com.zy.aop04;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("-----------before----------");
Object obj = invocation.proceed();
System.out.println("-----------after----------");
return obj;
}
}
5.TestAOP04.java
package com.zy.aop04;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAOP04 {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("com/qfedu/aop04/beans.xml");
IUserService us = ac.getBean("us", IUserService.class);
us.getAllUser();
us.deleteUser(1);
us.updateUser(new Object());
us.saveUser(new Object());
}
}