xml与注解配置AOP开发的步骤

xml方式:

1.建立目标方法(切点)以及通知(即方法)

2.建立xml文件

2.1配置

xmlns:aop="http://www.springframework.org/schema/aop"
以及
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

2.2 配置目标对象、

<bean id="target" class="com.it.aop.Target"></bean>

2.3配置切面对象

<bean id="myAspect" class="com.it.aop.MyAspect"></bean>

2.4配置织入,告诉spring哪些方法需要增强

<aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <!--抽取切点表达式-->
            <aop:pointcut id="myPointcut" expression="execution(* com.it.aop.*.*(..))"/>
            <!--切面:切点+通知-->
            <!--<aop:before method="before" pointcut="execution(public void com.it.aop.Target.save())"/>
            <aop:after-returning method="afterRunning" pointcut="execution(* com.it.aop.Target.*())"/>
            <aop:around method="around" pointcut="execution(* com.it.aop.Target.*())"/>
            <aop:after-throwing method="afterThrowing" pointcut="execution(* com.it.aop.Target.*())"/>
            <aop:after method="after" pointcut="execution(* com.it.aop.Target.*())"/>-->


            <aop:around method="around" pointcut-ref="myPointcut"/>
            <aop:after method="after" pointcut-ref="myPointcut"/>

        </aop:aspect>
    </aop:config>

3.测试类 注解:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")

注解说明(搬运内容,若侵权请告知后删除):

    1)@RunWith:用于指定junit运行环境,是junit提供给其他框架测试环境接口扩展,为了便于使用spring的依赖注入,spring提供了org.springframework.test.context.junit4.SpringJUnit4ClassRunner作为Junit测试环境。

    2)@ContextConfiguration({"classes=Congfig.clsss",classpath:applicationContext.xml"}) 这里可以用classes来直接导入同包下写的配置类。或者导入配置文件。

注解方式配置AOP

1.配置xml文件

xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">

1.2开启组件扫描和aop自动代理(组件扫描是为了扫到目标方法即切点)

<!--开启组件扫描-->
    <context:component-scan base-package="com.it.anno"/>

    <!--aop自动代理-->
    <aop:aspectj-autoproxy/>

2.建立目标方法(需要执行的方法)和切面类(通知内容)

2.1 对于目标方法只需要加上(在测试类中能被扫到以及识别即可)

@Component("xxx")

2.2对于切面类不同的通知对应不同的注解 (在类的上方注解该类位切面类)

@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {


    //配置前置通知
    @Before("execution(* com.it.anno.*.*(..))")
    public void before(){
        System.out.println("前置...");
    }

    public void afterRunning(){
        System.out.println("后置...");
    }


    //@Around("execution(* com.it.anno.*.*(..))")
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强");
        Object proceed = pjp.proceed();//切点方法
        System.out.println("环绕后增强");
        return proceed;
    }

    public void afterThrowing(){
        System.out.println("异常增强");
    }

    //@After("execution(* com.it.anno.*.*(..))")
    @After("MyAspect.pointcut()")
    public void after(){
        System.out.println("最终增强");
    }

    //定义切点表达式
    @Pointcut("execution(* com.it.anno.Target.save())")
    public void pointcut(){}
}

3.测试类 同xml

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值