实现方式一:
-使用Proxy类的静态方法newProxyInstance来实现对于目标对象us的代理
-目的是在原本us可以做的事情之前和之后可以做额外的事情
区分三个概念
类, 对象, 类对象
User为一个类
u为一个User类的对象
User.class是User的类对象
AOP01
1.IUserService.java
import java.util.List;
public interface IUserService<T> {
/**
*获取获取所有的用户对象列表
* @return
*/
List<T> getUser();
/**
* 保存用户
* @param user
* @return
*/
boolean saveUser(T user);
/**
* 根基id删除用户
* @param id
* @return
*/
boolean deleteUser(int id);
/**
* 跟新用户信息
* @param obj
* @return
*/
boolean updateUser(T obj);
}
2.UserServiceImpl
import java.util.ArrayList;
import java.util.List;
public class UserServiceImpl implements IUserService {
@Override
public List getUser() {
System.out.println("-------getUser()------");
return new ArrayList();
}
@Override
public boolean saveUser(Object user) {
System.out.println("-------saveUser()------");
return true;
}
@Override
public boolean deleteUser(int id) {
System.out.println("------- deleteUser()------");
return false;
}
@Override
public boolean updateUser(Object obj) {
System.out.println("------- updateUser()------");
return true;
}
}
3.MyAspect.java
package com.jinFang.aop01;
public class MyAspect {
public void before(){
System.out.println("======before()======");
}
public void after(){
System.out.println("======after()======");
}
}
4.UserFactory.java
package com.jinFang.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 UserServiceImpl();
MyAspect ma=new MyAspect();
/**
* 使用Proxy类的静态方法newProxyInstance来实现对于目标对象us的代理
* 目的是在原本us可以做的事情之前和之后可以做额外的事情
*/
IUserService ius= (IUserService)Proxy.newProxyInstance(UserFactory.class.getClassLoader(), us.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
/**
* 代理对象调用的回掉方法
* @param proxy 代理对象
* @param method 被代理的方法
* @param args 被代理方法的参数列表对象
* @return 每个方法的最终返回值
* @throws Throwable
*/
ma.before();
System.out.println("00000"+method);
System.out.println("00000"+args);
Object o = method.invoke(us, args);
ma.after();
System.out.println("11111"+o);
return o;
}
});
return ius;
}
}
5.TestAOP01.java
package com.jinFang.aop01;
import org.junit.Test;
public class TestAOP01 {
@Test
public void testUserService(){
IUserService us=new UserServiceImpl();
System.out.println(us.getUser());
System.out.println(us.saveUser(new Object()));
System.out.println(us.deleteUser(1));
System.out.println(us.updateUser(new Object()));
System.out.println("++++++++++++++++++++++++++++");
IUserService us2=UserFactory.getUserService();
System.out.println(us2.getUser());
System.out.println(us2.saveUser(new Object()));
System.out.println(us2.deleteUser(1));
System.out.println(us2.updateUser(new Object()));
}
}
测试结果:
-------getUser()------
[]
-------saveUser()------
true
------- deleteUser()------
false
------- updateUser()------
true
++++++++++++++++++++++++++++
======before()======
00000public abstract java.util.List com.jinFang.aop01.IUserService.getUser()
00000null
-------getUser()------
======after()======
11111[]
[]
======before()======
00000public abstract boolean com.jinFang.aop01.IUserService.saveUser(java.lang.Object)
00000[Ljava.lang.Object;@61e4705b
-------saveUser()------
======after()======
11111true
true
======before()======
00000public abstract boolean com.jinFang.aop01.IUserService.deleteUser(int)
00000[Ljava.lang.Object;@50134894
------- deleteUser()------
======after()======
11111false
false
======before()======
00000public abstract boolean com.jinFang.aop01.IUserService.updateUser(java.lang.Object)
00000[Ljava.lang.Object;@2957fcb0
------- updateUser()------
======after()======
11111true
true
Process finished with exit code 0
实现方式二
使用spring中的增强类来实现AOP方式
AOP02
1.IUserService.java
package com.jinFang.aop02;
import java.util.List;
public interface IUserService<T> {
/**
*获取获取所有的用户对象列表
* @return
*/
List<T> getUser();
/**
* 保存用户
* @param user
* @return
*/
boolean saveUser(T user);
/**
* 根基id删除用户
* @param id
* @return
*/
boolean deleteUser(int id);
/**
* 跟新用户信息
* @param obj
* @return
*/
boolean updateUser(T obj);
}
2.UserServiceImpl.java
package com.jinFang.aop02;
import java.util.ArrayList;
import java.util.List;
public class UserServiceImpl implements IUserService {
@Override
public List getUser() {
System.out.println("-------getUser()------");
return new ArrayList();
}
@Override
public boolean saveUser(Object user) {
System.out.println("-------saveUser()------");
return true;
}
@Override
public boolean deleteUser(int id) {
System.out.println("------- deleteUser()------");
return false;
}
@Override
public boolean updateUser(Object obj) {
System.out.println("------- updateUser()------");
return true;
}
}
3.MyAspect.java
public class MyAspect {
public void before(){
System.out.println("======before()======");
}
public void after(){
System.out.println("======after()======");
}
}
4.UserFactory.java
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 {
public static IUserService getUserService(){
/**
* 使用spring中的增强类来实现AOP方式
* 1.创建 Enhancer 对象
* 2.设置增强类的 Enhancer 的Superclass
* 3.设置Enhancer 对象的回调
* 4.通过Enhancer对象的create()方法来获取指定对象
*/
//1.创建 Enhancer 对象
Enhancer eh=new Enhancer();
// 2.设置增强类的 Enhancer 的Superclass
eh.setSuperclass(IUserService.class);
IUserService <Object> us= (IUserService<Object>) 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.通过Enhancer对象的create()方法来获取指定对象
IUserService <Object> ius= (IUserService<Object>) eh.create();
return ius;
}
}
5.TestAOP02.java
import org.junit.Test;
public class TestAOP02 {
@Test
public void testUserService(){
Object o=new Object();
IUserService ius=UserFactory.getUserService();
System.out.println(ius.getUser());
System.out.println(ius.deleteUser(1));
System.out.println(ius.saveUser(o));
System.out.println(ius.updateUser(o));
}
}
测试结果:
======before()======
-------getUser()------
======after()======
[]
======before()======
------- deleteUser()------
======after()======
false
======before()======
-------saveUser()------
======after()======
true
======before()======
------- updateUser()------
======after()======
true
Process finished with exit code 0
实现方式三
ProxyFactoryBean代理的FactoryBean对象
AOP03
1.beans.xml(注意idea版本不同放置的位置不一样 2019版本的需要放在resource下)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="us" class="com.jinFang.aop03.UserServiceImpl" />
<bean id="ma" class="com.jinFang.aop03.MyAspect"/>
<!--
ProxyFactoryBean代理的FactoryBean对象,我们现在要代理的是us
包含四个属性注入:
1. interfaces: 接口对象们
<property name="interfaces" value="com.jinFang.aop03.IUserService"/>
2. target:目标对象,哪个对象将被以代理的方式创建
<property name="target" ref="us" />
3. interceptorNames:拦截对象的名称,自定义的MethodInterceptor对象,注意它的包结构组成
<property name="interceptorNames" value="ma" />
4. optimize:boolean类型的值:
true:强制使用cglib的动态代理方式
false:使用jdk自带的动态代理
cglib:code generation library,代码生成库,性能更高
<property name="optimize" value="true"/>
-->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="interfaces" value="com.jinFang.aop03.IUserService"/>
<property name="target" ref="us" />
<property name="interceptorNames" value="ma" />
<property name="optimize" value="true"/>
</bean>
</beans>
2.IUserService.java
import java.util.List;
public interface IUserService {
List<Object> getUser();
/**
* 保存用户
* @param user
* @return
*/
boolean saveUser(Object user);
/**
* 根基id删除用户
* @param id
* @return
*/
boolean deleteUser(int id);
/**
* 跟新用户信息
* @param obj
* @return
*/
boolean updateUser(Object obj);
}
3.UserServiceImpl.java
import org.springframework.context.ApplicationContext;
import java.applet.AppletContext;
import java.util.List;
public class UserServiceImpl implements IUserService {
ApplicationContext as;
@Override
public List<Object> getUser() {
return null;
}
@Override
public boolean saveUser(Object user) {
return false;
}
@Override
public boolean deleteUser(int id) {
return false;
}
@Override
public boolean updateUser(Object obj) {
return false;
}
}
4.MyAspect.java
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 methodInvocation) throws Throwable {
before();
//业务处理方法的调用
Object obj = methodInvocation.proceed();
after();
return obj;
}
}
5.TestAOP03.java
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAOP03 {
@Test
public void testUserService(){
ApplicationContext as=new ClassPathXmlApplicationContext("beans.xml");
IUserService us = as.getBean("proxy", IUserService.class);
Object o = new Object();
System.out.println(us.getUser());
System.out.println(us.deleteUser(1));
System.out.println(us.saveUser(o));
System.out.println(us.updateUser(o));
}
}
测试结果:
二月 27, 2020 12:05:09 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2f410acf: startup date [Thu Feb 27 00:05:09 CST 2020]; root of context hierarchy
二月 27, 2020 12:05:09 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
---------before()---------
---------after()---------
null
---------before()---------
---------after()---------
false
---------before()---------
---------after()---------
false
---------before()---------
---------after()---------
false
Process finished with exit code 0