1. AOP Concepts(核心概念)
2. AOP Indication(标记方式): XML 和 AspetJ-style 两种方式
3. AOP 主要概念:
(1)Aspect: 关注横切多个Class的模块
(2)Join point: 程序执行的点,在spring中只描述method()
(3)Point cut: Indication to match Join point, 匹配横切点的程序描述,通常由Expession(表达式)关联,是AOP的核心概念
(4)Advice: Action token when match the Pointcut.匹配横切点后的执行动作
4. AOP Example:
Target Object:
model:
package com.cisco.springlearn.aopexample.model;
import com.cisco.springlearn.aopexample.aspect.Loggable;
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void throwExceptions() {
throw new RuntimeException("Dummy Exceptions");
}
}
service:
package com.cisco.springlearn.aopexample.service;
import com.cisco.springlearn.aopexample.model.Employee;
public class EmployeeService {
private Employee employee;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
Point cut:
package com.cisco.springlearn.aopexample.aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class EmployeeAspect {
/** 1. Mix the pointcut.expression & Advice
@Before("execution(public String getName())")
public void getNameAdvice() {
System.out.println("Executing the getNameAdvice() method");
}
@Before("execution(* com.cisco.springlearn.aopexample.service.*.*(..))")
public void getAllAdvice() {
System.out.println("Executing the all get*() method");
}
**/
/** 2.Reusing the pointcut to diff Advice**/
@After("getNamePointcut()")
public void afterAdvice(){
System.out.println("After call advice q1");
}
@Pointcut("execution(public String getName())")
public void getNamePointcut() {}
}
Xml config:
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<aop:aspectj-autoproxy/>
<bean name="employee" class="com.cisco.springlearn.aopexample.model.Employee">
<property name="name" value="Dummy Name"/>
</bean>
<bean name="employeeService" class="com.cisco.springlearn.aopexample.service.EmployeeService">
<property name="employee" ref="employee"/>
</bean>
<bean name="employeeAspectArgs" class="com.cisco.springlearn.aopexample.aspect.EmployeeAspectArgs"/>
</beans>
Final static test class:
package com.cisco.springlearn.aopexample;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cisco.springlearn.aopexample.service.EmployeeService;
public class AopExample {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
EmployeeService employeeService = context.getBean(EmployeeService.class);
System.out.println("Test: "+employeeService.getEmployee().getName());
employeeService.getEmployee().setName("hello");
context.close();
}
}
The Execute Log is:
13:24:03.910 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'employeeAspectArgs'
After call advice q1
Test: Dummy Name