AOP
(面向切面编程)
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
包结构如下:
1,StudentService接口,
package com.cruise.service;
public interface StudentService {
public void addStudent(String name);
}
2,studentServiceImpl实现类,
package com.cruise.service.impl;
import com.cruise.service.StudentService;
public class StudentServiceImpl implements StudentService{
@Override
public void addStudent(String name) {
System.out.println("添加学生:"+name);
}
}
3,写beans.xml,定义studentService的bean,
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentService" class="com.cruise.service.impl.StudentServiceImpl">bean>
beans>
4,写测试代码,运行测试
package com.cruise.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cruise.service.StudentService;
public class T {
private ClassPathXmlApplicationContext CPXAC=null;
@Before
public void setUp() throws Exception {
CPXAC= new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void test1() {
StudentService studentService = (StudentService) CPXAC.getBean("studentService");
studentService.addStudent("张三");
}
}
对以上程序添加日志
1.新建com.cruise.advice包,新建一个类StudentServiceAspect类,导入三个切面的包(见附件),build path , doBefore()方法,获取类名,方法名,参数列表
表结构如下:
StudentServiceAspect 类如下
package com.cruise.advice;
import org.aspectj.lang.JoinPoint;
public class StudentServiceAspect {
public void doBefor(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("参数:"+jp.getArgs()[0]);
System.out.println("开始添加前置通知:……");
}
}
2,在beans.xml中,定义StudentServiceAspect类,代码分析:
xmlns:aop="http://www.springframework.org/schema/aop" 和http://www.springframework.org/schema/aop
,http://www.springframework.org/schema/aop/spring-aop.xsd">这三个需要从网上粘贴,本版本是spring4,
* com.cruise.service.*.*(..)表示切入的位置,第一个 * 表示任意返回值;第二个 * 表示com.cruise.service包下的任意类;第三个 * 表示任意方法;(..)表示任意参数;method="doBefor" 表示切入的具体方法,该方法一定是com.cruise.advice.StudentServiceAspect类中的一种。
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="studentServiceAspect" class="com.cruise.advice.StudentServiceAspect">bean>
<bean id="studentService" class="com.cruise.service.impl.StudentServiceImpl">bean>
<aop:config>
<aop:aspect id="suibiandingyiaop" ref="studentServiceAspect">
<aop:pointcut expression="execution(* com.cruise.service.*.*(..))" id="bussnessService"/>
<aop:before method="doBefor" pointcut-ref="bussnessService"/>
aop:aspect>
aop:config>
beans>
3.写测试代码
package com.cruise.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cruise.service.StudentService;
public class T {
public static void main(String[] args) {
ClassPathXmlApplicationContext CPXAC=new ClassPathXmlApplicationContext("beans.xml");
StudentService studentService = (StudentService) CPXAC.getBean("studentService");
studentService.addStudent("张三");
}
}