1. 程式人生 > >十二.Spring AOP面向切面編程

十二.Spring AOP面向切面編程

類型 接口類 技術分享 after app mls schema 動態 方式

什麽是AOP?

AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。

AOP的第一個案例

要求:使用AOP實現日誌記錄功能,核心模塊和增強單獨開發,運行時組裝

1.創建接口HelloDao、HelloService、

創建接口類HelloDaoImpl、HelloServiceImpl並實現dao層接口

public interface IHolleDao {
    public void print();
}  



public class IHolleDaoImpl implements IHolleDao {
    public void print() {
        System.out.println("數據寫入成功");
    }
}



public interface HolleService {
    public void print();
}



public class HolleServiceImpl implements HolleService {

    IHolleDao dao;
    //封裝屬性
    public IHolleDao getDao() {
        return dao;
    }

    public void setDao(IHolleDao dao) {
        this.dao = dao;
    }

    //重寫print方法
    public void print() {
        dao.print();
    }
}

2.創建前後置增強類

前置增強需實現MethodBeforeAdvice接口並實現before方法

後置增強需實現AfterReturningAdvice接口實現afterReturning方法

package cn.happy.day03aop.aop;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/3/5.
 */
//前置增強
public class LoggerBefore implements MethodBeforeAdvice 
{ public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("記錄日誌"); } } ------------------------------------------------------------------ package cn.happy.day03aop.aop; import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; /** * Created by Administrator on 2018/3/5. */ //後置增強 public class LoggerAfter implements AfterReturningAdvice
{ public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("關閉"); } }

3.配置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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="dao" class="cn.happy.day03aop.dao.IHolleDaoImpl">
    </bean>

    <bean id="service" class="cn.happy.day03aop.service.HolleServiceImpl">
        <property name="dao" ref="dao"></property>
    </bean>

    <!--配置aop-->

    <bean id="Before" class="cn.happy.day03aop.aop.LoggerBefore">
    </bean>

    <bean id="After" class="cn.happy.day03aop.aop.LoggerAfter">
    </bean>

    <aop:config>
        <aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"/>
        <!--前置增強-->
        <aop:advisor advice-ref="Before" pointcut-ref="mypoint"/>
        <!--後置增強-->
        <aop:advisor advice-ref="After" pointcut-ref="mypoint"/>
    </aop:config>

</beans>

execution(【modifiers-pattern?】 訪問修飾符
ret-type-pattern 返回值類型
【declaring-type-pattern?】 全限定性類名
name-pattern(param-pattern) 方法名(參數名) 包名.類型名.方法名
【throws-pattern?】) 拋出異常類型

【】內表示可以省略

*代表0或任意多個字符

..代表方法內任意多個參數

4.編寫測試類

@Test
    public void Spring(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-day03aop.xml");
        HolleService service=(HolleService)ctx.getBean("service");
        service.print();
    }  

測試結果

技術分享圖片

分享完畢!

十二.Spring AOP面向切面編程