[Spring-Core] AOP 详解

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值