spring aop的三种简单实现方式

首先要实现spring aop需要先在pom.xml中导入2个包

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>

方式一:

通过spring api实现

首先编写业务层接口和实现类---------作为aop中的切入点

package com.wjj.service;

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void search();
}
package com.wjj.service;

public class UserServiceImpl implements UserService{

    public void add() {
        System.out.println("增加用户");
    }

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

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

    public void search() {
        System.out.println("查询用户");
    }
}

接下来去实现增强类--------这里我们以前置增强和后置增强为例

package com.wjj.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName()+"的"+method.getName()+"方法被执行了");
    }
}
package com.wjj.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {

    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行了"+o1.getClass().getName()+"的"+method.getName()+"方法被执行了"+o);
    }
}

最后还需要去springaop.xml配置文件注入bean和配置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">

    
    <bean id="userService" class="com.wjj.service.UserServiceImpl"/>
    <bean id="log" class="com.wjj.log.Log"/>
    <bean id="afterLog" class="com.wjj.log.AfterLog"/>
    
    <aop:config>
        <aop:pointcut id="pointcut" expression="
        execution(* com.wjj.service.UserServiceImpl.*(..))"/>
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
     </aop:config>

</beans>

最后可以测试一下,当然我这里用的是@Test,需要导入junit依赖

import com.wjj.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAopTest {
    @Test
    public void test(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        UserService userService = ac.getBean("userService",UserService.class);
        userService.add();
        userService.update();
    }
}

方式二:自定义类来实现aop

 

1.业务层接口和实现类的实现与方法一中一致

2.手动写一个切入类

package com.wjj.log;

public class DiyPointcut {

    public void before(){
        System.out.println("-----方法执行前-----");
    }
    public void after(){
        System.out.println("---------方法执行后---------");
    }
}

3.配置springaop.xml文件(这里依然需要注入bean)

手动指定类中的方法为前置方法,后置方法或者是其他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">


    <bean id="userService" class="com.wjj.service.UserServiceImpl"/>
    <bean id="diy" class="com.wjj.log.DiyPointcut"/>

    <aop:config>
        <aop:aspect ref="diy">
            <aop:pointcut id="diyPointcut" expression="execution(* com.wjj.service.UserServiceImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="diyPointcut"/>
            <aop:after method="after" pointcut-ref="diyPointcut"/>
        </aop:aspect>
    </aop:config>


</beans>

最后测试与方法一中的测试方法一样

 

方式三:使用注解方式实现spring aop

1.业务层接口和实现类的实现与方法一中一致

2.在增强类中配置注解

package com.wjj.config;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AnnotationPointcut {
    @Before("execution(* com.wjj.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("----方法执行前----");
    }

    @After("execution(*  com.wjj.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("-------方法执行后------");
    }
}

3.在springaop.xml文件中注入bean和开启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">


    <bean id="userService" class="com.wjj.service.UserServiceImpl"/>
    <bean id="annotationPointcut" class="com.wjj.config.AnnotationPointcut"/>

    <aop:aspectj-autoproxy/>


</beans>

最后测试与方法一中测试一样,也比较简单尤其是xml文件配置部分

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值