spring AOP四種實現方式,springaop四種實現

來源:互聯網
上載者:User

spring AOP四種實現方式,springaop四種實現
1. 經典的基於代理的AOP 

  1. 建立通知:定義一個介面

 

public interface Sleepable{     void sleep(); }然後寫一個Human類,他實現了這個介面public Human implements Sleepable{      public void sleep(){      System.out.println("睡覺中...!");   }}

2.編寫一個SleepHelper類,它裡麵包含了睡覺的輔助工作,用AOP術語來說它就應該是通知

public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice  {    public void before(Method method, Object[] arguments, Object target)            throws Throwable {        System.out.println("睡覺前");            }    public void afterReturning(Object rturnValue, Method method, Object[] arguments,            Object target) throws Throwable {        System.out.println("睡覺後");            }}

然後在spring設定檔中進行配置:

<!-- 被代理目標對象 -->

<bean id="Human" class="cn.happy.dao.Human"></bean><bean id="SleepHelper" class="cn.happy.aop.SleepHelper"></bean>//定義切點的常用的兩種方式:1)使用Regex 2)使用AspectJ運算式,//這裡用Regex<!-- 顧問 -->     <bean id="BeforAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">              <property name="advice" ref="BeforAdvice"></property>       <property name="pattern" value=".*l.*g.*"></property>       </bean><!-- 代理對象 -->// ProxyFactoryBean是一個代理,我們可以把它轉換為//proxyInterfaces中指定的實現該interface的代理對象      <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">              <property name="target" ref="Human"></property>       <property name="interceptorNames" value="BeforAdvisor"></property>       </bean>

代理類

public class StuTest {    public static void main(String[] args) {        //通過ClassPathXmlApplicationContext執行個體化Spring的上下文        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");        //通過ApplicationContext的getBean()方法,根據id來擷取Bean的執行個體        Sleepable s=(Sleepable)context.getBean("Human");        s.sleep();    }}
3、使用AspectJ的註解

用@Aspect的註解來標識切面

@Aspect//該類為切面public class MyAspect {    @Before(value="execution(public * *(..))") public void mybefore(){              System.out.println("前置增強"); }    //後置增強    @AfterReturning(value="execution(public * *(..))")     public void myafterReturning(){                      System.out.println("後置增強");     }        //異常增強        @AfterThrowing(value="execution(public * *(..))")         public void myafterThrowing(){                              System.out.println("異常增強");         }                //環繞增強        @Around(value="execution(public * *(..))")         public void myAround(ProceedingJoinPoint jp){            System.out.println("環繞前增強");                 try {                    jp.proceed();                } catch (Throwable e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                 System.out.println("環繞後增強");                          }                //最終增強                @After(value="execution(public * *(..))")                 public void myafterLogger(){                                              System.out.println("最終增強");                 }}

Spring設定檔:

<!-- 目標對象 -->   <bean id="Human" class="cn.happy.dao.IserviceImpl"></bean>   <!-- 切面: -->   <bean id="myAspect" class="cn.happy.aop.MyAspect"></bean>     <aop:aspectj-autoproxy/>

測試類別

public class StuTest {    public static void main(String[] args) {        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");        Sleepable s=(Sleepable)context.getBean("Service");        s.sleep();    }}
4、通過<aop:config>來配置(純POJO切面)

<aop:advisor> 定義一個AOP通知者
<aop:after> 後通知
<aop:after-returning> 返回後通知
<aop:after-throwing> 拋出後通知
<aop:around> 周圍通知
<aop:aspect>定義一個切面
<aop:before>前通知
<aop:config>頂級配置元素,類似於<beans>這種東西
<aop:pointcut>定義一個切點

public class MyAspect { public void mybefore(){              System.out.println("前置增強"); } public String myafterReturning(String Returning){              System.out.println("前置增強");        return Returning;}}
public class IserviceImpl implements Iservice{    public void log() {        System.out.println("開啟事務");            }    public String dofirst() {        System.out.println("記錄日誌");        return "";            }    }

Spring的設定檔:

<!-- 目標對象 -->   <bean id="Service" class="cn.happy.dao.IserviceImpl"></bean>   <!-- 切面: -->   <bean id="myAspect" class="cn.happy.aop.MyAspect"></bean>   <!-- 配置切面 -->   <aop:config>   <!-- 配置切入點 -->       <aop:pointcut id="pointcut" expression="execution(public * *..Iservice.log(..))"/>       <!-- 將類方法定義為最終增強並引用pointcut切入點-->       <aop:aspect ref="myAspect">           <aop:after method="myafterReturning" pointcut-ref="pointcut"/>       </aop:aspect>   </aop:config>

測試類別

public class SpringTest {    public static void main(String[] args) {         ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");         Iservice bean = (Iservice)context.getBean("Service");        bean.log();        String count=bean.dofirst();         System.out.println(count);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.