Aop面向切面编程

 

 

aop在spring中的作用

 

  • 提供声明式事务,允许用户自定义切面
  • 横切关注点:跨域引用程序多个模块的方法或功能,即是,与我们业务逻辑无关的,但是我们需要
  • 关注的部分,就是横切关注点,如日志,安全,缓存,事务等等。
  • 切面:aspect :横切关注点,被模块化的特殊对象,即,它是一个类
  • 通知:切面必须要完成的工作,它是类中一个方法
  • 目标:被通知对象
  • 代理:向目标对象引用通知之后创建的对象,
  • 切入点:切面通知执行的地点的定义,
  • 连接点:与切入点匹配的执行点。

 

 

如图所示:

      

 

 

 

 

即:可以理解为在不改变原有的代码的情况下,去增加新的功能。

 

 

添加依赖:

<dependency>
             <groupId>org.aspectj</groupId>
             <artifactId>aspectjweaver</artifactId>
             <version>1.9.4</version>
         </dependency>

 

第一种方式:

首先编写我们的业务接口和实现类

public interface UserService {

    //增
    public  void  add();

    //删除
    public  void  delete();

    //改
    public  void  update();

    //  查
    public  void  select();

    
}

 

 

 

实现类

public class UserServiceImpl implements UserService{



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


    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }


    @Override
    public void update() {
        System.out.println("更新了一个用户");
    }


    @Override
    public void select() {
        System.out.println("查询了一个用户");
    }

    
}

 

 

然后去写我们的增强类,编写两个一个前置增强,一个后置增强。 

前置增强:

public class Log implements MethodBeforeAdvice {

    
    //重写接口抽象方法    method 要执行的目标对象的方法
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
                                      // 参数       //目标对象

        //  获取反射对象  然后.getname()
        System.out.println(target.getClass().getName()+""+"的"+method.getName()+"被执行了");
        

    }

}

 

 

 

后置增强:

public class AfterLog implements AfterReturningAdvice {
    
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {

        System.out.println("执行了"+method.getName()+"方法,返回的结果为:"+returnValue);

    }
    
}

 

 

 

最后在去spring中注册,并且实现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">


<aop:config>
        <!--切入点,expression :表达式,execution 要执行的位置 -->
        <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>

        <!-- 执行环绕增加 -->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>


    </aop:config>

</beans>

 

 

 

测试结果:

 

 

第二种方式:

自定义类来实现:

目标业务不变依旧是:userServiceImpl

第一步:写我们自己的一个切入类,

//  自定义一个类,变成他的切面。
public class DiyPointCut {

    public  void  before(){
        System.out.println("*****方法执行前**********88");
    }


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

}

 

到spring中去配置:

<!--     方式二:自定义类 -->
<bean id="diy" class="com.kuang.diy.DiyPointCut"/>

    <aop:config>
           <!-- 自定义切面,ref 要引用的类 -->

        <!--把这个类  标记成切面  -->
        <aop:aspect ref="diy"
        >
            <!--切入点 -->
            <aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
            <!--通知 -->
            <aop:before method="before" pointcut-ref="point"/>

            <aop:after method="after" pointcut-ref="point"/><!--pointcut-ref  在这个切入点,执行-->


        </aop:aspect>

    </aop:config>

 

测试结果:

 

'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的AOP面向切面编程的测试代码示例,使用Spring框架实现: 首先,创建一个切面类 `LoggingAspect`,用于定义切面逻辑: ```java import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature().getName()); } @After("execution(* com.example.service.*.*(..))") public void afterAdvice(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature().getName()); } } ``` 然后,创建一个测试服务类 `UserService`,用于演示AOP的应用: ```java import org.springframework.stereotype.Service; @Service public class UserService { public void createUser(String username) { System.out.println("Creating user: " + username); } public void deleteUser(String username) { System.out.println("Deleting user: " + username); } } ``` 最后,创建一个Spring Boot应用程序,并在启动类中进行配置: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; @SpringBootApplication @EnableAspectJAutoProxy public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); UserService userService = context.getBean(UserService.class); userService.createUser("John"); userService.deleteUser("John"); } } ``` 在上述示例中,`LoggingAspect` 切面类使用 `@Before` 和 `@After` 注解分别定义了在目标方法执行前和执行后的逻辑。切面逻辑会应用于 `UserService` 类中的所有方法。 当运行应用程序时,可以看到切面逻辑在方法执行前和执行后打印了相应的日志消息。 这是一个简单的AOP面向切面编程的示例,你可以根据实际需求进行更复杂的切面逻辑定义和应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值