Spring 框架的底层原理
Spring 框架的底层原理主要包括以下几个方面:
核心容器(IoC 容器)
-
IoC(控制反转)原理 :
-
依赖注入(DI) :这是 IoC 的实现方式之一。在传统的程序开发中,程序组件自己负责创建和管理其依赖对象,而在 Spring 中,这种依赖关系被反转了。例如,一个
BookService
类依赖于BookRepository
类来完成书籍数据的持久化操作。在没有 Spring 的情况下,BookService
类需要自己创建BookRepository
的实例。但在 Spring 中,BookService
不再自己创建这个依赖,而是将这个依赖交给 Spring 容器来管理。我们通过配置文件或者注解来告诉 Spring 容器BookService
需要BookRepository
作为依赖,Spring 容器会在运行时将这个依赖注入到BookService
中。// BookRepository.java public interface BookRepository {void save(Book book); }// BookRepositoryImpl.java @Component public class BookRepositoryImpl implements BookRepository {@Overridepublic void save(Book book) {// 持久化逻辑} }// BookService.java @Component public class BookService {@Autowiredprivate BookRepository bookRepository;public void addBook(Book book) {bookRepository.save(book);} }
在上面的代码中,
@Component
注解用于将BookRepositoryImpl
和BookService
类标记为 Spring 管理的组件。@Autowired
注解用于自动装配BookRepository
依赖,在运行时,Spring 容器会将BookRepositoryImpl
的实例注入到BookService
中。 -
BeanFactory 和 ApplicationContext :
BeanFactory
是 Spring IoC 容器的基础,它负责创建和管理 Bean(对象)。ApplicationContext
是BeanFactory
的子接口,提供了更多的功能,如国际化、事件传播等。在配置文件中定义好 Bean 后,通过ApplicationContext
加载配置文件并获取 Bean。// applicationContext.xml 配置文件示例 <beans><bean id="bookService" class="com.example.BookService"><property name="bookRepository" ref="bookRepositoryImpl"></property></bean><bean id="bookRepositoryImpl" class="com.example.BookRepositoryImpl"></bean> </beans>// 加载配置文件并获取 Bean 示例 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); BookService bookService = context.getBean("bookService", BookService.class);
AOP(面向切面编程)
-
AOP 基本概念 :
-
切面(Aspect) :是一个横切关注点。例如日志记录、事务管理等,这些关注点通常会横跨多个模块或类。在 Spring 中,切面可以是一个类,通过注解的方式定义。
-
连接点(Join Point) :程序执行过程中明确的点,如方法执行、异常处理等。在 Spring AOP 中,连接点主要是方法执行。
-
通知(Advice) :在切面的某个特定连接点执行的动作。包括前置通知(在连接点之前执行)、后置通知(在连接点之后执行)等。
-
切入点(Pointcut) :定义了切面在哪些连接点上执行。通过表达式来指定,如特定的方法签名等。
-
目标对象(Target Object) :被通知的对象。
-
代理(Proxy) :Spring AOP 通过代理模式来实现对目标对象的增强。例如,当我们对一个业务类应用 AOP 时,Spring 会创建一个代理对象,代理对象在调用目标方法前后添加我们定义的横切逻辑。
-
-
底层原理 :Spring AOP 主要基于动态代理技术。对于实现了接口的类,Spring 会使用 JDK 动态代理;对于没有实现接口的类,Spring 会使用 CGLIB 库来生成代理类。在运行时,当目标对象的方法被调用时,代理会拦截这个调用,按照我们定义的通知类型(如前置通知、后置通知等)来执行相应的横切逻辑,然后再调用原始方法或者在后置通知中处理方法执行后的结果。
// 切面类示例 @Aspect @Component public class LoggingAspect {// 定义切入点,匹配 com.example 包下所有类的所有方法@Pointcut("execution(* com.example..*(..))")public void logPointcut() {}// 前置通知@Before("logPointcut()")public void beforeLogging(JoinPoint joinPoint) {System.out.println("Before method: " + joinPoint.getSignature().getName());}// 后置通知@After("logPointcut()")public void afterLogging(JoinPoint joinPoint) {System.out.println("After method: " + joinPoint.getSignature().getName());} }// 目标类 @Component public class BookService {public void addBook(Book book) {// 添加书籍逻辑System.out.println("Adding book: " + book.getTitle());} }
当调用
BookService
的addBook
方法时,Spring AOP 会根据切面类中的定义,在方法执行前后输出相应的日志信息。事务管理
-
声明式事务管理原理 :Spring 的声明式事务管理主要是基于 AOP 的。通过在配置文件或注解中声明事务规则,Spring 会自动在方法执行前后添加事务相关的代码。例如,当一个方法被标记为事务方法时,Spring 会在方法执行前开启事务,在方法执行成功后提交事务,在方法执行出现异常时回滚事务。
// application.properties 配置文件中开启事务管理 spring.datasource.url=jdbc:mysql://localhost:3306/bookdb spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver// 配置 JPA spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true// 业务类示例 @Service @Transactional public class BookService {@Autowiredprivate BookRepository bookRepository;public void transferBook(long bookId, String fromUser, String toUser) {// 模拟书籍转移逻辑,开启事务Book book = bookRepository.findById(bookId).orElse(null);if (book != null) {book.setOwner(toUser);bookRepository.save(book);// 模拟可能出现的异常情况if (true) {throw new RuntimeException("Transfer failed");}}} }
在上面的代码中,
@Transactional
注解用于声明事务。当transferBook
方法被调用时,Spring 会开启一个事务。如果方法执行成功,事务提交;如果方法执行过程中出现异常(如这里人为抛出的RuntimeException
),事务回滚。事务管理器会根据配置的事务规则(如传播行为、隔离级别等)来控制事务的执行。
-