SSM框架之SpringAOP快速搭建

SSM框架之SpringAOP快速搭建

有關(guān)AOP的相關(guān)解析在該文章中以實(shí)例方式闡述:
http://blog.csdn.net/dushiwodecuo/article/details/78180803

1.添加maven依賴

<!--spring-aop-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${spring.version}</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>${spring.version}</version>
</dependency>

2.配置spring-aop.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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--掃描所有注解的組件-->
    <context:component-scan base-package="com.chen"/>
    <!--2、開啟基于注解的aop功能  -->
    <aop:aspectj-autoproxy/>
</beans>

3.編寫切面類

/**
 * @Author: CatalpaFlat
 * @Descrition:
 * @Date: Create in 16:19 2017/11/11
 * @Modified BY:
 */
@Aspect
@Component
public class TestAspect {
    /**service層切面*/
    private final String POINT_CUT = "execution(* com.chen.logic.service..*(..))";
    /**日志輸出*/
    private static final Logger logger = Logger.getLogger(TestAspect.class.getName());


    @Pointcut(POINT_CUT)
    private void pointcut(){}

    @Before(value="pointcut()")
    public void logStart(){
        logger.info("AOP日志,方法開始");
    }
    @After("pointcut()")
    public void logEnd(){
        logger.info("AOP日志,方法最終結(jié)束");
    }

    @AfterThrowing(value="pointcut()")
    public void logException(){
        logger.info("AOP日志,方法出現(xiàn)異常");
    }

    @AfterReturning(value="pointcut()")
    public void logReturn(){
        logger.info("AOP日志,方法正常執(zhí)行");
    }
}

4.測試

注:基于這篇文章
http://blog.csdn.net/DuShiWoDeCuo/article/details/78506579

image

image

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容