springAOP 代理

springAOP  代理
springAOP  代理

较全的springAOP 包括静态代理动态代理及spring代理、spring 动态代理可以在myEclips中建立一个项目然后直接将以下代码拷入即可

一、静态代理

DeptDao:

package com.alibaba.staticproxy;

public class DeptDao {

public void deleteDao(){

System.out.println("delete dept staticly");

}

}

IdeptService:

package com.alibaba.staticproxy;

public interface IDeptService {

public abstract void deleteDept();

}

DeptService:

package com.alibaba.staticproxy;

public class DeptService implements IDeptService{

private DeptDao deptDao=new DeptDao();

public void deleteDept(){

deptDao.deleteDao();

}

}

DeptServiceProxy:

package com.alibaba.staticproxy;

public class DeptServiceProxy {

private IDeptService deptService=new DeptService();

public void deleteDept(){

System.out.println("事物开启");

deptService.deleteDept();

System.out.println("事物结束");

}

}

TestStatic:

package com.alibaba.test;

import com.alibaba.staticproxy.DeptServiceProxy;

public class TestStatic {

public static void main(String[] args) { new DeptServiceProxy().deleteDept();

}

}

二、动态代理

DeptDao:

package com.alibaba.dynamicproxy;

public class DeptDao {

public void deleteDept(){

System.out.println("first delete emps ");

System.out.println("second delete dept");

}

}

IdeptService:

package com.alibaba.dynamicproxy;

public interface IDeptService {

public abstract void deleteDept();

}

DeptService:

package com.alibaba.dynamicproxy;

public class DeptService implements IDeptService {

private DeptDao deptDao=new DeptDao();

public void deleteDept(){

deptDao.deleteDept();

}

}

ProxyDeptService:

package com.alibaba.dynamicproxy;

import https://www.360docs.net/doc/1a4489896.html,ng.reflect.InvocationHandler;

import https://www.360docs.net/doc/1a4489896.html,ng.reflect.Method;

import https://www.360docs.net/doc/1a4489896.html,ng.reflect.Proxy;

public class ProxyDeptService implements InvocationHandler { private IDeptService deptService=new DeptService();

private Object obj;

public Object bind(Object obj){

this.obj=obj;

return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);

}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

System.out.println("事物开启");

Object result=null;

try {

result=method.invoke(obj, args);

} catch (Exception e) {

e.printStackTrace();

}

System.out.println("事物结束");

return result;

}

}

TestDynamic:

package com.alibaba.test;

import com.alibaba.dynamicproxy.DeptService;

import com.alibaba.dynamicproxy.IDeptService;

import com.alibaba.dynamicproxy.ProxyDeptService;

public class TestDynamic {

public static void main(String[] args) {

IDeptService deptService=(IDeptService) new ProxyDeptService().bind(new DeptService());

deptService.deleteDept();

}

}

三.Spring代理(下边的spring.xml为手动代理配置,spring1.xml为自动代理配置)(所需jar包为右击工程名→点击MyEclipse→点击Add Spring Capabilities…→拖动滚动条,选中Aop包、Core包、presistence Core 包→点击finish)

DeptDao:

package com.alibaba.springAOP;

public class DeptDao {

public void deleteDept(){

System.out.println("springAop delete dept");

}

}

IDeptService:

package com.alibaba.springAOP;

public interface IDeptService {

public abstract void deleteDept();

}

DeptService:

package com.alibaba.springAOP;

public class DeptService implements IDeptService {

private DeptDao deptDao=new DeptDao();

public void deleteDept(){

deptDao.deleteDept();

}

}

然后声明通知:

AspectAfter:

package com.alibaba.springAOP;

import https://www.360docs.net/doc/1a4489896.html,ng.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class AspectAfter implements AfterReturningAdvice {

public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {

System.out.println("事物结束");

}

}

AspectAround:

package com.alibaba.springAOP;

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

public class AspectAround implements MethodInterceptor {

public Object invoke(MethodInvocation arg0) throws Throwable {

// TODO Auto-generated method stub

System.out.println("start around");

Object result=arg0.proceed();

System.out.println("end around");

return result;

}

}

AspectBefore:

package com.alibaba.springAOP;

import https://www.360docs.net/doc/1a4489896.html,ng.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class AspectBefore implements MethodBeforeAdvice { public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {

System.out.println("事物开启");

}

}

最后在spring配置文件中配置

spring.xml :

xmlns="https://www.360docs.net/doc/1a4489896.html,/schema/beans"

xmlns:xsi="https://www.360docs.net/doc/1a4489896.html,/2001/XMLSchema-instance"

xmlns:p="https://www.360docs.net/doc/1a4489896.html,/schema/p"

xsi:schemaLocation="https://www.360docs.net/doc/1a4489896.html,/schema/beans https://www.360docs.net/doc/1a4489896.html,/schema/beans/spring-beans-2.5.xsd">

class="com.alibaba.springAOP.AspectBefore">

class="com.alibaba.springAOP.AspectAfter">

class="com.alibaba.springAOP.AspectAround">

class="com.alibaba.springAOP.DeptService">

class="org.springframework.aop.framework.ProxyFactoryBean">

com.alibaba.springAOP.IDeptService

beforeMethod

afterMethod

aroundMethod

Spring1.xml

xmlns="https://www.360docs.net/doc/1a4489896.html,/schema/beans"

xmlns:xsi="https://www.360docs.net/doc/1a4489896.html,/2001/XMLSchema-instance"

