最近在学习Spring AOP,其中涉及到AspectJ的AOP框架。主要参考:http://howtodoinjava.com/spring/spring-aop/spring-aop-aspectj-example-tutorial-using-annotation-config/
在编写例子之前,先熟悉几个spring AOP中专业术语:
1. advice,通知,就是在方法之前或者之后你需要做的事情,比如日志记录,事务之类的等等。
2. pointCut,切入点,主要一系列表达式
3. join points,连接点,是为了切入点而定义出来的,pointCut对应的一系列方法
4. aspect, 切面, advice+pointCut,可以确定什么时候(before, after, around)做事情,在哪个点(pointCut)做
在spring中主要使用代理来包裹切面,把他们织入spring管理的bean中。其中有两个方法:
1. 和目标类实现相同的接口
2. 继承目标类
这两种方式都可以通过jvm的检查,在执行具体的功能时还是由目标类完成,伪装类可以在此之前做一些其他的事情。
接下来是一个AspectJ的小例子。
首先,创建一个切面,使用@Aspect注解
@Aspect public class EmployeeCRUDAspect { @Before("execution(public * howtodoinjava.com.manager.EmployeeManager.*(..))") public void logBeforeV1(JoinPoint joinPoint) { System.out.println("EmployeeCRUDAspect.logBeforeV1() : " + joinPoint.getSignature().getName()); }
然后,对aop进行配置,applicationContext.xml文件的内容是:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- aop命名空间 --> <aop:aspectj-autoproxy /> <context:component-scan base-package="howtodoinjava.com" /> <bean id="loggingAspect" class="howtodoinjava.com.aspect.EmployeeCRUDAspect" /> </beans>
其他辅助类有EmployeeDTO.java和EmployeeManager.java
---EmployeeDTO.java
package howtodoinjava.com.manager;
import java.util.ArrayList;
import java.util.List;
import howtodoinjava.com.dto.EmployeeDTO;
import org.springframework.stereotype.Component;
@Component
public class EmployeeManager
{
public EmployeeDTO getEmployeeById(Integer employeeId) {
System.out.println("Method getEmployeeById() called");
return new EmployeeDTO();
}
public List<EmployeeDTO> getAllEmployee() {
System.out.println("Method getAllEmployee() called");
return new ArrayList<EmployeeDTO>();
}
public void createEmployee(EmployeeDTO employee) {
System.out.println("Method createEmployee() called");
}
public void deleteEmployee(Integer employeeId) {
System.out.println("Method deleteEmployee() called");
}
public void updateEmployee(EmployeeDTO employee) {
System.out.println("Method updateEmployee() called");
}
}
----EmloyeeManager.java
package howtodoinjava.com.manager; import java.util.ArrayList; import java.util.List; import howtodoinjava.com.dto.EmployeeDTO; import org.springframework.stereotype.Component; @Component public class EmployeeManager { public EmployeeDTO getEmployeeById(Integer employeeId) { System.out.println("Method getEmployeeById() called"); return new EmployeeDTO(); } public List<EmployeeDTO> getAllEmployee() { System.out.println("Method getAllEmployee() called"); return new ArrayList<EmployeeDTO>(); } public void createEmployee(EmployeeDTO employee) { System.out.println("Method createEmployee() called"); } public void deleteEmployee(Integer employeeId) { System.out.println("Method deleteEmployee() called"); } public void updateEmployee(EmployeeDTO employee) { System.out.println("Method updateEmployee() called"); } }
构建完成之后,创建测试类TestAOP.java
public class TestAOP { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("howtodoinjava/com/aspect/applicationContext.xml"); EmployeeManager manager = context.getBean(EmployeeManager.class); manager.getEmployeeById(1); manager.createEmployee(new EmployeeDTO()); } }
执行结果如下:
EmployeeCRUDAspect.logBeforeV1() : getEmployeeById
Method getEmployeeById() called
EmployeeCRUDAspect.logBeforeV1() : createEmployee
Method createEmployee() called