深入理解Spring框架中的AOP、IoC和DI

目录

AOP(面向切面编程)

核心概念:

AOP示例:

IoC(控制反转)

核心概念:

IoC示例:

DI(依赖注入)

前置:

构造函数注入:

属性注入:

方法注入(Setter方法注入):

总结


 

Spring框架是一个功能强大且广泛使用的Java应用程序开发框架,它引入了多种关键概念和技术,其中包括AOP(面向切面编程)、IoC(控制反转)和DI(依赖注入)。本文将深入介绍这些概念,希望帮助大家更好地理解Spring框架的核心原理和用途~~😁

AOP(面向切面编程)

AOP是一种编程范式,它允许我们将应用程序的不同关注点分离开来,例如日志记录、事务管理、安全性等。

核心概念:

在Spring中,AOP通过代理机制实现,主要使用以下几个关键概念:

切面(Aspect): 切面是一种模块化的方式来处理横切关注点(cross-cutting concerns),例如日志记录或性能监控。

连接点(Join Point): 连接点是在应用程序执行过程中可以被拦截的点,例如方法调用或异常抛出。

通知(Advice): 通知是在连接点上执行的操作,包括前置通知(在方法执行之前执行)、后置通知(在方法执行之后执行)、异常通知(在方法抛出异常时执行)和环绕通知(在方法执行前后都执行)等。

切点(Pointcut): 切点是一组连接点的集合,它定义了在何处应用通知。

Spring的AOP功能允许我们通过配置将通知与切点关联起来,从而实现横切关注点的模块化管理。


AOP示例:

假设我们有一个简单的Java类 Calculator,它包含两个方法:addsubtract

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }
}

现在,我们想要在执行这两个方法之前和之后记录日志,但我们不想修改 Calculator 类的源代码。这就是AOP的用武之地🤩

首先,我们创建一个切面类,该类包含了我们的日志记录逻辑:

public class LoggingAspect {
    public void logBefore() {
        System.out.println("Before method execution: Logging started.");
    }

    public void logAfter() {
        System.out.println("After method execution: Logging finished.");
    }
}

接下来,我们配置AOP,告诉Spring在执行 Calculator 类的方法之前和之后应用我们的切面。这可以通过XML配置或Java注解来完成。以下是使用XML配置的示例:

<aop:config>
    <aop:aspect id="loggingAspect" ref="loggingAspectBean">
        <aop:before method="logBefore" pointcut="execution(* Calculator.*(..))" />
        <aop:after method="logAfter" pointcut="execution(* Calculator.*(..))" />
    </aop:aspect>
</aop:config>

在这里,我们创建了一个切面 loggingAspect,并指定了 logBeforelogAfter 方法分别在 Calculator 类的所有方法执行之前和之后执行。pointcut 属性定义了切入点,指定了应该应用切面的方法。

最后,我们需要在Spring容器中配置 CalculatorLoggingAspect 的bean。然后,当我们调用 Calculator 的方法时,AOP会自动应用日志记录切面,而不需要在 Calculator 类中编写任何日志记录代码。

Calculator calculator = (Calculator) context.getBean("calculator");
int result = calculator.add(5, 3);

输出结果:

Before method execution: Logging started.
After method execution: Logging finished.

这个简单的例子展示了AOP的概念:通过切面和切入点,我们可以将横切关注点(如日志记录)从应用程序的核心业务逻辑中分离出来,从而实现了模块化和松耦合的代码。这使得我们可以更轻松地维护和扩展应用程序。

IoC(控制反转)

IoC是Spring框架的核心原则之一,它是一种设计模式,也称为依赖反转。在传统的开发中,对象通常负责管理其依赖关系,而在IoC中,控制权反转,由容器负责管理对象的生命周期和依赖关系。

核心概念:

容器(Container): Spring的IoC容器是一个负责创建、管理和组装应用程序组件的容器。最常见的容器是BeanFactory和ApplicationContext。