xmlns:p="https://www.360docs.net/doc/1a4489896.html,/schema/p"

xsi:schemaLocation="https://www.360docs.net/doc/1a4489896.html,/schema/beans https://www.360docs.net/doc/1a4489896.html,/schema/beans/spring-beans-2.5.xsd">

class="com.alibaba.springAOP.AspectBefore">

class="com.alibaba.springAOP.AspectAfter">

class="com.alibaba.springAOP.AspectAround">

class="com.alibaba.springAOP.DeptService">

class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAuto ProxyCreator">

class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

.*d.*

class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

.*d.*

TestSpringAOP:

package com.alibaba.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.springAOP.IDeptService;

public class TestSpringAOP {

public static void main(String[] args) {

IDeptService deptService=(IDeptService) new ClassPathXmlApplicationContext("spring.xml").getBean("deptServiceProxy");

deptService.deleteDept();

}

}

TestSpringAuto

package com.alibaba.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.springAOP.IDeptService;

public class TestSpringAOPAuto {

public static void main(String[] args) {

IDeptService deptService=(IDeptService) new ClassPathXmlApplicationContext("spring1.xml").getBean("deptService");

deptService.deleteDept();

}

}

四、springAnnotation代理(比较容易)

DeptDao:

package com.alibaba.springAOP;

public class DeptDao {

public void deleteDept(){

System.out.println("springAop delete dept");

}

}

IDeptService:

package com.alibaba.springAOP;

public interface IDeptService {

public abstract void deleteDept();

}

DeptService:

package com.alibaba.springAOP;

public class DeptService implements IDeptService {

private DeptDao deptDao=new DeptDao();

public void deleteDept(){

deptDao.deleteDept();

}

}

AspectAfter:

package com.alibaba.springAOPAnnatation;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.AfterReturning;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.Aspect;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.Before;

@Aspect

public class AspectAfter {

@AfterReturning("execution(* com.alibaba.springAOPAnnatation.DeptService.*(..))") public void deleteDept(){

System.out.println("事物结束");

}

}

AspectAround:

package com.alibaba.springAOPAnnatation;

import https://www.360docs.net/doc/1a4489896.html,ng.ProceedingJoinPoint;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.Around;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.Aspect;

@Aspect

public class AspectAround {

@Around("execution(* com.alibaba.springAOPAnnatation.DeptService.*(..))")

public Object arroundMethod(ProceedingJoinPoint pjp){

System.out.println("方法前环绕");

Object result=null;

try {

result=pjp.proceed();

} catch (Throwable e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("方法后环绕");

return result;

}

}

AspectBefore:

package com.alibaba.springAOPAnnatation;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.Aspect;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.Before;

@Aspect

public class AspectBefore {

@Before("execution(* com.alibaba.springAOPAnnatation.DeptService.*(..) )") public void deleteDept(){

System.out.println("事物开启");

}

}

AspectAll:

package com.alibaba.springAOPAnnatation;

import https://www.360docs.net/doc/1a4489896.html,ng.ProceedingJoinPoint;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.AfterReturning;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.Around;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.Aspect;

import https://www.360docs.net/doc/1a4489896.html,ng.annotation.Before;

@Aspect

public class AspectAll {

@AfterReturning("execution(* com.alibaba.springAOPAnnatation.DeptService.*(..))") public void deleteDept(){

System.out.println("事物结束");

}

@Around("execution(* com.alibaba.springAOPAnnatation.DeptService.*(..))")

public Object arroundMethod(ProceedingJoinPoint pjp){

System.out.println("方法前环绕");

Object result=null;

try {

result=pjp.proceed();

} catch (Throwable e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("方法后环绕");

return result;

}

@Before("execution(* com.alibaba.springAOPAnnatation.DeptService.*(..) )")

public void before(){

System.out.println("事物开启");

}

}

Spring2.xml

xmlns="https://www.360docs.net/doc/1a4489896.html,/schema/beans"

xmlns:aop="https://www.360docs.net/doc/1a4489896.html,/schema/aop"

xmlns:context="https://www.360docs.net/doc/1a4489896.html,/schema/context"

xmlns:xsi="https://www.360docs.net/doc/1a4489896.html,/2001/XMLSchema-instance"

xmlns:p="https://www.360docs.net/doc/1a4489896.html,/schema/p"

xsi:schemaLocation="

https://www.360docs.net/doc/1a4489896.html,/schema/beans

https://www.360docs.net/doc/1a4489896.html,/schema/beans/spring-beans-2.5.xsd https://www.360docs.net/doc/1a4489896.html,/schema/aop

https://www.360docs.net/doc/1a4489896.html,/schema/aop/spring-aop-2.5.xsd

https://www.360docs.net/doc/1a4489896.html,/schema/context

https://www.360docs.net/doc/1a4489896.html,/schema/context/spring-context-2.5. xsd

"

default-autowire="byType"

>

class="com.alibaba.springAOPAnnatation.AspectBefore">

class="com.alibaba.springAOPAnnatation.DeptService"/>

TestSpringAopAnnotation:

package com.alibaba.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.springAOPAnnatation.IDeptService;

public class TestSpringAOPAnnotation {

public static void main(String[] args) {

IDeptService deptService=(IDeptService) new ClassPathXmlApplicationContext("spring2.xml").getBean("deptService");

deptService.deleteDept();

}

}

相关主题
相关文档
最新文档