1. 程式人生 > >Spring+SpringMVC+MyBatis入門實踐(5)

Spring+SpringMVC+MyBatis入門實踐(5)

註解方式AOP

註解配置切面

@Aspect 註解表示這是一個切面 @Component 表示這是一個bean,由Spring進行管理 @Around(value = “execution(* com.happycoder.service.ProductService.*(…))”) 表示對com.happycoder.service.ProductService 這個類中的所有方法進行切面操作

package com.happycoder.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.
annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class LoggerAspect { @Around(value = "execution(* com.happycoder.service.ProductService.*(..))") public Object log(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("sytart log:" +joinPoint.getSignature().getName()); Object object = joinPoint.proceed(); System.out.println("end log:"+joinPoint.getSignature().getName()); return object; } }
  1. 修改spring-config.xml

去掉原有資訊,新增如下3行

<context:component-scan base-package
="com.happycoder.aspect"/>
<context:component-scan base-package="com.happycoder.service" /> <aop:aspectj-autoproxy />
  1. 執行測試類

在這裡插入圖片描述