aspect oriented programming
是通过预编译方式和运行期动态代理实现程序和功能的统一维护的一种技术。
aop是oop的延续,是函数式编程的一种衍生。利用aop可以对业务逻辑的各个部分进行隔离,从而使得
业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发效率。
aop作用
在程序运行期间不用修改代码对已有方法进行增强
优势:减少使用重复代码、提高开发效率、维护方便
aop的实现方式 使用动态代理
JoinPoint 连接点,指哪些被拦截的点,是方法,或织入时机
PointCut 切入点,指对哪些JoinPoint进行拦截的定义,是被增强的方法
Advice 通知,指拦截到JointPoint之后要做的事情,有前置通知、后置通知、环绕通知
异常通知、最终通知
Introduction
Target 被代理对象(目标对象)
Weaving 加入事物支持的过程
Proxy 代理,一个类被AOP织入增强后,就产生了一个代理类
Aspect 切面,切入点和通知的结合
基于xml的aop
访问修饰符可以省略,
返回值可以使用*,
包名可以使用通配符
…表示当前包及其子包
类名和方法名可以通配
参数列表可以直接写数据类型
全通配写法*…….*(…)
实际开发中的通常写法是业务层实现类下的所有方法·
com.test3.AccountServiceImp.*(..)
package com.test3;
public interface IAccountService {
void saveAccount();
void updateAccount(int i);
int deleteAccount();
}
package com.test3;
public class AccountServiceImp implements IAccountService{
public int deleteAccount() {
// TODO Auto-generated method stub
System.out.println("执行了删除");
return 0;
}
public void saveAccount() {
// TODO Auto-generated method stub
System.out.println("执行了保存");
}
public void updateAccount(int i) {
// TODO Auto-generated method stub
System.out.println("执行了更新");
}
}
package com.test3;
public class Logger {
/**
* 用于打印日志,在切入点方法执行之前执行
*
*/
public void printLog(){
System.out.println("记录日志...");
}
}
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="accountService" class="com.test3.AccountServiceImp">bean>
<bean id="logger" class="com.test3.Logger">bean>
<aop:config>
<aop:aspect id="logAdvice" ref="logger">
<aop:before method="printLog" pointcut="execution(public void com.test3.AccountServiceImp.saveAccount())"/>
aop:aspect>
aop:config>
beans>
package com.test3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService accountService = (IAccountService)ac.getBean("accountService");
accountService.saveAccount();
accountService.updateAccount(1);
accountService.deleteAccount();
}
}
三月 27, 2020 11:42:21 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@12922f6: display name [org.springframework.context.support.ClassPathXmlApplicationContext@12922f6]; startup date [Fri Mar 27 11:42:21 CST 2020]; root of context hierarchy 三月 27, 2020 11:42:21 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [bean.xml] 三月 27, 2020 11:42:21 上午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@12922f6]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1356f42 三月 27, 2020 11:42:21 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1356f42: defining beans [accountService,logger,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0]; root of factory hierarchy
记录日志…
执行了保存
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="accountService" class="com.test3.AccountServiceImp">bean>
<bean id="logger" class="com.test3.Logger">bean>
<aop:config>
<aop:pointcut id="pt2" expression="execution(* com.test3.AccountServiceImp.*(..))"/>
<aop:aspect id="logAdvice" ref="logger">
<aop:before method="printLog" pointcut-ref="pt1"/>
<aop:pointcut id="pt1" expression="execution(* com.test3.AccountServiceImp.*(..))"/>
aop:aspect>
aop:config>
beans>
基于注解的AOP
package com.test3;
public interface IAccountService {
void saveAccount();
void updateAccount(int i);
int deleteAccount();
}
package com.test3;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImp implements IAccountService{
public int deleteAccount() {
// TODO Auto-generated method stub
System.out.println("执行了删除");
return 0;
}
public void saveAccount() {
// TODO Auto-generated method stub
System.out.println("执行了保存");
}
public void updateAccount(int i) {
// TODO Auto-generated method stub
System.out.println("执行了更新");
}
}
package com.test3;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component("logger")
@Aspect //表示当前类是一个切面
public class Logger {
@Pointcut("execution(* com.test3.AccountServiceImp.*(..))")
private void pt1(){}
/**
* 用于打印日志,在切入点方法执行之前执行
*
*/
@Before("pt1()")
public void printLog(){
System.out.println("记录日志...");
}
}
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:component-scan base-package="com.test3">context:component-scan>
<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
beans>
package com.test3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService accountService = (IAccountService)ac.getBean("accountService");
accountService.saveAccount();
accountService.updateAccount(1);
accountService.deleteAccount();
}
}
三月 27, 2020 12:35:47 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@12c9557: display name [org.springframework.context.support.ClassPathXmlApplicationContext@12c9557]; startup date [Fri Mar 27 12:35:47 CST 2020]; root of context hierarchy 三月 27, 2020 12:35:47 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [bean.xml] 三月 27, 2020 12:35:47 下午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@12c9557]: org.springframework.beans.factory.support.DefaultListableBeanFactory@3a3001 三月 27, 2020 12:35:47 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3a3001: defining beans [accountService,logger,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator]; root of factory hierarchy
记录日志…
执行了保存
记录日志…
执行了更新
记录日志…
执行了删除