AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和辅助功能。
所谓的核心业务,好比登录,增长数据,删除数据都叫核心业务
所谓的辅助功能,好比性能统计,日志,事务管理等等
辅助功能在Spring的面向切面编程AOP思想里,即被定义为切面
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
而后把切面功能和核心业务功能 "编织" 在一块儿,这就叫AOPjava
举个例子方便了解aop来作什么:百度搜索aop,其中搜索功能就是核心业务,下面的“百度为您找到相关结果约22,700,000个”就是aop作的事情。spring
一、新建一个java项目express
包的结构以下编程
二、导入jar包,按照结构创建相关文件app
三、ProductService类,输出一句话用来演示业务功能性能
public class ProductService { public void doSomeService(){ System.out.println("doSomeService"); } }
四、准备日志切面LoggerAspect,该日志切面的功能是 在调用核心功能以前和以后分别打印日志测试
import org.aspectj.lang.ProceedingJoinPoint; public class LoggerAspect { public Object log(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("start log:" + joinPoint.getSignature().getName()); Object object = joinPoint.proceed(); System.out.println("end log:" + joinPoint.getSignature().getName()); return object; } }
五、application.xml文件的配置spa
基础通用模板日志
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> </beans>
在其中添加核心功能类,和切面类的声明, class="包名.类名"code
<bean name="p" class="com.yyt.service.ProductService"> </bean> <bean id="loggerAspect" class="com.yyt.aspect.LoggerAspect"/>
指定业务功能(打印一句话)
<aop:pointcut id="loggerCutpoint" expression=
"execution(* com.yyt.service.ProductService.*(..)) "/>
指定辅助功能(在打印话以前以后输出日志)
<aop:aspect id="logAspect" ref="loggerAspect"> <aop:around pointcut-ref="loggerCutpoint" method="log"/> </aop:aspect>
而后经过aop: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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="p" class="com.yyt.service.ProductService"> </bean> <bean id="loggerAspect" class="com.yyt.aspect.LoggerAspect"/> <aop:config> <aop:pointcut id="loggerCutpoint" expression= "execution(* com.yyt.service.ProductService.*(..)) "/> <aop:aspect id="logAspect" ref="loggerAspect"> <aop:around pointcut-ref="loggerCutpoint" method="log"/> </aop:aspect> </aop:config> </beans>
六、测试类,载入xml配置文件,调用业务
package com.yyt.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yyt.service.ProductService; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); ProductService s = (ProductService) context.getBean("p"); s.doSomeService(); } }
七、结果