


How does the RuoYi framework implement Bean dependency injection without explicitly writing DataSource implementation class?
Apr 19, 2025 pm 05:51 PMRuoYi framework clever Bean dependency injection: no explicit DataSource implementation class required
The RuoYi framework is known for its concise code and efficient development experience. However, beginners may be confused about how it implements Bean dependency injection without explicitly writing the DataSource implementation class. This article will clarify the mechanism behind the RuoYi framework by analyzing the code.
Many developers often encounter errors of "cannot automatically assemble, cannot find a bean of 'datasource' type" when imitating the RuoYi framework's com.ruoyi.framework.config.MybatisConfig
. This is because they try to inject DataSource objects directly, and the RuoYi framework does not directly define the implementation class of DataSource.
The core of RuoYi framework is to cleverly utilize the dependency injection mechanism and @Configuration
annotation of the Spring framework. It does not omit the definition of DataSource, but delegates the creation and configuration of DataSource to the configuration class DruidConfig.java
.
Let's dive into the key code of DruidConfig.java
:
/** * Druid Multi-Data Source Configuration* * @author ruoyi */ @Configuration public class DruidConfig { @Bean @ConfigurationProperties("spring.datasource.druid.master") public DataSource masterDataSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); } @Bean @ConfigurationProperties("spring.datasource.druid.slave") @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true") public DataSource slaveDataSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); } @Bean(name = "dynamicDataSource") @Primary public DynamicDataSource dataSource(DataSource masterDataSource) { Map<object object> targetDataSources = new HashMap(); targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource"); return new DynamicDataSource(masterDataSource, targetDataSources); } // ... }</object>
The @Configuration
annotation marks DruidConfig
as a Spring configuration class. The @Bean
annotation indicates that the method will create a bean and register it into the Spring container. masterDataSource
and slaveDataSource
methods create the master data source and slave data source beans respectively, and read the configuration information from the configuration file through the @ConfigurationProperties
annotation. @ConditionalOnProperty
annotation ensures that the Bean from the data source is created only when enabled in the configuration file. Finally, dataSource
method integrates the master and slave data source to create a dynamic data source bean.
When DataSource
type bean needs to be injected, the Spring container will automatically find and inject the registered masterDataSource
or dynamicDataSource
Bean. Therefore, the RuoYi framework does not have the implementation of DataSource
, but hides the creation and configuration of DataSource
in DruidConfig
class through Spring's dependency injection mechanism and configuration classes, thereby keeping the code concise. Developers only need to declare that they need to inject a Bean of DataSource
type, and the Spring container will automatically complete the dependency injection.
The above is the detailed content of How does the RuoYi framework implement Bean dependency injection without explicitly writing DataSource implementation class?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

As an industry leader, Spring+AI provides leading solutions for various industries through its powerful, flexible API and advanced functions. In this topic, we will delve into the application examples of Spring+AI in various fields. Each case will show how Spring+AI meets specific needs, achieves goals, and extends these LESSONSLEARNED to a wider range of applications. I hope this topic can inspire you to understand and utilize the infinite possibilities of Spring+AI more deeply. The Spring framework has a history of more than 20 years in the field of software development, and it has been 10 years since the Spring Boot 1.0 version was released. Now, no one can dispute that Spring

Introduction RESTful APIs have become an integral part of modern WEB applications. They provide a standardized approach to creating and using Web services, thereby improving portability, scalability, and ease of use. In the Java ecosystem, JAX-RS and springmvc are the two most popular frameworks for building RESTful APIs. This article will take an in-depth look at both frameworks, comparing their features, advantages, and disadvantages to help you make an informed decision. JAX-RS: JAX-RSAPI JAX-RS (JavaAPI for RESTful Web Services) is a standard JAX-RSAPI developed by JavaEE for developing REST

I want to modify the requestbody before routing it to the given uri. Modify the body based on the documentation I'm using org.springframework.cloud.gateway.filter.factory.rewrite.modifyrequestbodygatewayfilterfactory. When starting my server, the server fails to start with the following error reason: Element [spring.cloud.gateway.routes[0].filters[0].modifyrequestbody.class] is not bound. \n\nOperation:\

Optimizing program logging: Tips for setting log4j log levels Summary: Program logging plays a key role in troubleshooting, performance tuning, and system monitoring. This article will share tips on setting log4j log levels, including how to set different levels of logs and how to illustrate the setting process through code examples. Introduction: In software development, logging is a very important task. By recording key information during the running process of the program, it can help developers find out the cause of the problem and perform performance optimization and system monitoring.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

An in-depth analysis of the architecture and working principles of the Spring framework Introduction: Spring is one of the most popular open source frameworks in the Java ecosystem. It not only provides a powerful set of container management and dependency injection functions, but also provides many other functions, such as transactions. Management, AOP, data access, etc. This article will provide an in-depth analysis of the architecture and working principles of the Spring framework, and explain related concepts through specific code examples. 1. Core concepts of the Spring framework 1.1IoC (Inversion of Control) Core of Spring

Detailed explanation of Oracle database connection methods In application development, database connection is a very important link, which carries the data interaction between the application and the database. Oracle database is a relational database management system with powerful functions and stable performance. In actual development, we need to be proficient in different connection methods to interact with Oracle database. This article will introduce in detail several common connection methods of Oracle database and provide corresponding code examples to help readers better understand and apply

Java reflection mechanism is widely used in Spring framework for the following aspects: Dependency injection: instantiating beans and injecting dependencies through reflection. Type conversion: Convert request parameters to method parameter types. Persistence framework integration: mapping entity classes and database tables. AspectJ support: intercepting method calls and enhancing code behavior. Dynamic Proxy: Create proxy objects to enhance the behavior of the original object.
