Spring4.X後配置AOP
阿新 • • 發佈:2019-02-11
面向切面攔截,主要分為基於註解的攔截與基於方法規則的攔截。
一:新增Spring和AOP相關的依賴
<properties> <java.version>1.7</java.version> <spring-framework.version>4.1.5.RELEASE</spring-framework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- spring aop支援 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- aspectj支援 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.6</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency>
二:編寫攔截規則的註解(配置此註解的地方都會被攔截)
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Action { String name(); }
三:編寫使用註解的被攔截類
import org.springframework.stereotype.Service;
@Service
public class DemoAnnotationService {
@Action(name="註解式攔截的add操作")
public void add(){}
}
四:編寫基於方法規則被攔截的類
import org.springframework.stereotype.Service;
@Service
public class DemoMethodService {
public void add(){}
}
五:編寫切面
import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; @Aspect //宣告一個切面 @Component public class LogAspect { @Pointcut("@annotation(com.flysun.Action)") //宣告切點,為配置Action註解的地方都會攔截 public void annotationPointCut(){}; @After("annotationPointCut()") //使用上面定義的切點 public void after(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Action action = method.getAnnotation(Action.class); System.out.println("註解式攔截 " + action.name()); //通過反射獲得註解上的屬性,然後做日誌記錄相關操作 } @Before("execution(* com.flysun.DemoMethodService.*(..))") //攔截DemoMethodService下的所有方法 public void before(JoinPoint joinPoint){ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); System.out.println("方法規則式攔截,"+method.getName()); } }
六:配置類
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.flysun")
@EnableAspectJAutoProxy //開啟Spring對AspectJ代理的支援
public class AopConfig {
}
七:執行:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
demoAnnotationService.add();
demoMethodService.add();
context.close();
}
}
結果如下: