(二)Spring框架学习--AOP

本文介绍面向切面编程(AOP)的概念及其在Spring框架中的应用。通过具体实例展示了如何将日志记录等横切关注点与核心业务逻辑分离,并使用Spring AOP实现它们的动态组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的周边功能,比如性能统计,日志,事务管理等等

周边功能在Spring的面向切面编程AOP思想里,即被定义为切面

在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP

代码

maven依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>5.0.8.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.0.8.RELEASE</version>
    </dependency>
package cn.why.test1.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * Title:           LoggerAspect
 * Description:
 * Company:         AceGear
 * Author:          henrywang
 * Date:            2018/8/2
 * JDK:             8
 * Encoding:        UTF-8
 */
public class LoggerAspect {
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:"+ joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log: "+joinPoint.getSignature().getName());
        return object;
    }
}
package cn.why.test1.pojo;

/**
 * Title:           Category
 * Description:
 * Company:         AceGear
 * Author:          henrywang
 * Date:            2018/8/2
 * JDK:             8
 * Encoding:        UTF-8
 */
public class Category {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package cn.why.test1.pojo;

/**
 * Title:           Producgt
 * Description:
 * Company:         AceGear
 * Author:          henrywang
 * Date:            2018/8/2
 * JDK:             8
 * Encoding:        UTF-8
 */
public class Product {
    private int id;
    private String name;
    private Category category;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
}
package cn.why.test1.service;

/**
 * Title:           ProductService
 * Description:
 * Company:         AceGear
 * Author:          henrywang
 * Date:            2018/8/2
 * JDK:             8
 * Encoding:        UTF-8
 */
public class ProductService {
    public void doSomeService(){
        System.out.println("doSomeService");
    }
}
package cn.why.test1.test;

import cn.why.test1.service.ProductService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Title:           TestSpring
 * Description:
 * Company:         AceGear
 * Author:          henrywang
 * Date:            2018/8/2
 * JDK:             8
 * Encoding:        UTF-8
 */
public class TestSpring {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext2.xml"});
        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}

applicationContext2.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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean name="c" class="cn.why.test1.pojo.Category">
        <property name="name" value="yyy"/>
    </bean>
    <bean name="p" class="cn.why.test1.pojo.Product">
        <property name="name" value="product1"/>
        <property name="category" ref="c"/>

    </bean>
    <bean name="s" class="cn.why.test1.service.ProductService"/>
    <bean id = "loggerAspect" class="cn.why.test1.aspect.LoggerAspect"/>
    <aop:config>
        <aop:pointcut id="loggerCutpoint" expression="execution(* cn.why.test1.service.ProductService.*(..))"/>
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:around method="log" pointcut-ref="loggerCutpoint"/>
        </aop:aspect>
    </aop:config>
</beans>

整体结构

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值