Spring:通过XML方式实现aop学习笔记

使用maven 导入依赖


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
    </dependencies>

创建applicationContext.xml,配置 加入约束aop

<?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 http://www.springframework.org/schema/aop/spring-aop.xsd"
>
<!--使用aop前需要进行约束配置-->


    <!--目标对象-->
    <bean id="target" class="aop.Target"></bean>

    <!--切面对象-->
    <bean id="myAspect" class="aop.MyAspect"></bean>
    <!--配置织入 告诉spring 哪些方法(切点)需要进行增强-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
        <!--切面=切点+通知-->
            <aop:before method="before" pointcut="execution(public void aop.Target.test())"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

创建目标接口,实现类,切面

public interface TargetInterface {
    public void test();
}
public class Target implements TargetInterface{
    public void test() {
        System.out.println("test ready");
    }
}
public class MyAspect {
    public void before(){
        System.out.println("前置增强");
    }
}

编写测试类(注解方式)

import aop.Target;
import aop.TargetInterface;
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;

@RunWith(SpringJUnit4ClassRunner.class)//指定测试引擎
@ContextConfiguration("classpath:applicationContext.xml")//指定配置文件
public class AopTest {
    @Autowired
    private TargetInterface target;//需要用接口而不是实现类
    @Test
    public void test1(){
        target.test();
    }
}

编写测试类(普通方式)

/**需注意在applicationContext.xml中
**<aop:config>中加入
**加入proxy-target-class="true"
**否则报错com.sun.proxy.$Proxy6 cannot be cast to xxx
**1.默认使用 JDK 动态代理,这样便可以代理所有的接口类型(interface)
**2.Spring AOP也支持CGLIB的代理方式。如果我们被代理对象没有实现任何接口或者实现的接口都是空接口,则是CGLIB
**3.我们可以强制使用CGLIB,指定proxy-target-class = "true" 或者 基于注解@EnableAspectJAutoProxy(proxyTargetClass = true)
**/
public class AopTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Target target = (Target) context.getBean("target");
        target.test();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值