Java——面向切面編程,Spring中的AOP編程

來源:互聯網
上載者:User

標籤:join   span   實現   代理技術   src   release   代碼實現   image   weave   

面向切面編程

AOP思想:將橫向重複代碼,縱向抽取出來

AOP體現——Filter

AOP體現——攔截器

AOP體現——動態代理

Spring中實現AOP思想

原理:Spring可以為容器中管理的對象產生代理對象

代理分為動態代理和cglib代理:

  • 動態代理(優先)
    • 被代理對象必須要實現介面,才能產生代理對象,如果沒有介面將不能使用動態代理技術,換句話說,就是代理對象和被代理要實現同一介面
  • cglib代理
    • 第三方代理技術,cglib代理,可以對任何類組建代理程式,代理的原理是對目標對象進行繼承代理,如果目標對象被final修飾,那麼該類無法被cglib代理
    • Hibernate的實體使用的是cglib代理

名次解釋

代碼實現:

 

1) 導包

4+2

spring-aop-4.2.4.RELEASE.jar

spring-aspects-4.2.4.RELEASE.jar

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

2)準備介面和實作類別

UserService介面

public interface UserService {void save();void delete();void update();void find();}

UserServiceImpl實作類別

public class UserServiceImpl implements UserService {@Overridepublic void save() {System.out.println("儲存使用者!");//int i = 1/0;}@Overridepublic void delete() {System.out.println("刪除使用者!");}@Overridepublic void update() {System.out.println("更新使用者!");}@Overridepublic void find() {System.out.println("尋找使用者!");}}

3)準備通知(要往裡面織入的代碼)

//通知類public class MyAdvice {//前置通知public void before(){System.out.println("這是前置通知!!");}//後置通知public void afterReturning(){System.out.println("這是後置通知(如果出現異常不會調用)!!");}//環繞通知public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println("這是環繞通知之前的部分!!");Object proceed = pjp.proceed();//調用目標方法System.out.println("這是環繞通知之後的部分!!");return proceed;}//異常通知public void afterException(){System.out.println("出事啦!出現異常了!!");}//後置通知public void after(){System.out.println("這是後置通知(出現異常也會調用)!!");}}

4)配置進行織入,將通知織入目標對象中

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">    <!--1.配置目標對象-->    <bean name="userService" class="cn.x5456.service.impl.UserServiceImpl"></bean>    <!--2.配置通知對象-->    <bean name="myAdvice" class="cn.x5456.advice.MyAdvice"></bean>    <!--3.配置織入點-->    <aop:config>        <!--id任意,設定織入點模式-->        <aop:pointcut id="pc" expression="execution(* cn.x5456.service.impl.*ServiceImpl.*(..))"></aop:pointcut>        <!--配置通知,ref是通知的類名-->        <aop:aspect ref="myAdvice">            <!--method與通知對象中的方法名對應,pointcut為織入點的id-->            <!-- 指定名為before方法作為前置通知 -->            <aop:before method="before" pointcut-ref="pc" />            <!-- 後置 -->            <aop:after-returning method="afterReturning" pointcut-ref="pc" />            <!-- 環繞通知 -->            <aop:around method="around" pointcut-ref="pc" />            <!-- 異常攔截通知 -->            <aop:after-throwing method="afterException" pointcut-ref="pc"/>            <!-- 後置 -->            <aop:after method="after" pointcut-ref="pc"/>        </aop:aspect>    </aop:config></beans>

5)測試類別

public class testDemo {    @Test    public void func(){        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring-config.xml");        UserService userService = (UserService) ac.getBean("userService");        userService.save();    }}

 

Java——面向切面編程,Spring中的AOP編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.