好程序员Java分享Spring框架之AOP的基本配置

  好程序员Java分享Spring框架之AOP的基本配置,前言咱们在前面学习了动态代理机制,Spring的AOP正是应用此机制实现的,下面咱们来学习如何来配置Spring AOP,实现基本的功能。
  什么是AOP
  AOP(Aspect Oriented Programming)面向切面编程,是OOP(面向对象编程)的重要补充,面向对象是Java编程的基础,以封装、继承、多态创建了对象间的层次关系,关注的是每一个对象的具体实现。面向对象容许咱们开发纵向的关系。
  AOP关注横向关系,也就是说类和类之间不须要特定的关系,它可以将某些通用的操做(如:日志处理、安全验证、事务处理、异常处理、性能统计等)插入到这些无关的类中,从而可让每一个类只关注本身的核心逻辑,没必要处理这些通用的操做,这个过程就是横切,而这些通用的操做就是切面Aspect。
  简单来讲:AOP能把和类的核心业务无关,多个类又共同须要的逻辑代码封装起来,当对象须要时自动调用,这样就减小了类中的重复代码,下降了类之间的耦合性,提升了程序的维护性。
  AOP术语
  一、横切关注点
  对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
  二、切面(aspect)
  类是对物体特征的抽象,切面就是对横切关注点的抽象
  三、链接点(joinpoint)
  被拦截到的点,由于Spring只支持方法类型的链接点,因此在Spring中链接点指的就是被拦截到的方法,实际上链接点还能够是字段或者构造器
  四、切入点(pointcut)
  对链接点进行拦截的定义
  五、通知(advice)
  所谓通知指的就是指拦截到链接点以后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类
  六、目标对象
  代理的目标对象
  七、织入(weave)
  将切面应用到目标对象并致使代理对象建立的过程
  八、引入(introduction)
  在不修改代码的前提下,引入能够在运行期为类动态地添加一些方法或字段
  AOP应用场景
  1)日志 ,方法执行开始和完成以及出现异常均可以用日志记录
  2)缓存 ,第一次调用查询数据库,将查询结果放入内存对象, 第二次调用, 直接从内存对象返回,不须要查询数据库
  3)事务 ,调用方法前开启事务, 调用方法后提交关闭事务
  等等
  AOP的配置
  1)导入Maven依赖
  <dependencies>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
  </dependency>
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.3.14.Release</version>
  </dependency>
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.3.14.RELEASE</version>
  </dependency>
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>4.3.14.RELEASE</version>
  </dependency>
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.3.14.RELEASE</version>
  </dependency>
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>4.3.14.RELEASE</version>
  </dependency>
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>4.3.14.RELEASE</version>
  </dependency>
  <!-- https://mvnrepository.com/art... -->
  <dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.8.13</version>
  </dependency>
  <!-- https://mvnrepository.com/art... -->
  <dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.13</version>
  </dependency>
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.3.14.RELEASE</version>
  </dependency>
  <dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.1.2</version>
  </dependency>
  <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
  </dependency>
  </dependencies>
  编写Service接口和实现类,这里只完成模拟操做
  public interface AdminService {
  void saveAdmin();
  void updateAdmin();
  void deleteAdmin();
  void selectAdmin();
  }
  public class AdminServiceImpl implements AdminService {
  @Override
  public void saveAdmin() {
  System.out.println("Invoke saveAdmin");
  }
  @Override
  public void updateAdmin() {
  System.out.println("Invoke updateAdmin");
  }
  @Override
  public void deleteAdmin() {
  System.out.println("Invoke deleteAdmin");
  }
  @Override
  public void selectAdmin() {
  System.out.println("Invoke selectAdmin");
  }
  }
  3)编写自定义加强类,该类的方法对应AOP的5种加强通知,分别是:
  1)前置通知before :方法调用前执行
  2)后置通知after-returning:方法调用后执行,出现异常不执行
  3)后置通知after:方法调用后执行,出现异常也执行
  4)异常通知after-throwing:出现异常执行
  5)环绕通知around:方法调用先后执行
  /**
  * 自定义加强类
  */
  public class MyAdvise {
  public void before() throws Throwable {
  System.out.println("这是before");
  }
  public void afterReturning(){
  System.out.println("这是afterReturning");
  }
  public void after(){
  System.out.println("这是after");
  }
  public void afterThrowing() throws Throwable {
  System.out.println("这是afterThrowing");
  }
  public Object around(ProceedingJoinPoint point) throws Throwable {
  System.out.println("这是around -- 前置执行");
  Object obj = point.proceed();
  System.out.println("这是around -- 后置执行");
  return obj;
  }
  }
4)  Spring配置文件
  <?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.or...
  http://www.springframework.or...
  http://www.springframework.or...d">
  <bean id="userService" class="com.qianfeng.springaop.UserServiceImpl"/>
  <bean id="adminService" class="com.qianfeng.springaop.AdminServiceImpl"/>
  <bean id="myAdvise" class="com.qianfeng.springaop.MyAdvise"/>
  <aop:config>
  <!--配置切入点
  execution中第一个表明任意返回值类型,后面是包名,第二个表明类名的开头,第三个*表明任意方法,(..)表明任意方法参数
  -->
  <aop:pointcut id="c" expression="execution( com.qianfeng.springaop.ServiceImpl.*(..))"/>
  <!--配置方面-->
  <aop:aspect ref="myAdvise">
  <aop:before method="before" pointcut-ref="c"/>
  <aop:after method="after" pointcut-ref="c"/>
  <aop:after-returning method="afterReturning" pointcut-ref="c"/>
  <aop:after-throwing method="afterThrowing" pointcut-ref="c"/>
  <aop:around method="around" pointcut-ref="c"/>
  </aop:aspect>
  </aop:config>
  </beans>
  5)运行测试
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("classpath:applicationContext-aop.xml")
  public class TestAOP {
  @Test
  public void test(){
  ApplicationContext app = new
  ClassPathXmlApplicationContext("applicationContext-aop.xml");
  AdminService as = (AdminService) app.getBean("adminService");
  as.deleteAdmin();
  }
  }
  能够看到执行任何Service类的方法,都会调用MyAdvise中的通知方法
  总结
  本章咱们学习了Spring AOP的配置,首先须要添加通知加强类,能够添加前置、后置或环绕等通知方法来完成某些通用操做,而后再Spring的配置文件中配置切入点,最后是配置切面,将通知类中的方法和切面中的方法进行绑定,最后调用Java对象的方法时,就能看到AOP的效果。程序员

相关文章
相关标签/搜索