Javaee Spring基于XML的AOP开发

快速入门

1. 导入 AOP 相关坐标
2. 创建目标接口和目标类(内部有切点)
3. 创建切面类(内部有增强方法)
4. 将目标类和切面类的对象创建权交给 spring
5. applicationContext.xml 中配置织入关系
6. 测试代码

项目结构:

 

AccountService 创建目标接口
package wwx.aop;

public interface AccountService {
    public void save();
    //AccountService,Spring,AOPTest,AccountServiceImpl,MyAspect
}
AccountServiceImpl 创建目标类

package wwx.aop;
//目标类
public class AccountServiceImpl implements AccountService {
    @Override
    public void save() {
        System.out.println("save方法执行了。。。");
        int num=10/0;//0不能作分母,有异常,异常通知才会执行
    }
}
MyAspect 创建切面类(内部有增强的方法)

package wwx.aop;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Date;

//切面类
public class MyAspect {
    //前置通知
    public void before() {
        Date date = new Date();
        System.out.println("前置通知:"+date);
    }
    //后置通知
    public void afterreturning(){
        System.out.println("后置通知:方法执行完毕");
    }
    //环绕通知
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        //记录方法执行的时间
        long time01 = System.currentTimeMillis();
        System.out.println("方法执行前");
        //切入点方法执行
        joinPoint.proceed();
        //记录方法执行后的时间
        long time2 = System.currentTimeMillis();
        System.out.println("方法执行后");
        //后-前
        System.out.println("环绕通知:方法执行的时间"+(time2-time01));
    }
    //异常通知
    public void throwing(){
        System.out.println("异常通知:系统升级中...");
    }
    //最终通知
    public void after(){
        System.out.println("最终通知:我一定会执行");
    }
}
Spring 将目标类和切面类的对象创建权交给 spring,在 spring配置文件中配置织入关系

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

    <!--配置目标类-->
    <bean id="accountService" class="wwx.aop.AccountServiceImpl"></bean>
    <!--配置切面类-->
    <bean id="myAspect" class="wwx.aop.MyAspect"></bean><!--这行,是把创建切面类的权利交给Spring-->
    <!--私有方法一旦执行,spring就会监听到,一旦监听到就会根据类的接口
    去整一个代理对象,然后根据通知的类型,去对私有方法进行增强
     -->
    <!--配置aop的织入-->
     <aop:config>
     <!--配置切面:切入点+通知-->
    <aop:aspect ref="myAspect"><!--指定myAspect作为切面类-->
 <!--第1种织入方式-->      
        <!--前置通知-->
        <aop:before method="before" pointcut="execution(public void wwx.aop.AccountServiceImpl.save())"></aop:before>  

        <!--后置通知-->
        <aop:after-returning method="afterreturning" pointcut-ref="pt1"></aop:after-returning>
<!--第2种织入方式-->
    <aop:pointcut id="pt1" expression="execution(public void
wwx.aop.AccountServiceImpl.save())"/>  

        <!--环绕通知-->
        <aop:around method="around" pointcut-ref="pt1"></aop:around>
        <!--异常通知-->
        <aop:after-throwing method="throwing" pointcut-ref="pt1"></aop:after-throwing>
        <!--最终通知-->
         <aop:after method="after" pointcut-ref="pt1"></aop:after>
    </aop:aspect>
     </aop:config>



</beans>
AOPTest 测试代码
package wwx.aop;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPTest {
   @Test
    public void test01()
   {
       //加载配置文件
       ApplicationContext app = new ClassPathXmlApplicationContext("Spring.xml");
       //获得Bean
       AccountService accountService = (AccountService) app.getBean("accountService");
       //执行方法
       accountService.save();

   }
}

目标类中有异常时: 

 目标类中无异常时: 

 

2.2 切点表达式的写法
表达式语法:
execution ([ 修饰符 ] 返回值类型 包名 . 类名 . 方法名 ( 参数 ))

 

访问修饰符可以省略
返回值类型、包名、类名、方法名可以使用星号 * 代表任意
包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类
参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表
execution ( public void com . lzw . aop . Service . method ())
execution ( void com . lzw . aop . Service . * (..))
execution ( * com . lzw . aop . * . * (..))
execution ( * com . lzw . aop .. * . * (..))
execution ( * * .. * . * (..))
2.3 通知的类型
通知的配置语法:
< aop : 通知类型 method = 切面类中方法名 ” pointcut = 切点表达式 "></aop: 通知类型 >

 

 

 

 

JavaEE实验三中的Spring AOP是指Spring框架中的面向切面编程。它可以在不修改原有代码的情况下,通过在程序运行时动态地将代码织入到类的指定方法中,实现对方法的增强。具体来说,Spring AOP主要包括两种代理方式:JDK动态代理和CGLIB动态代理。其中,JDK动态代理只能代理实现了接口的类,而CGLIB动态代理则可以代理任意类。在Spring AOP中,我们可以通过XML配置或注解来定义切面和通知,实现对目标方法的增强。 以下是JavaEE实验三中Spring AOP的基本步骤: 1. 定义切面类,即包含通知的类。 2. 定义通知,即增强的代码逻辑。 3. 配置切面和通知,可以通过XML配置或注解来实现。 4. 在需要增强的方法上添加切点,即指定需要增强的方法。 5. 运行程序,观察增强效果。 下面是一个使用XML配置的Spring AOP的例子: 1. 定义切面类和通知: ```java public class LogAspect { public void before() { System.out.println("方法执行前"); } public void after() { System.out.println("方法执行后"); } } ``` 2. 在XML配置文件中定义切面和通知: ```xml <bean id="logAspect" class="com.example.LogAspect"/> <aop:config> <aop:aspect ref="logAspect"> <aop:before method="before" pointcut="execution(* com.example.Service.*(..))"/> <aop:after method="after" pointcut="execution(* com.example.Service.*(..))"/> </aop:aspect> </aop:config> ``` 3. 在需要增强的方法上添加切点: ```java @Service public class UserServiceImpl implements UserService { @Override public void addUser(User user) { System.out.println("添加用户:" + user.getName()); } } ``` 4. 运行程序,观察增强效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值