通过xml配置aop过程

添加aspectJ依赖

<!--添加ajpectJ依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

创建spring配置文件

项目结构:

1.目标类 UserService.java


public class UserServiceImpl implements UserService {
    @Override
    public User getUserByName(String username, Integer age) {
        User user = new User();
        user.setAge(age);
        user.setUsername(username);
        return user;
    }

    @Override
    public int getUserCounts() {
        return 32;
    }
}

2.切面类 UserAspectJ.java

// UserService的代理类,可以为目标类添加额外的方法;使用spring的aop机制,通过xml进行配置
public class UserAspectJ {

    // 输出当前系统时间
    void getCurData() {
        Date date = new Date();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        String curTime = dft.format(date);
        System.out.println(curTime);
    }

    void getCommitInfo() {
        System.out.println("方法执行结束,事务已提交");
    }

}

- 在spring配置文件中创建相应的切面类和目标类

<!--创建目标类和切面类交由spring进行管理-->
    <bean id="target" class="com.righteye.spring.pratice.service.impl.UserServiceImpl"></bean>

    <bean id="myAspectJ" class="com.righteye.spring.pratice.proxy.UserAspectJ"></bean>

- 使用aop:config 配置aop的配置,将相关操作如切入点表达式写在config标签中间

       1. 使用<aop:aspect>配置切面

                参数说明:id, 为切面提供唯一的标识; ref, 引用切面实现类

       2. 使用<aop:pointcut>配置切入点表达式

                参数说明:id, 切入点标识; expression, 切入点表达式,为哪个类添加切面

       3. 使用<aop:xxx> 指明具体的方法引用切入点表达式;指定切入时间

              参数说明:根据aop:xxx的具体不同声明不同的切入时间,method为切面中提供的方法

    <aop:config>

        <aop:aspect id="AspectJConfig" ref="myAspectJ">

            <!--指定切入点, 表明要为哪个目标类添加切面-->
            <aop:pointcut id="pointcut" expression="execution(* com.righteye.spring.pratice.service.impl.*.*(..))"></aop:pointcut>

            <!--指定切入时间-->
            <aop:before method="getCurData" pointcut-ref="pointcut"></aop:before>
            <aop:after method="getCommitInfo" pointcut-ref="pointcut"></aop:after>

        </aop:aspect>

    </aop:config>

完整配置:

<?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: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 https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--创建目标类和切面类交由spring进行管理-->
    <bean id="target" class="com.righteye.spring.pratice.service.impl.UserServiceImpl"></bean>

    <bean id="myAspectJ" class="com.righteye.spring.pratice.proxy.UserAspectJ"></bean>


    <aop:config>

        <aop:aspect id="AspectJConfig" ref="myAspectJ">

            <!--指定切入点, 表明要为哪个目标类添加切面-->
            <aop:pointcut id="pointcut" expression="execution(* com.righteye.spring.pratice.service.impl.*.*(..))"></aop:pointcut>

            <!--指定切入时间-->
            <aop:before method="getCurData" pointcut-ref="pointcut"></aop:before>
            <aop:after method="getCommitInfo" pointcut-ref="pointcut"></aop:after>

        </aop:aspect>

    </aop:config>



</beans>

测试代码:

 @Test
    public void testAspectJByXml() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("pratice/demo01/applicationContext.xml");
        UserService userService = (UserService) ac.getBean("target");
        User user = userService.getUserByName("索隆", 18);
        System.out.println(user);
        System.out.println("-----------华丽分割线-------------");
        int count = userService.getUserCounts();
        System.out.println("数量:" + count);
    }

总结

        主要记录使用xml如何配置aop, 使用xml进行配置,可以减少在java代码中大量引入切面加入的时间,如@Before, @After等等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值