Spring AOP功能多种实现方式

XML配置方式

maven依赖

<properties>
  	<spring-version>5.1.7.RELEASE</spring-version>
</properties>

<dependencies>
  
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-version}</version>
  </dependency>
  
</dependencies>    

applicationContext.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: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">
    <!-- 1.配置目标对象 -->
    <bean name="userService" class="com.kang.service.impl.UserServiceImpl" ></bean>
    <!-- 2.配置通知对象 -->
    <bean name="userAdvice" class="com.kang.advice.UserAdvice" ></bean>
    <!-- 3.配置将通知织入目标对象 -->
    <aop:config>
        <!-- 配置切入点 -->
        <aop:pointcut expression="execution(* com.kang.service.impl.*ServiceImpl.*(..))" id="pc"/>
        <!--切面 -->
        <aop:aspect ref="userAdvice" >
            <!--连接通知方法与切点 -->

            <!-- 指定名为before方法作为前置通知 -->
            <aop:before method="before" pointcut-ref="pc" />
            <!-- 后置 -->
            <aop:after-returning method="afterReturning" pointcut-ref="pc" />
            <!-- 环绕通知 -->
            <aop:around method="around" pointcut-ref="pc" />
            <!-- 异常拦截通知 -->
            <aop:after-throwing method="afterException" pointcut-ref="pc"/>
            <!-- 后置 -->
            <aop:after method="after" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>
</beans>

切面增强

package com.kang.advice;

import org.aspectj.lang.ProceedingJoinPoint;

public class UserAdvice {
    //前置通知
    public void before(){
        System.out.println("这是前置通知!!");
    }
    //后置通知
    public void afterReturning(){
        System.out.println("这是后置通知(如果出现异常不会调用)!!");
    }
    //环绕通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return proceed;
    }
    //异常通知
    public void afterException(){
        System.out.println("出事啦!出现异常了!!");
    }
    //后置通知
    public void after(){
        System.out.println("这是后置通知(出现异常也会调用)!!");
    }
}

被代理对象

public interface UserService {
    void save();
    void delete();
    void update();
    void find();
}

被代理对象的实现类

public class UserServiceImpl implements UserService {

    public void save() {
        System.out.println("save(): 保存用户!");
        //int i = 1/0;
    }

    public void delete() {
        System.out.println("delete(): 删除用户!");
    }

    public void update() {
        System.out.println("update(): 更新用户!");
    }

    public void find() {
        System.out.println("find(): 查找用户!");
    }
}

测试类

public class SpringAopTest {

    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.save();
    }
}

结果:

这是前置通知!!
这是环绕通知之前的部分!!
save!
这是后置通知(出现异常也会调用)!!
这是环绕通知之后的部分!!
这是后置通知(如果出现异常不会调用)!!

AOP注解方式

注解增强,Spring框架会自动为与AspectJ切面配置的Bean创建代理,proxy-target-class="true"属性表示被代理的目标对象是一个类

package com.kang.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//通知类
@Aspect
@Component
public class UserAdiveAnott {

    @Pointcut("execution(* com.kang.service.impl.*ServiceImpl.*(..))")
    private void execute(){}

    //前置通知
    //指定该方法是前置通知,并制定切入点
    @Before("execute()")
    public void before(){
        System.out.println("这是前置通知!!");
    }

    //后置通知
    @AfterReturning("execute()")
    public void afterReturning(){
        System.out.println("这是后置通知(如果出现异常不会调用)!!");
    }

    //环绕通知
    @Around("execute()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return proceed;
    }
    //异常通知
    @AfterThrowing("execute()")
    public void afterException(){
        System.out.println("出事啦!出现异常了!!");
    }
    //后置通知
    @After("execute()")
    public void after(){
        System.out.println("这是后置通知(出现异常也会调用)!!");
    }
}

applicationContext.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: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">
    <!-- 1.配置目标对象 -->
    <bean name="userService" class="com.kang.service.impl.UserServiceImpl" ></bean>
    <!-- 2.配置通知对象 -->
    <bean name="userAdvice" class="com.kang.advice.UserAdiveAnott" ></bean>
    <!-- 3.开启aop自动代理-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

结果:

这是环绕通知之前的部分!!
这是前置通知!!
save...
这是环绕通知之后的部分!!
这是后置通知(出现异常也会调用)!!
这是后置通知(如果出现异常不会调用)!!

零配置实现AOP

增加配置类

@Configuration  //用于表示当前类为容器的配置类,类似<beans/>
@ComponentScan(basePackages="com.kang")
//扫描的范围,相当于xml配置的结点<context:component-scan/>
@EnableAspectJAutoProxy
//自动代理,相当于<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
public class SpringAopConfig {
    //在配置中声明一个bean,相当于<bean name="userService" class="com.kang.service.impl.UserServiceImpl" ></bean>
    @Bean
    public UserService getUserService(){
        return new UserServiceImpl();
    }
}

测试类

public static void main(String[] args) {
        // 通过类初始化容器
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringAopConfig.class);
        UserService userService = (UserService) context.getBean(UserService.class);
        userService.save();
    }

结果

这是环绕通知之前的部分!!
这是前置通知!!
save...
这是环绕通知之后的部分!!
这是后置通知(出现异常也会调用)!!
这是后置通知(如果出现异常不会调用)!!

参考

https://docs.spring.io/spring-framework/docs/5.1.7.RELEASE/spring-framework-reference/core.html#aop-pointcuts

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值