SpringAOP
区分三个概念
类, 对象, 类对象
User u = new User();
User为一个类
u为一个User类的对象
User.class是User的类对象
AOP01
- IUserService.java
package com.qfedu.aop01;
import java.util.List;
public interface IUserService<T> {
/**
* 获取所有的用户对象列表
* @return
*/
List<T> getAllUser();
/**
* 保存用户
* @param user
* @return
*/
boolean saveUser(T user);
/**
* 根据用户uid删除该uid对应的用户信息
* @param uid
* @return
*/
boolean deleteUser(int uid);
/**
* 更新指定用户信息
* @param obj
* @return
*/
boolean updateUser(T obj);
}
- UserServiceImpl.java
package com.qfedu.aop01;
import java.util.ArrayList;
import java.util.List;
public class UserservcieImpl implements IUserService<Object> {
@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;
}
}
- MyAspect.java
package com.qfedu.aop01;
public class MyAspect {
public void before(){
System.out.println("********before**********");
}
public void after(){
System.out.println("********after**********");
}
}
- UserFactory.java
package com.qfedu.aop01;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class UserFactory {
public static IUserService getUserService(){
IUserService us = new UserservcieImpl();
MyAspect ma = new MyAspect();
/**
*
* 使用Proxy类的静态方法newProxyInstance来实现对于目标对象us的代理
* 目的是在原本us可以做的事情之前和之后可以做额外的事情
*
*/
IUserService ius = (IUserService) Proxy.newProxyInstance(UserFactory.class.getClassLoader(), us.getClass().getInterfaces(), new InvocationHandler() {
/**
* 代理对象调用的回掉方法
* @param proxy 代理对象
* @param method 被代理的方法
* @param args 被代理方法的参数列表对象
* @return 每个方法的最终返回值
* @throws Throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
ma.before();
System.out.println("000000000" + method);
System.out.println("000001111" + args);
Object obj = method.invoke(us, args);
ma.after();
System.out.println("111111111" + obj);
return obj;
}
});
return ius;
}
}
- TestAOP01.java
package com.qfedu.aop01;
import org.junit.Test;
public class TestAOP01 {
@Test
public void testUserService(){
IUserService us = new UserservcieImpl();
System.out.println(us.getAllUser());
System.out.println(us.updateUser(new Object()));
System.out.println(us.deleteUser(1));
System.out.println(us.saveUser(new Object()));
System.out.println("===============");
IUserService us2 = UserFactory.getUserService();
System.out.println(us2.getAllUser());
System.out.println(us2.updateUser(new Object()));
System.out.println(us2.deleteUser(2));
System.out.println(us2.saveUser(new Object()));
}
}
AOP02
- IUserService.java
package com.qfedu.aop02;
import java.util.List;
public interface IUserService<T> {
/**
* 获取所有的用户对象列表
* @return
*/
List<T> getAllUser();
/**
* 保存用户
* @param user
* @return
*/
boolean saveUser(T user);
/**
* 根据用户uid删除该uid对应的用户信息
* @param uid
* @return
*/
boolean deleteUser(int uid);
/**
* 更新指定用户信息
* @param obj
* @return
*/
boolean updateUser(T obj);
}
- UserServiceImpl.java
package com.qfedu.aop02;
import java.util.ArrayList;
import java.util.List;
public class UserServiceImpl<T> implements IUserService<T> {
@Override
public List<T> getAllUser() {
System.out.println("--------getAllUser----------");
return new ArrayList<>();
}
@Override
public boolean saveUser(T user) {
System.out.println("--------saveUser----------");
return true;
}
@Override
public boolean deleteUser(int uid) {
System.out.println("--------deleteUser----------");
return false;
}
@Override
public boolean updateUser(T obj) {
System.out.println("--------updateUser----------");
return true;
}
}
- MyAspect.java
package com.qfedu.aop02;
public class MyAspect {
public void before(){
System.out.println("********before**********");
}
public void after(){
System.out.println("********after**********");
}
}
- UserFactory.java
package com.qfedu.aop02;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class UserFactory {
/**
* 使用Spring中的一个增强类来实现aop方式
* 1. 创建Enhancer对象
* 2. 设置增强类Enhancer的superClass
* 3. 设置Enhancer对象的回调
* 4. 通过eh对象的create()方法来得到指定的对象
* @return
*/
public static IUserService getUserService(){
// 1. 创建Enhancer对象
Enhancer eh = new Enhancer();
// 2. 设置增强类Enhancer的superClass
eh.setSuperclass(IUserService.class);
IUserService<Object> us = new UserServiceImpl<>();
MyAspect ma = new MyAspect();
// 3. 设置Enhancer对象的回调
eh.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
ma.before();
Object obj = method.invoke(us, objects);
ma.after();
return obj;
}
});
// 4. 通过eh对象的create()方法来得到指定的对象
IUserService<Object> ius = (IUserService<Object>) eh.create();
return ius;
}
}
- TestAOP02.java
package com.qfedu.aop02;
import org.junit.Test;
public class TestAOP02 {
@Test
public void testAOP02(){
// IUserService<Object> us = new UserServiceImpl<>();
Object o = new Object();
// System.out.println(us.getAllUser());
// System.out.println(us.saveUser(o));
// System.out.println(us.deleteUser(1));
// System.out.println(us.updateUser(o));
System.out.println("==============");
IUserService ius = UserFactory.getUserService();
System.out.println(ius.getAllUser());
System.out.println(ius.saveUser(o));
System.out.println(ius.deleteUser(1));
System.out.println(ius.updateUser(o));
}
}
AOP03
- 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="us" class="com.qfedu.aop03.UserServiceImpl" />
<bean id="my" class="com.qfedu.aop03.MyAspect" />
<!--
ProxyFactoryBean代理的FactoryBean对象,我们现在要代理的是us
包含四个属性注入:
1. interfaces: 接口对象们
<list>
<value>com.qfedu.aop03.IUserService</value>
<value>com.qfedu.aop03.IUserService</value>
<value>com.qfedu.aop03.IUserService</value>
</list>
2. target:目标对象,哪个对象将被以代理的方式创建
3. interceptorNames:拦截对象的名称,自定义的MethodInterceptor对象,注意它的包结构组成
4. optimize:boolean类型的值:
true:强制使用cglib的动态代理方式
false:使用jdk自带的动态代理
cglib:code generation library,代码生成库,性能更高
-->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="com.qfedu.aop03.IUserService" />
<property name="target" ref="us" />
<property name="interceptorNames" value="my" />
<property name="optimize" value="true" />
</bean>
</beans>
- IUserService.java
package com.qfedu.aop03;
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);
}
- UserServiceImpl.java
package com.qfedu.aop03;
import org.springframework.context.ApplicationContext;
import java.util.ArrayList;
import java.util.List;
public class UserServiceImpl implements IUserService {
ApplicationContext ac;
@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;
}
}
- MyAspect.java
package com.qfedu.aop03;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAspect implements MethodInterceptor {
private void before(){
System.out.println("---------before----------");
}
private void after(){
System.out.println("---------after----------");
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
before();
// 业务处理方法的调用
Object obj = invocation.proceed();
after();
return obj;
}
}
- TestAOP03.java
package com.qfedu.aop03;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAOP03 {
@Test
public void testAOP03(){
ApplicationContext ac = new ClassPathXmlApplicationContext("com/qfedu/aop03/beans.xml");
IUserService us = ac.getBean("proxy", IUserService.class);
Object o = new Object();
System.out.println(us.getAllUser());
System.out.println(us.deleteUser(1));
System.out.println(us.saveUser(o));
System.out.println(us.updateUser(o));
}
}