zoukankan      html  css  js  c++  java
  • spring-AOP(面向切面编程)-注解方式配置

    项目结构:


    切面类:

    package edu.nf.ch12.service.aspect;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    /**
     * @author wangl
     * @date 2018/10/24
     */
    @Aspect //这个注解标识当前的类为一个切面
    @Component //标识容器受管的Bean对象
    public class UserServiceAspect {
    
        /**
         * 声明一个切入点,并编写切入点表达式
         */
        @Pointcut("execution(* edu.nf.ch12.service.*.*(..))")
        public void pointcut(){
        }
    
        /**
         * 前置通知,指定切入点函数
         * 也可在注解中自定义不同的切入点表达式
         * @Before("execution(...)")
         *
         */
        @Before("pointcut()")
        public void before(JoinPoint joinPoint){
            System.out.println("前置通知..."+joinPoint.getArgs()[0]);
        }
    
        /**
         * 后置通知
         */
        @AfterReturning(value = "pointcut()", returning = "returnVal")
        public void afterReturn(String returnVal){
            System.out.println("后置通知..." + returnVal);
        }
    
        /**
         * 环绕通知
         */
        @Around("pointcut()")
        public Object around(ProceedingJoinPoint pjp) throws Throwable {
            System.out.println("环绕通知前...");
            Object returnVal = pjp.proceed();
            System.out.println("环绕通知后...");
            return returnVal;
        }
    
        /**
         * 异常通知
         * 通过pointcut指定切入点
         */
        @AfterThrowing(pointcut = "pointcut()", throwing = "e")
        public void throwableAdvice(Throwable e){
            System.out.println("异常通知..." + e.getMessage());
        }
    
        /**
         * 最终通知
         */
        @After("pointcut()")
        public void after(){
            System.out.println("最终通知...");
        }
    
    }

    配置类AppConfig:

    package edu.nf.ch12.service.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    /**
     * @author wangl
     * @date 2018/10/24
     */
    @ComponentScan("edu.nf.ch12") //启用包扫描
    @EnableAspectJAutoProxy //启用AspectJ注解自动配置,proxyTargetClass用于指定是否强制使用cglib代理
    public class AppConfig {
    }

    接口类:

    package edu.nf.ch12.service;
    
    /**
     * @author wangl
     * @date 2018/10/24
     */
    public interface UserService {
    
        /**
         * 查询用户
         * @param uid
         * @return
         */
        String getUserNameById(String uid);
    }

    接口实现类:

    package edu.nf.ch12.service.impl;
    
    import edu.nf.ch12.service.UserService;
    import org.springframework.stereotype.Service;
    
    /**
     * @author wangl
     * @date 2018/10/24
     */
    @Service("userService")
    public class UserServiceImpl implements UserService {
    
        @Override
        public String getUserNameById(String uid) {
            System.out.println("查询用户..."+uid);
            return "user1";
        }
    }

    程序测试类:

    package edu.nf.ch12.test;
    
    import edu.nf.ch12.service.UserService;
    import edu.nf.ch12.service.config.AppConfig;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author wangl
     * @date 2018/10/24
     */
    public class UserServiceTest {
    
        @Test
        public void testGetUser(){
            //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
            UserService service = context.getBean("userService", UserService.class);
            service.getUserNameById("1001");
        }
    }

    如果半注解半配置文件实现的话, new ClassPathXmlApplicationContext("applicationContext.xml");实例 然后再配置一个xml

    applicationContext:

    <?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:context="http://www.springframework.org/schema/context"
           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/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="edu.nf.ch12"/>
        <!-- 启用AspectJ注解自动配置,proxy-target-class是否强制使用cglib动态代理,true表示强制-->
        <aop:aspectj-autoproxy/>
    
    </beans>

    运行结果:

  • 相关阅读:
    Ansible学习 Playbooks_1
    Ansible学习 ad-hoc命令
    Ansible学习 Patterns
    【转】对于SQL SERVER 事务日志已满问题整理
    【转】Apache配置正向代理与反向代理
    【转】Python的IDE和破解Code——pycharm
    【转】SQLServer 数据库变成单个用户后无法访问问题的解决方法
    【转】 尝试在数据库5 中提取逻辑页() 失败。该逻辑页属于分配单元xxx而非xxx
    【Oracle】Oracle版本导入导出问题
    【Java】eclipse导入war包二次开发
  • 原文地址:https://www.cnblogs.com/hhmm99/p/9844137.html
Copyright © 2011-2022 走看看