Spring框架的AOP之注解的方式

9 篇文章 0 订阅

Spring框架的AOP技术(注解方式)

1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包
    * 先引入Spring框架开发的基本开发包
    * 再引入Spring框架的AOP的开发包
        * spring的传统AOP的开发的包
            * spring-aop-4.2.4.RELEASE.jar
            * com.springsource.org.aopalliance-1.0.0.jar

        * aspectJ的开发包
            * com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
            * spring-aspects-4.2.4.RELEASE.jar

2. 步骤二:创建Spring的配置文件,引入具体的AOP的schema约束
    <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.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    </beans>

3. 步骤三:创建包结构,编写具体的接口和实现类
    * com.itheima.demo1
        * CustomerDao           -- 接口
        * CustomerDaoImpl       -- 实现类

4. 步骤四:将目标类配置到Spring中
    <bean id="customerDao" class="com.itheima.demo1.CustomerDaoImpl"/>

5. 步骤五:定义切面类
把<aop:config>
        <!-- 引入切面类 -->
        <aop:aspect ref="myAspectXml">
            <!-- 定义通知类型:切面类的方法和切入点的表达式 -->
            <aop:before method="log" pointcut="execution(public * com.itheima.demo3.CustomerDaoImpl.save(..))"/>
        </aop:aspect>
    </aop:config>
分散成下面几个   
    * 添加切面和通知的注解
        * @Aspect                   -- 定义切面类的注解
        **相当于<aop:aspect ref="myaspectxml">**
        * 通知类型(注解的参数是切入点的表达式)
            * @Before               -- 前置通知
            * @AfterReturing        -- 后置通知
            * @Around               -- 环绕通知
            * @After                -- 最终通知
            * @AfterThrowing        -- 异常抛出通知

    * 具体的代码如下
        @Aspect
        public class MyAspectAnno {
            @Before(value="execution(public void com.itheima.demo1.CustomerDaoImpl.save())")

//相当于
public void log(){
System.out.println(“记录日志…”);
}
}

6. 步骤六:在配置文件中定义切面类
    <bean id="myAspectAnno" class="com.itheima.demo1.MyAspectAnno"/>

**7. 步骤七:在配置文件中开启自动代理**
    <aop:aspectj-autoproxy/>

8. 完成测试
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class Demo1 {

        @Resource(name="customerDao")
        private CustomerDao customerDao;

        @Test
        public void run1(){
            customerDao.save();
            customerDao.update();
        }
    }

通知类型

1. 通知类型
    * @Before               -- 前置通知
    * @AfterReturing        -- 后置通知
    * @Around               -- 环绕通知(目标对象方法默认不执行的,需要手动执行)
    * @After                -- 最终通知
    * @AfterThrowing        -- 异常抛出通知

2. 配置通用的切入点
    * 使用@Pointcut定义通用的切入点

    @Aspect
    public class MyAspectAnno {
        @Before(value="MyAspectAnno.fn()")
        public void log(){
            System.out.println("记录日志...");
        }
        @Pointcut(value="execution(public void com.itheima.demo1.CustomerDaoImpl.save())")
        public void fn(){}
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值