Spring 项目如何使用AOP

简介: Spring 项目如何使用AOP

导入依赖:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.15</version>
        </dependency>
 
 
//不导入可能会报错
        <dependency>
            <groupId>org.aspectj</groupId >
            <artifactId>aspectjweaver</artifactId >
            <version>1.6.11</version >
        </dependency>

1.创建实体类  

service接口

public interface MyService {
 
    void add();
    void delete();
}

service接口的实现类

public class MyServiceImpl implements MyService{
    @Override
    public void add() {
        System.out.println("执行了add         方法");
    }
 
    @Override
    public void delete() {
        System.out.println("执行了delete方法");
    }
}

定义要横向切入的内容:

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);
    }
}
public class BeforeLog implements MethodBeforeAdvice {
 
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法");
    }
}

2.编写xml文件 开启和配置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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 
    <bean id="beforelog" class="log.BeforeLog"/>
    <bean id="afterlog" class="log.AfterLog"/>
    <bean id="service" class="service.MyServiceImpl"/>
 
    <aop:config>
<!--        配置切入点:切入点名称  在那个类的那个方法上执行-->
        <aop:pointcut id="pointcut" expression="execution(* service.MyServiceImpl.*(..))"/>
 
<!--        在哪执行 执行什么-->
        <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="beforelog" pointcut-ref="pointcut"/>
    </aop:config>
 
</beans>

3.测试:

     ApplicationContext context = new ClassPathXmlApplicationContext("test.xml");
        MyService service = context.getBean("service", MyService.class);
        service.add();


目录
相关文章
|
6天前
|
Java 应用服务中间件 Android开发
Eclipse创建Spring项目
本文介绍了在Eclipse中创建Spring项目的步骤,包括如何配置Tomcat服务器、创建项目、部署项目到Tomcat以及添加Spring框架所需的JAR包。
23 1
Eclipse创建Spring项目
|
8天前
|
Java Spring
ij社区版如何创建spring项目
如何在IntelliJ IDEA社区版中创建Spring项目,包括安装Spring Boot Helper插件的步骤和创建过程。
20 1
ij社区版如何创建spring项目
|
21天前
|
设计模式 Java 测试技术
spring复习04,静态代理动态代理,AOP
这篇文章讲解了Java代理模式的相关知识,包括静态代理和动态代理(JDK动态代理和CGLIB),以及AOP(面向切面编程)的概念和在Spring框架中的应用。文章还提供了详细的示例代码,演示了如何使用Spring AOP进行方法增强和代理对象的创建。
spring复习04,静态代理动态代理,AOP
|
4天前
|
Java 编译器 Spring
Spring AOP 和 AspectJ 的区别
Spring AOP和AspectJ AOP都是面向切面编程(AOP)的实现,但它们在实现方式、灵活性、依赖性、性能和使用场景等方面存在显著区别。‌
17 2
|
12天前
|
Java Spring 容器
Spring IOC、AOP与事务管理底层原理及源码解析
【10月更文挑战第1天】Spring框架以其强大的控制反转(IOC)和面向切面编程(AOP)功能,成为Java企业级开发中的首选框架。本文将深入探讨Spring IOC和AOP的底层原理,并通过源码解析来揭示其实现机制。同时,我们还将探讨Spring事务管理的核心原理,并给出相应的源码示例。
50 9
|
6天前
|
前端开发 Java 应用服务中间件
【Spring】Spring MVC的项目准备和连接建立
【Spring】Spring MVC的项目准备和连接建立
21 2
|
6天前
|
XML Java 应用服务中间件
【Spring】运行Spring Boot项目,请求响应流程分析以及404和500报错
【Spring】运行Spring Boot项目,请求响应流程分析以及404和500报错
76 2
|
6天前
|
前端开发 安全 Java
【Spring】Spring Boot项目创建和目录介绍
【Spring】Spring Boot项目创建和目录介绍
32 2
|
6天前
|
Java 应用服务中间件 Maven
idea+maven+tomcat+spring 创建一个jsp项目
这篇文章介绍了如何在IntelliJ IDEA中使用Maven和Tomcat创建一个JSP项目,包括配置Maven依赖、设置Tomcat服务器、编写JSP页面、创建控制器和配置文件,以及项目的运行结果。
38 0
idea+maven+tomcat+spring 创建一个jsp项目
|
3天前
|
XML Java 数据格式
Spring的IOC和AOP
Spring的IOC和AOP
15 0