面向切面AOP编程---Spring框架介绍(二)

面向切面(AOP)的概念:Spring提供了面向切面编程的支持,允许通过分离应用的业务逻辑与系统级服务(例如事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。概念比较抽象,下面展示一个使用Spring框架进行AOP编程的案例。

要进行AOP编程,首先必须在Spring的配置文件中引入AOP命名空间(如下阴影部分代码所示)。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
       <aop:aspectj-autoproxy />                //  启动对@Aspect注解进行支持

</beans>

Spring 提供两种切面使用方式(分别是基于XML配置方式和基于注解方式),这里我们采用注解的方式。

第二步,创建业务层业务Bean接口及实现类,代码如下:

接口:

package com.lyk.service;

public interface PersonService {
 public void save(String name);
 public void update(String name, Integer id);
 public String getPersonName(Integer id);
}

实现类:

package com.lyk.service.impl;

import cn.lyk.service.PersonService;

public class PersonServiceBean implements PersonService {

 public String getPersonName(Integer id) {
  System.out.println("我是getPersonName()方法");
  return "xxx";
 }

 public void save(String name) {
  throw new RuntimeException("我爱例外");
  //System.out.println("我是save()方法");
 }

 public void update(String name, Integer id) {
  System.out.println("我是update()方法");
 }

}

至此准备工作做好,第三步正式进行AOP开发。编写一个被声明为切面的类(其中要定义切入点、定义环绕通知等)。代码如下:

package com.lyk.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
 * 切面
 *
 */
@Aspect
public class MyInterceptor {
 @Pointcut("execution (* com.lyk.service.impl.PersonServiceBean.*(..))")
 private void anyMethod() {}//声明一个切入点
 
 @Before("anyMethod() && args(name)")
 public void doAccessCheck(String name) {
  System.out.println("前置通知:"+ name);
 }
 @AfterReturning(pointcut="anyMethod()",returning="result")
 public void doAfterReturning(String result) {
  System.out.println("后置通知:"+ result);
 }
 @After("anyMethod()")
 public void doAfter() {
  System.out.println("最终通知");
 }
 @AfterThrowing(pointcut="anyMethod()",throwing="e")
 public void doAfterThrowing(Exception e) {
  System.out.println("例外通知:"+ e);
 }
 
 @Around("anyMethod()")
 public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
  //if(){//判断用户是否在权限
  System.out.println("进入方法");
  Object result = pjp.proceed();
  System.out.println("退出方法");
  //}
  return result;
 }
 
}
此外,还要将定义的业务Bean和切面类交给Spring管理,添加相应的配置文件。如下:

        <bean id="myInterceptor" class="com.lyk.service.MyInterceptor"/>
        <bean id="personService" class="com.lyk.service.impl.PersonServiceBean"></bean>

第四步,进行Juit测试。代码如下:

package junit.test;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lyk.service.PersonService;

public class SpringAOPTest {

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

 @Test public void interceptorTest(){
  ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
  PersonService personService = (PersonService)cxt.getBean("personService");
  personService.save("xx");
 }
}

如能顺利通过测试,则可看到Junit测试出现绿条,且控制台有正确的结果显示。

over~

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值