Spring基礎 匯入AOP+自定義標籤切面
阿新 • • 發佈:2019-02-11
maven
<!--使用AspectJ方式註解需要相應的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> </dependency> <!--使用AspectJ方式註解需要相應的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <!-- Spring Dependency Begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency>
springmvc+標頭檔案
簡單切面<?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:mvc="http://www.springframework.org/schema/mvc" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <description>Spring MVC Configuration</description> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
簡單註解@Aspect @Component public class TaskLoggerAspect { private static Logger logger = LoggerFactory.getLogger(TaskLoggerAspect.class); @Before(value="@annotation(com.minstone.platform.tool.sys.annotation.TaskLoggerAnnotation)") public void beforeTask(JoinPoint joinPoint){ logger.info("標籤執行前"); } @After(value="@annotation(com.minstone.platform.tool.sys.annotation.TaskLoggerAnnotation)") public void afterTask(JoinPoint joinPoint){ logger.info("標籤執行後"); } @AfterThrowing(value="@annotation(com.minstone.platform.tool.sys.annotation.TaskLoggerAnnotation)") public void AfterThrowing(JoinPoint joinPoint){ logger.info("標籤報錯後"); } }
@Target(value={ElementType.METHOD})
public @interface TaskLoggerAnnotation {
}