Spring(六)AOP切入方式

原文出自:http://www.cnblogs.com/liunanjava/p/4411234.htmlhtml

 

1、接口切入方式

實現類java

package com.pb.entity; /** * 實體類 */ public class Hello { private String name; private String password; public void show(){ System.out.println("姓名 :"+this.getName()+"密碼: "+this.getPassword()); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

1.一、前置加強

 

package com.pb.aop; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; /** * 前置加強的Bean * @author Administrator *實現MethodBeforeAdvice接口 */ public class BeforeAdded implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("====前置加強!====="); } }

applicationContext.xmlspring

<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!--hello Bean --> <bean id="hello" class="com.pb.entity.Hello" p:name="張三" p:password="123qwe"/> <!--切入的Bean --> <bean id="beforeAdd" class="com.pb.aop.BeforeAdded"/> <!--前置加強切入的Bean --> <aop:config> <!-- 切入點 --> <aop:pointcut expression="execution(* com.pb.entity.Hello.*(..))" id="myPoint"/> <!-- 關聯切入類和切入點 --> <aop:advisor advice-ref="beforeAdd" pointcut-ref="myPoint"/> </aop:config> </beans>

 

1.二、後置加強

package com.pb.aop; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; /** * 後置加強 */ public class AfterAdded implements AfterReturningAdvice { @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { System.out.println("====這裏是後置加強!===="); } }

 

 applicationContext.xmlexpress

<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!--hello Bean --> <bean id="hello" class="com.pb.entity.Hello" p:name="張三" p:password="123qwe"/> <!--切入的Bean --> <!--後置加強切入的Bean --> <bean id="afterAdd" class="com.pb.aop.AfterAdded"/> <aop:config> <!-- 切入點 --> <aop:pointcut expression="execution(* com.pb.entity.Hello.*(..))" id="myPoint"/> <!-- 關聯後置加強切入類和切入點 --> <aop:advisor advice-ref="afterAdd" pointcut-ref="myPoint"/> </aop:config> <!--後置加強切入的Bean --> </beans>

 

1.三、異常加強

實體類中增長異常app

package com.pb.entity; /** * 實體類 */ public class Hello { private String name; private String password; public void show(){ System.out.println("姓名 :"+this.getName()+"密碼: "+this.getPassword()); //加入異常 System.out.println(1/0); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

 

package com.pb.aop; import java.lang.reflect.Method; import org.springframework.aop.ThrowsAdvice; /** * 異常加強類 * @author Administrator * */ public class ThrowingAdded implements ThrowsAdvice { //第一種寫法 public void afterThrowing(Exception ex){ System.out.println("我是異常加強!,,沒處理異常,有問題就找我"); } //第二種寫法 public void afterThrowing(Method arg0, Object[] arg1, Object arg2,Exception ex){ System.out.println("我是異常加強!,,沒處理異常,有問題就找我"); } }

 

 applicationContext.xmlide

<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!--hello Bean --> <bean id="hello" class="com.pb.entity.Hello" p:name="張三" p:password="123qwe"/> <!--切入的Bean --> <!--異常加強切入的Bean --> <bean id="throwAdd" class="com.pb.aop.ThrowingAdded"/> <aop:config> <!-- 切入點 --> <aop:pointcut expression="execution(* com.pb.entity.Hello.*(..))" id="myPoint"/> <!-- 關聯異常加強切入類和切入點 --> <aop:advisor advice-ref="throwAdd" pointcut-ref="myPoint"/> </aop:config> </beans>

1.四、以上綜合

<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!--hello Bean --> <bean id="hello" class="com.pb.entity.Hello" p:name="張三" p:password="123qwe"/> <!--切入的Bean --> <!--前置加強切入的Bean --> <bean id="beforeAdd" class="com.pb.aop.BeforeAdded"/> <!--後置加強切入的Bean --> <bean id="afterAdd" class="com.pb.aop.AfterAdded"/> <!--異常加強切入的Bean --> <bean id="throwAdd" class="com.pb.aop.ThrowingAdded"/> <aop:config> <!-- 切入點 --> <aop:pointcut expression="execution(* com.pb.entity.Hello.*(..))" id="myPoint"/> <!-- 關聯前置加強切入類和切入點 --> <aop:advisor advice-ref="beforeAdd" pointcut-ref="myPoint"/> <!-- 關聯後置加強切入類和切入點 --> <aop:advisor advice-ref="afterAdd" pointcut-ref="myPoint"/> <!-- 關聯異常加強切入類和切入點 --> <aop:advisor advice-ref="throwAdd" pointcut-ref="myPoint"/> </aop:config> </beans>

 

1.五、環繞加強

 

package com.pb.entity; /** * 實體類 */ public class Hello { private String name; private String password; public void show(){ System.out.println("姓名 :"+this.getName()+"密碼: "+this.getPassword()); //加入異常 System.out.println(1/0); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

環繞增長Beanthis

package com.pb.aop; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 環繞加強 * @author Administrator *實現接口MethodIntercepor */ public class AroundAdded implements MethodInterceptor { @Override public Object invoke(MethodInvocation arg0) throws Throwable { Object result=null; try { System.out.println("環繞加強開始!"); result=arg0.proceed(); System.out.println("環繞加強結束!"); } catch (Exception e) { System.out.println("環繞加強異常!"); }finally{ System.out.println("環繞加強最終加強!"); } return result; } }

 

 applicationContext.xmlspa

 

<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 關聯Bean Hello --> <bean id="hello" class="com.pb.entity.Hello" p:name="張三" p:password="qewr"/> <!--環繞加強的Bean --> <bean id="aroundAdded" class="com.pb.aop.AroundAdded"></bean> <aop:config> <!-- 切入點--> <aop:pointcut expression="execution(* com.pb.entity.*.*(..))" id="myPoint"/> <!--關聯切入點 --> <aop:advisor advice-ref="aroundAdded" pointcut-ref="myPoint"/> </aop:config> </beans>

 

2、註解方式

 

package com.pb.entity; /** * 實體類 */ public class Hello { private String name; private String password; public void show(){ System.out.println("姓名 :"+this.getName()+"密碼: "+this.getPassword()); //加入異常 System.out.println(1/0); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

aop的類代理

package com.pb.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; /** * 註解的方式現實AOP * @author Administrator * */ @Aspect public class MyAnnotionAOP { /* * 前置 */ @Before(value="execution(* com.pb.entity.*.*(..))") public void before(JoinPoint point){ System.out.println("前置加強"); System.out.println(point.getClass()); } /* * 後置 */ @AfterReturning(value="execution(* com.pb.entity.*.*(..))") public void after(JoinPoint point){ System.out.println("後置加強"); //參數個數  System.out.println(point.getArgs().length); } /* *異常 */ @AfterThrowing(value="execution(* com.pb.entity.*.*(..))") public void afterThrowing(JoinPoint point){ System.out.println("我是異常加強"); System.out.println(point.getSignature().getName()); } /** * 環繞 */ @Around(value="execution(* com.pb.entity.*.*(..))") public Object myAround(ProceedingJoinPoint point){ Object result=null; try { System.out.println("環繞加強開始了"); System.out.println(point.getKind()+point.getArgs()); point.proceed(); System.out.println("環繞加強後置加強了"); System.out.println(point.getTarget()+""+point.getClass()); } catch (Throwable e) { System.out.println("環繞加強,異常加強處理"); e.printStackTrace(); }finally{ System.out.println("環繞加強最終加強"); } return result; } }

applicationContext.xmlcode

<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 關聯Bean Hello --> <bean id="hello" class="com.pb.entity.Hello" p:name="張三" p:password="qewr"/> <!-- 切入 類 --> <bean id="myAnnAOP" class="com.pb.aop.MyAnnotionAOP" /> <!--開啓自動代理 --> <aop:aspectj-autoproxy/> </beans>

3、Schema方式(推薦)

 Hello類同上

package com.pb.entity; /** * 實體類 */ public class Hello { private String name; private String password; public void show(){ System.out.println("姓名 :"+this.getName()+"密碼: "+this.getPassword()); //加入異常 System.out.println(1/0); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

aop類

package com.pb.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; /** * SCHEMA方式切入類 * * @author Administrator * */ public class MySchemaAOP { /** * 前置切入 能夠有參數可是固定寫法 */ public void before(JoinPoint point){ System.out.println("這裏是前置加強切入"); System.out.println(point.getKind()+point.getArgs().toString()); } /** * 後置切入 */ public void after(JoinPoint point){ System.out.println("這裏是後置加強切入"); System.out.println(point.getTarget()+point.getSignature().getName()); } /** * 異常切入 */ public void myException(JoinPoint point){ System.out.println("這裏是異常加強切入"); System.out.println(point.getSourceLocation()); } /** * 環繞加強切入 */ public Object myAround(ProceedingJoinPoint point){ Object resut=null; try { System.out.println("環繞加強---前置加強"); resut=point.proceed(); System.out.println("環繞加強---後置加強"); } catch (Throwable e) { System.out.println("環繞加強---異常加強"); e.printStackTrace(); }finally{ System.out.println("環繞加強---最終加強"); } return resut; } }

applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 關聯Bean Hello --> <bean id="hello" class="com.pb.entity.Hello" p:name="張三" p:password="qewr"/> <!-- 切入 類 --> <bean id="mySchemaAOP" class="com.pb.aop.MySchemaAOP"/> <aop:config> <!-- 切入點--> <aop:pointcut expression="execution(* com.pb.entity.*.*(..))" id="myPoint"/> <!--關聯切入類、方法和切入點 --> <aop:aspect ref="mySchemaAOP"> <!-- 切入 前置方法和切入點--> <aop:before method="before" pointcut-ref="myPoint"/> <!-- 切入 後置方法和切入點--> <aop:after method="after" pointcut-ref="myPoint"/> <!-- 切入 異常方法和切入點--> <aop:after method="myException" pointcut-ref="myPoint"/> <!-- 切入 環繞增長方法和切入點--> <aop:around method="myAround" pointcut-ref="myPoint"/> </aop:aspect> </aop:config> </beans>
相關文章
相關標籤/搜索