spring初体验第二天--基于配置文件的aop

演示的spring版本为4.2.4

首先配置文件中引入名称空间:

<?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">

引入jar包:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>

仍然是之前的案例代码:

IUserService:

package com.itheima.service;

public interface IUserService {
    void save();
}

UserServiceImpl:

package com.itheima.service.impl;

import com.itheima.dao.IUserDao;
import com.itheima.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserDao userDao;

    public void save() {
        System.out.println(this.getClass().getName()+" 执行...");
        userDao.save();
    }
}

IUserDao:

package com.itheima.dao;

public interface IUserDao {
    void save();
}

UserDaoImpl:

package com.itheima.dao.impl;

import com.itheima.dao.IUserDao;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements IUserDao {
    @Override
    public void save() {
        System.out.println(this.getClass().getName()+" 执行...");
        System.out.println("保存用户...");
    }
}

test类:

package com.itheima.test;

import com.itheima.domain.Address;
import com.itheima.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//junit中的注解,表示在SpringJUnit4ClassRunner中运行测试方法
@RunWith(SpringJUnit4ClassRunner.class)
//ContextConfiguration指定配置文件,相对路径的applicationContext.xml
@ContextConfiguration("classpath:applicationContext.xml")
public class AnnotationTest1 {
    @Autowired//给对象赋值
    private IUserService userService;

    @Test
    public void test1(){
        userService.save();
    }
}

配置文件:

<?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="com.itheima"></context:component-scan>

    <!-- AOP配置 -->
    <aop:config>
        <!-- 设置一个切入点:告诉spring框架哪些方法需要增强 -->
        <aop:pointcut id="userDaoImpl_save" expression="execution(* com.itheima.dao.impl.UserDaoImpl.save(..))"></aop:pointcut>
        <!-- 设置一个切面:告诉spring框架调用哪个切面类的哪个方法-->
        <aop:aspect ref="myAspectXml">
            <!-- before表示在userDaoImpl_save中的save方法前面切一刀,加入myAspectXml中的writeLog -->
            <aop:before pointcut-ref="userDaoImpl_save" method="writeLog"></aop:before>
        </aop:aspect>
        <aop:aspect ref="myAspectXml">
            <!-- after表示在userDaoImpl_save中的save方法后面切一刀,加入myAspectXml中的writeLog2 -->
            <aop:after method="writeLog2" pointcut-ref="userDaoImpl_save"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

切面类:

package com.itheima.util;

import org.springframework.stereotype.Component;

@Component
public class MyAspectXml {
    public void writeLog(){
        System.out.println("开始写日志....");
    }
    public void writeLog2(){
        System.out.println("写日志结束....");
    }
}

执行test1方法,结果:


将applicationContext.xml中的配置注释,再运行:


在原来的类与测试类都不变的情况下,修改配置文件与添加切面类,可以改变原来执行的方法。

下一篇会讲aop配置文件中更多的配置

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值