spring-AspectJ环绕通知

本文详细介绍了一个使用Spring框架实现面向切面编程(AOP)的具体案例,包括项目的搭建过程、必要的依赖库引入、配置文件的设置、业务Bean及切面的创建,并通过测试验证了功能的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、创建项目
    项目名称:spring101002
二、添加jar包
    1.在项目中创建lib目录
        /lib
    2.在lib目录下添加相关spring jar包
        --用于AspectJ
        com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
        spring-aspects-3.2.0.RELEASE.jar
        --用于切面编程
        com.springsource.org.aopalliance-1.0.0.jar
        commons-logging.jar
        junit-4.10.jar
        log4j.jar
        --用于切面编程
        spring-aop-3.2.0.RELEASE.jar
        spring-beans-3.2.0.RELEASE.jar
        spring-context-3.2.0.RELEASE.jar
        spring-core-3.2.0.RELEASE.jar
        spring-expression-3.2.0.RELEASE.jar
三、添加配置文件
    1.在项目中创建conf目录
        /conf
    2.在conf目录下添加配置文件
        配置文件名称:applicationContext.xml
        配置文件内容:
        <?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"
               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">
        </beans>
四、创建业务bean
    1.在src目录下创建业务bean包
        包名:cn.jbit.spring101002.service
    2.在包下创建业务bean
        业务bean名称:UserService.java
        业务bean内容:
        /**
         * 被代理类
         * @author Administrator
         *
         */
        public class UserService {
            /**
             * 5.查询个数
             * @return
             */
            public int findCount(){
                System.out.println("查找个数");
                return 0;
            }
        }
五.创建切面
    1)在src下创建包
        包名:cn.jbit.spring101002.aspect
    2)在包下创建自定义切面类
        切面名称:AnnotationAspect.java
        切面内容:
        /**
         * 自定义切面
         * @author Administrator
         *
         */
        @Aspect
        public class AnnotationAspect {
                /**
                 * 5.环绕通知
                 * @throws Throwable 
                 */
                @Around("execution(* cn.jbit.spring101002.service.UserService.*(..))")
                public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
                    System.out.println("环绕通知");
                    Object obj = proceedingJoinPoint.proceed();
                    System.out.println(obj);
                    return obj;
                }
        }
六、在核心配置文件中添加配置信息
    <!-- 配置被代理类 -->
    <bean id="userservice" class="cn.jbit.spring101002.service.UserService"></bean>
    
    <!-- 配置切面 -->
    <bean id="annotationaspect" class="cn.jbit.spring101002.aspect.AnnotationAspect"></bean>
    
    <!-- 配置自动代理  -->
    <aop:aspectj-autoproxy/>
七、测试
    1.在项目中创建test目录
        /test
    2.在test目录中创建测试包
        包名:cn.jbit.spring101002.aspect
    3.在测试包中创建测试类
        测试类名:AnnotationAspectTest.java
        测试内容:
        /**
         * 测试类
         * @author Administrator
         *
         */
        public class AnnotationAspectTest {
            /**
             * 5.测试环绕通知
             */
            @Test
            public void testAround(){
                //读取并加载配置文件
                ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
                //根据bean id 获取bean对象
                UserService userService = (UserService) context.getBean("userservice");
                //调用保存用户方法
                userService.findCount();
            }

        }

本文转自  素颜猪  51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1562132


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值