目录
AOP的导入
1.创建service类(切面类)
2.创建目标类
3.创建InvocationHandler接口的实现类,增强功能
4.创建代理对象类完成功能的增强
AOP的导入
chapter04-aop-leadin:面向切面编程导入
动态代理:可以在程序执行的过程中,创建代理对象。
通过代理对象执行方法,给目标类的方法增加功能(功能增强)
JDK动态代理实现步骤:
1.创建目标类,SomeServiceImpI目标类,给它的doSome,doOther增加输出时间,事务。
2.创建InvocationHandler接口的实现类,在这个类实现给目标方法增强功能。
3.使用jdk中类 Proxy,创建代理对象。实现创建对象的能力。
1.创建service类(切面类)
package edu.tjdz.util;
import java.util.Date;
public class ServiceTools {
public static void doLog(){
System.out.println("非业务方法,方法执行时间:"+new Date());
}
public static void doTrans(){
//方法的最后提交一个事务
System.out.println("方法执行完毕后,提交事务");
}
}
2.创建目标类
package edu.tjdz.service;
public interface SomeService {
void doSome();
void doOther();
}
package edu.tjdz.service.impl;
import edu.tjdz.service.SomeService;
//service类的代码不修改,也能增加 输出时间,事务。
public class SomeServiceImpl implements SomeService {
@Override
public void doSome() {
System.out.println("执行业务方法doSome");
}
@Override
public void doOther() {
System.out.println("执行业务方法doOther");
}
}
package edu.tjdz.service.impl;
import edu.tjdz.service.SomeService;
import edu.tjdz.util.ServiceTools;
public class SomeServiceImpl1 implements SomeService {
@Override
public void doSome() {
ServiceTools.doLog();
System.out.println("执行业务方法doSome");
//方法的最后提交一个事务
System.out.println("方法执行完毕后,提交事务");
}
@Override
public void doOther() {
ServiceTools.doLog();
System.out.println("执行业务方法doOther");
//方法的最后提交一个事务
System.out.println("方法执行完毕后,提交事务");
}
}
package edu.tjdz.service.impl;
import edu.tjdz.service.SomeService;
import edu.tjdz.util.ServiceTools;
import java.util.Date;
public class SomeServiceImpl2 implements SomeService {
@Override
public void doSome() {
System.out.println("非业务方法,方法执行时间:"+new Date());
System.out.println("执行业务方法doSome");
ServiceTools.doTrans();
}
@Override
public void doOther() {
System.out.println("非业务方法,方法执行时间:"+new Date());
System.out.println("执行业务方法doOther");
ServiceTools.doTrans();
}
}
3.创建InvocationHandler接口的实现类,增强功能
package edu.tjdz.handler;
import edu.tjdz.util.ServiceTools;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyInvocationHandler implements InvocationHandler {
//目标对象
private Object target;//SomeServiceImpI类
public MyInvocationHandler(Object target){
this.target=target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//通过代理对象执行方法时,会执行这个invke()
System.out.println("执行MyInvocationHandler中的invoke方法");
System.out.println("method名称:"+method.getName());
String methodName = method.getName();
Object res = null;
if("doSome".equals(methodName)){ //JoinPoint Pointcut
ServiceTools.doLog();//在目标方法之前,输出时间
//执行目标类的方法,通过Method类实现
res = method.invoke(target,args); //SomeServiceImpI.doSome()
ServiceTools.doTrans();//在目标方法执行之后,提交事务
}else{
res = method.invoke(target,args); //SomeServiceImpI.doOther()
}
//目标方法的执行结果
return res;
}
}
4.创建代理对象类完成功能的增强
package edu.tjdz;
import edu.tjdz.handler.MyInvocationHandler;
import edu.tjdz.service.SomeService;
import edu.tjdz.service.impl.SomeServiceImpl;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class MyApp {
public static void main(String[] args) {
//调用doSome,doOther
//使用jdk的Proxy创建代理对象
//创建目标对象
SomeService target = new SomeServiceImpl();
//创建InvocationHandler对象
InvocationHandler handler = new MyInvocationHandler(target);
//使用Proxy创建代理
SomeService proxy = (SomeService) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),handler);
//proxy:com.sun.proxy.$Proxy0
System.out.println("proxy:"+proxy.getClass().getName());
//通过代理执行方法,会调用handler中的invoke()
proxy.doSome();
System.out.println("-----------------------------");
proxy.doOther();
}
}
chapter04-aop-leadin:面向切面编程导入 动态代理:可以在程序执行的过程中,创建代理对象。 通过代理对象执行方法,给目标类的方法增加功能(功能增强) JDK动态代理实现步骤: 1.创建目标类,SomeServiceImpI目标类,给它的doSome,doOther增加输出时间,事务。 2.创建InvocationHandler接口的实现类,在这个类实现给目标方法增强功能。 3.使用jdk中类 Proxy,创建代理对象。实现创建对象的能力。
package edu.tjdz.util;
import java.util.Date;
public class ServiceTools {
public static void doLog(){
System.out.println("非业务方法,方法执行时间:"+new Date());
}
public static void doTrans(){
//方法的最后提交一个事务
System.out.println("方法执行完毕后,提交事务");
}
}
2.创建目标类
package edu.tjdz.service;
public interface SomeService {
void doSome();
void doOther();
}
package edu.tjdz.service.impl;
import edu.tjdz.service.SomeService;
//service类的代码不修改,也能增加 输出时间,事务。
public class SomeServiceImpl implements SomeService {
@Override
public void doSome() {
System.out.println("执行业务方法doSome");
}
@Override
public void doOther() {
System.out.println("执行业务方法doOther");
}
}
package edu.tjdz.service.impl;
import edu.tjdz.service.SomeService;
import edu.tjdz.util.ServiceTools;
public class SomeServiceImpl1 implements SomeService {
@Override
public void doSome() {
ServiceTools.doLog();
System.out.println("执行业务方法doSome");
//方法的最后提交一个事务
System.out.println("方法执行完毕后,提交事务");
}
@Override
public void doOther() {
ServiceTools.doLog();
System.out.println("执行业务方法doOther");
//方法的最后提交一个事务
System.out.println("方法执行完毕后,提交事务");
}
}
package edu.tjdz.service.impl;
import edu.tjdz.service.SomeService;
import edu.tjdz.util.ServiceTools;
import java.util.Date;
public class SomeServiceImpl2 implements SomeService {
@Override
public void doSome() {
System.out.println("非业务方法,方法执行时间:"+new Date());
System.out.println("执行业务方法doSome");
ServiceTools.doTrans();
}
@Override
public void doOther() {
System.out.println("非业务方法,方法执行时间:"+new Date());
System.out.println("执行业务方法doOther");
ServiceTools.doTrans();
}
}
3.创建InvocationHandler接口的实现类,增强功能
package edu.tjdz.handler;
import edu.tjdz.util.ServiceTools;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyInvocationHandler implements InvocationHandler {
//目标对象
private Object target;//SomeServiceImpI类
public MyInvocationHandler(Object target){
this.target=target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//通过代理对象执行方法时,会执行这个invke()
System.out.println("执行MyInvocationHandler中的invoke方法");
System.out.println("method名称:"+method.getName());
String methodName = method.getName();
Object res = null;
if("doSome".equals(methodName)){ //JoinPoint Pointcut
ServiceTools.doLog();//在目标方法之前,输出时间
//执行目标类的方法,通过Method类实现
res = method.invoke(target,args); //SomeServiceImpI.doSome()
ServiceTools.doTrans();//在目标方法执行之后,提交事务
}else{
res = method.invoke(target,args); //SomeServiceImpI.doOther()
}
//目标方法的执行结果
return res;
}
}
4.创建代理对象类完成功能的增强
package edu.tjdz;
import edu.tjdz.handler.MyInvocationHandler;
import edu.tjdz.service.SomeService;
import edu.tjdz.service.impl.SomeServiceImpl;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class MyApp {
public static void main(String[] args) {
//调用doSome,doOther
//使用jdk的Proxy创建代理对象
//创建目标对象
SomeService target = new SomeServiceImpl();
//创建InvocationHandler对象
InvocationHandler handler = new MyInvocationHandler(target);
//使用Proxy创建代理
SomeService proxy = (SomeService) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),handler);
//proxy:com.sun.proxy.$Proxy0
System.out.println("proxy:"+proxy.getClass().getName());
//通过代理执行方法,会调用handler中的invoke()
proxy.doSome();
System.out.println("-----------------------------");
proxy.doOther();
}
}
package edu.tjdz.handler;
import edu.tjdz.util.ServiceTools;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyInvocationHandler implements InvocationHandler {
//目标对象
private Object target;//SomeServiceImpI类
public MyInvocationHandler(Object target){
this.target=target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//通过代理对象执行方法时,会执行这个invke()
System.out.println("执行MyInvocationHandler中的invoke方法");
System.out.println("method名称:"+method.getName());
String methodName = method.getName();
Object res = null;
if("doSome".equals(methodName)){ //JoinPoint Pointcut
ServiceTools.doLog();//在目标方法之前,输出时间
//执行目标类的方法,通过Method类实现
res = method.invoke(target,args); //SomeServiceImpI.doSome()
ServiceTools.doTrans();//在目标方法执行之后,提交事务
}else{
res = method.invoke(target,args); //SomeServiceImpI.doOther()
}
//目标方法的执行结果
return res;
}
}



