AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的周边功能,比如性能统计,日志,事务管理等等
周边功能在Spring的面向切面编程AOP思想里,被定义为切面
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
然后把切面功能和核心业务功能 "编织" 在一起,叫AOP
1. 功能分两大类,辅助功能和核心业务功能
2. 辅助功能和核心业务功能彼此独立进行开发
3. 比如登陆功能,即便是没有性能统计和日志输出,也可以正常运行
4. 如果有需要,就把"日志输出" 功能和 "登陆" 功能 编织在一起,这样登陆的时候,就可以看到日志输出了
5. 辅助功能,又叫做切面,这种能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫做切面编程
1.新建java项目
2.新建lib,导入库,新建实体类Category 和 Product
package com.how2java.pojo; /** * 类别 */ public class Category { public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } private int id; private String name; }
package com.how2java.pojo; /**产品类 * @author lenovo */ public class Product { private int id; private String name; private Category category; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } }
3.创建业务类 ProductService
package com.how2java.service; /** * 业务类 * @author lenovo */ public class ProductService { public void doSomeService(){ System.out.println("doSomeService"); } }
4.测试业务类是否成功?创建测试类:
package com.how2java.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.how2java.service.ProductService; /** * 测试业务类是否成功 * @author lenovo */ public class SpringTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); ProductService s = (ProductService) context.getBean("s"); s.doSomeService(); } }
结果:
doSomeService
表示成功了:
5.创建日志切面: LoggerAspect
package com.how2java.aspect; import org.aspectj.lang.ProceedingJoinPoint; /** * 日志切面的功能是 在调用核心功能之前和之后分别打印日志,切面就是新能统计,日志输出,事务管理 * 来与某个核心功能编织之后,用于执行核心功能的代码 * 核心业务:登录,查询,生成订单 * @author lenovo */ 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; } }
6.配置spring的核心配置文件:applicationConfig.xml
<?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="c" class="com.how2java.pojo.Category"> <property name="name" value="yyy" /> </bean> <bean name="p" class="com.how2java.pojo.Product"> <property name="name" value="product1" /> <property name="category" ref="c" /> </bean> <!-- 声明业务对象--> <bean name="s" class="com.how2java.service.ProductService"> </bean> <!--execution(* com.how2java.service.ProductService.*(..)) 表示对满足如下条件的方法调用,进行切面操作:* 返回任意类型 com.how2java.service.ProductService.* 包名以 com.how2java.service.ProductService 开头的类的任意方法 (..) 参数是任意数量和类型--> <!-- 声明日志切面--> <bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/> <!-- 通过aop:config把业务对象与辅助功能编织在一起。--> <aop:config> <!-- 指定核心业务功能--> <aop:pointcut id="loggerCutpoint" expression= "execution(* com.how2java.service.ProductService.*(..)) "/> <!-- 指定辅助功能--> <aop:aspect id="logAspect" ref="loggerAspect"> <aop:around pointcut-ref="loggerCutpoint" method="log"/> </aop:aspect> </aop:config> </beans>
7.新建测试类:
package com.how2java.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.how2java.service.ProductService; /** * 测试业务类是否成功 * @author lenovo */ public class SpringTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); ProductService s = (ProductService) context.getBean("s"); s.doSomeService(); } }
运行结果:
start log:doSomeService
doSomeService
end log:doSomeService