上一篇文章中,介绍了java中的代理模式,这篇文章就来了解一下代理模式的扩展也就是Spring中的Aop,Aop是什么呢?惯例我们还是来看一下360百科上给我们的详细介绍(这里应该翻译官方文档的,但是目前能力有限,还不快速翻阅Spring的官方文档)在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。 百科里面讲到的AOP是OOP的延续,我个人并不赞成这句话,了解java的人都知道,OOP就是面向对象编程,是java开发中,非常重要的思想,用过Spring框架的也都了解,当我们想在不修改一个业务逻辑的源代码的前提下想对这个业务进行一下功能上的拓展,我们都会优先想到Spring框架中的AOP技术,AOP是一种技术,是一种解决问题的手段,和思想是不能完全划等号的。下面就来介绍一下在实际开发中,代码中会出现的API以及变量声明,也就是AOP中的一些基本概念
Spring aop中的基本概念
基于注解的AOP案例
模拟一个注册用户的小案例
@Controller
public class UserServlet {
@Autowired
UserService userService = null;
public void addUser() {
userService.addUser();
}
}
package com.jmy.service;
public interface UserService {
public void addUser();
}
package com.jmy.service;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
// TODO Auto-generated method stub
System.out.println("添加联系人成功!");
try {
Thread.sleep();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.jmy.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/*
* 记录业务执行时间
*/
@Component
@Aspect
public class UseTimeAspect {
// 配置环绕通知
@Around("execution(void com.jmy.service.*.*(..))")
public void around(ProceedingJoinPoint pjp) throws Throwable {
long begin = System.currentTimeMillis();
Object object = pjp.proceed();
long end = System.currentTimeMillis();
long useTime = end - begin;
System.out.println("###调用["+pjp.getTarget()+"]的["+pjp.getSignature().getName()+"]方法,用时["+useTime+"]毫秒###");
}
}
package com.jmy.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jmy.web.UserServlet;
public class Test01 {
@Test
public void test01() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServlet userServlet = (UserServlet) context.getBean("userServlet");
userServlet.addUser();
((ClassPathXmlApplicationContext)context).close();
}
}
添加联系人成功!
###调用[com.jmy.service.UserServiceImpl@3b69e7d1]的[addUser]方法,用时[]毫秒###
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置包扫描 -->
<context:component-scan base-package="com.jmy.service"></context:component-scan>
<context:component-scan base-package="com.jmy.web"></context:component-scan>
<context:component-scan base-package="com.jmy.aspect"></context:component-scan>
<!-- 配置注解 -->
<context:annotation-config></context:annotation-config>
<!-- 配置注解aop -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
html
好啦…就这样。