Bean(组件): Bean是Spring管理的应用程序对象。这些对象通过容器的配置来创建和管理,通常被称为IoC容器中的组件。

配置元数据(Configuration Metadata): 配置元数据是指告诉容器如何创建和组装Bean的信息,通常使用XML配置、注解或Java配置方式提供。

依赖注入(DI): DI是IoC的一种具体实现,它指的是容器将依赖关系注入到Bean中,而不是Bean自己负责管理它们的依赖。这可以通过构造函数注入、属性注入或方法注入来实现。


IoC示例:

假设我们有一个简单的Java类 MessageService,它用于发送消息:

public class MessageService {
    public void sendMessage(String message) {
        System.out.println("Sending message: " + message);
    }
}

现在,我们想要在另一个类中使用 MessageService 来发送消息,但我们不想直接在这个类中创建 MessageService 的实例,而是希望通过Spring容器来管理 MessageService

首先,我们需要配置Spring容器。在XML配置文件中,我们可以定义 MessageService 的bean:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="messageService" class="com.example.MessageService" />
</beans>

在这里,我们定义了一个名为 messageService 的bean,它的类是 com.example.MessageService

接下来,我们可以创建一个类,让Spring容器注入 MessageService 的实例。这可以通过在类中声明一个属性并使用 @Autowired 注解来实现:

import org.springframework.beans.factory.annotation.Autowired;

public class MyApplication {
    private MessageService messageService;

    @Autowired
    public MyApplication(MessageService messageService) {
        this.messageService = messageService;
    }

    public void sendMessage(String message) {
        messageService.sendMessage(message);
    }
}

在这里,我们在 MyApplication 类的构造函数中接受了一个 MessageService 的参数,并使用 @Autowired 注解将它注入到属性中。

最后,我们可以使用Spring容器来创建 MyApplication 的实例并调用 sendMessage 方法:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MyApplication app = context.getBean(MyApplication.class);

        app.sendMessage("Hello, Spring!");
    }
}

在这个IoC示例中,我们通过Spring容器配置了 MessageService 的bean,然后让Spring容器自动将它注入到 MyApplication 类中。这样,我们实现了控制反转,将对象的创建和依赖管理交给了Spring框架,使我们的代码更加松耦合、可维护和可测试~~😎

DI(依赖注入)

DI是IoC的一种具体实现,它是指容器负责将组件的依赖关系注入到组件中,而不是由组件自己创建或查找依赖。DI可以通过以下三种方式实现:

前置:

首先我们准备一个StudentService类

@Service
public class StudentService {
    public void sayHi(){
        System.out.println("do student service sayHi()");
    }
}

我们还得准备一个启动类:

public class App {
        public static void main(String[] args) {
            //1,获取Spring上下文
            ApplicationContext applicationContext =
                    new ClassPathXmlApplicationContext("spring-config.xml");
            //2,得到Bean对象
            MyService myService=
                    applicationContext.getBean("StudentService",StudentService.class);
            //3,使用Bean对象
            myService.sayHi();
        }
    }

 

构造函数注入:

通过Bean的构造函数来注入依赖。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    private final StudentService studentService;

    @Autowired
    public MyService(StudentService studentService) {
        this.studentService = studentService;
    }
}

属性注入:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    @Autowired
    private StudentService studentService;
}

方法注入(Setter方法注入):

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    private StudentService studentService;

    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
}

然后我们使用其中一种注入之后,我们启动启动类,就会显示如下运行结果:

do student service sayHi()

总结

Spring框架的AOP(面向切面编程)允许有效地处理横切关注点,如日志和事务,提高了代码的模块化性。IoC(控制反转)将对象创建和依赖关系管理交给Spring容器,提高了代码的可扩展性和可维护性。DI(依赖注入)则是IoC的实现方式之一,使依赖关系通过构造函数、属性或方法注入,降低了组件之间的耦合度。这些核心概念共同构成了Spring框架的基础,为构建灵活、可维护的应用程序提供了强大的工具。🤗

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马可波罗.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值