加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_丽江站长网 (http://www.0888zz.com/)- 科技、建站、数据工具、云上网络、机器学习!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Spring中AOP实例分析

发布时间:2021-11-19 10:20:26 所属栏目:PHP教程 来源:互联网
导读:Spring中AOP实例详解 需要增强的服务 假如有以下service,他的功能很简单,打印输入的参数并返回参数。 @Service public class SimpleService { public String getName(String name) { System.out.println(get name is: + name); return name; } } 定义切面

Spring中AOP实例详解
 
需要增强的服务
 
假如有以下service,他的功能很简单,打印输入的参数并返回参数。
@Service
public class SimpleService {
    public String getName(String name) {
        System.out.println(get name is:  + name);
        return name;
    }
}
 
定义切面和切点
@Component
@Aspect
public class L
ogAspect {
    // 定义切点
    @Pointcut(within(com.ydoing.service..*))
    // @Pointcut(execution(* com.ydoing.service.*.*(..)))
    public void pointCut() {
    }
}
 
Before增强处理
    // 定义Before增强处理
    // 在目标方法调用之前执行增强处理
    @Before(pointCut())
    public void before(JoinPoint jp) {
        // 获取连接点传入参数
        // Object args = jp.getArgs();
        System.out.println(Before增强处理--execute before target method call);
    }
 
--------------------------------------------------------------------------------
 
测试输出:
Before增强处理--execute before target method call
get name is: Bob
 
AfterReturning增强
    // 在目标方法调用之后执行增强处理
    @AfterReturning(pointcut = pointCut(), returning = ret)
    public void afterReturning(JoinPoint jp, Object ret) {
        System.out.println(AfterReturnin增强处理--execute after target method call, return value is : + ret);
    }
 
--------------------------------------------------------------------------------
 
测试输出:
get name is: Bob
AfterReturnin增强处理--execute after target method call, return value is :Bob
 
Around增强
    @Around(pointCut())
    public void around(ProceedingJoinPoint jp) {
        System.out.println(Around增强--around start...);
        Object[] args = jp.getArgs();
 
        // 修改目标方法传入的参数
        args[0] = around_add_ + args[0];
        try {
            System.out.println(修改传入参数后执行输出:);
            jp.proceed(args);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        System.out.println(Around增强--around end);
    }
 
--------------------------------------------------------------------------------
 
输出:
Around增强--around start...
修改传入参数后执行输出:
get name is: around_add_Bob
Around增强--around end
 
After增强
    // 无论是否发生异常都会 处理
    @After(pointCut())
    public void after() {
        System.out.println(After增强--always do no matter what happen);
    }
 
--------------------------------------------------------------------------------
 
输出:
get name is: Bob
After增强--always do no matter what happen
 
AfterThrowing增强
    @AfterThrowing(pointcut = pointCut(), throwing = ex)
    public void afterThrowing(JoinPoint jp, Throwable ex) {
        System.out.println(error is:  + ex);
    }
 
这里没抛异常,就没有输出了
 
测试代码如下
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = com.ydoing.service,com.ydoing.aspect)
public class AppConfig {
    public static void main(String[] args) {
        @SuppressWarnings(resource)
        ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
        SimpleService service = ctx.getBean(SimpleService.class);
        service.getName(Bob);
    }
}

(编辑:应用网_丽江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读