Spring--Aop_xml 爱被打了一巴掌 2024-02-18 15:12 43阅读 0赞 bean.xml <?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:p="http://www.springframework.org/schema/p" 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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd" xmlns:context="http://www.springframework.org/schema/context"> <context:component-scan base-package="com.dw"/> <!-- <aop:aspectj-autoproxy/> --> <bean id = "car" class = "com.dw.car.impl.Tanke"/> <bean id = "iterception" class="com.dw.model.Iterception"/> <aop:config> <aop:pointcut expression="execution(* com.dw.car.*.*(..))" id="point"/> <aop:aspect id="aspect" ref="iterception"> <aop:before method="before1" pointcut-ref="point"/> <aop:after-returning method="after1" pointcut-ref="point"/> <aop:around method="around1" pointcut="execution(* com.dw.car.*.*(..))"/> </aop:aspect> </aop:config> </beans> Iterception.java: package com.dw.model; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; //@Aspect //@Component public class Iterception { // @Pointcut("execution(* com.dw.car.*.*(..))") // public void myMethod(){}; // // //@Before("execution(public void com.dw.car.impl.Tanke.move())") // @Before("execution(* com.dw.car.*.*(..))") // public void before() { // System.out.println("exec before"); //} // // @AfterReturning("execution(* com.dw.car.*.*(..))") // public void afterReturning() { // System.out.println("exec after"); // } public void before1() { System.out.println("before……"); } public void after1() { System.out.println("after……"); } public void around1(ProceedingJoinPoint pjp) { System.out.println("around 1 ……"); System.out.println("around 2 ……"); try { pjp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("around 3 ……"); } } 输出: before…… around 1 …… around 2 …… tanke is moving! around 3 …… after…… Aspectj是一个面向切面的框架,使用时需要引入相关jar包。aop的内部实现原理是动态代理。
还没有评论,来说两句吧...