📑 Content of this article: SSM framework integrates Druid from simple to deep, and SpringBoot takes over Druid configuration
📘 Article column: Principle and project development of SpringBoot microservice
🎬 Last updated: January 25, 2022 SpringBoot quick start - this quick start is different. It teaches you the method, not the technology. It teaches you to learn to read official references
🙊 Personal profile: a Junior Program ape who is studying in a two-year college. In line with paying attention to foundation, clock in algorithm and sharing technology, as a blogger who summarizes personal experience, although he may be lazy sometimes, he will stick to it. If you like blog very much, it is suggested to look at the following line ~ (crazy hint QwQ)
🌇 give the thumbs-up 👍 Collection ⭐ Leaving a message. 📝 One key three connection care program ape, start with you and me
📝 SSM framework integrates Druid from simple to deep, and SpringBoot takes over Druid configuration
🙊 1. This is another article on how to teach yourself~
Then yesterday's article on how to self-study the official documents, the corresponding source code, Configuration and other methods has been recognized by many small partners, but how to firm this idea and write @ Configuration for the Configuration we want to customize and inject it into the IOC container in the subsequent front and back-end separation project? It's difficult and a waste of time to learn, but when you master a so-called routine, these configurations are like fish in water. Customized Configuration will brighten your project. We just want to cultivate the ability of self Configuration. Xiaofu is giving you a more common Configuration today, In the future, it is possible to take over the custom Configuration of spring security. I also believe this article can bring you into this door and gain a lot.
📚 2. Spring annotation takes over to SpringBoot
⭐ 1. Spring takes over control
We need to familiarize ourselves with the configuration annotations previously injected into Spring:
@Configuration: declare the current class as configuration class instead of xml file.
@Bean: declared on the method, inject the object returned by the method into the IOC container instead of the bean tag
@value: attribute injection
@PropertySource: Specifies the external property file.
Write a Druid configuration class integrated by Spring and inject it instead of xml
DruidConfig.java
/** * Function description * * @author Alascanfu * @date 2022/1/25 */ @Configuration @PropertySource("classpath:db.properties") public class DruidConfig { @Value("${jdbc.driverClassName}") String driverClassName; @Value("${jdbc.url}") String url; @Value("${jdbc.username}") String username; @Value("${jdbc.password}") String password; @Bean public DataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } }
- Add the @ Configuration annotation to the DruidConfig class to make it a Configuration class in Spring and inject it into the IOC container.
- @PropertySource: import the properties in the external configuration file into it. Of course, xxxproperty is used after SpringBoot Class is used to enable it to be configured in the configuration file, which will not be discussed here and will be described later.
- @Value injects the value of the property in our external configuration file into the configuration class property
- Inject the return of the DataSource object into the IOC container through the @ Bean annotation, so that the configuration in the alternative configuration file can be completed.
Let's debug to see if there is a configured DruidDataSource in our IOC container
@Controller @ResponseBody public class DruidController { @Autowired DataSource dataSource ; @RequestMapping("/dataSource") public String dataSource(){ System.out.println(dataSource); return dataSource.toString(); } }
See, Druid's data source configuration has been imported into QwQ
Xiaofu is here because it is connected to Alibaba cloud servers purchased by itself, so it is understood by en everyone~
You can also see the properties of our configuration.
The above is to configure the data source through annotations in Spring. We only need to change dB Properties, we can rewrite our data.
⭐ 2. SpringBoot takeover configuration ⭐
Students who haven't learned the automatic assembly principle of SpringBoot have a look here~
SpringBoot's automatic assembly principle - it's really as simple as you think~
Students who have read the above articles must understand what a great thing @ ConfigurationProperties annotation has done. It will not only be in our application properties|application. Yaml file to find the configuration with prefix "xxx" and inject it instead of @ Value attribute.
Let's try~
DruidProperties.java
@ConfigurationProperties(prefix = "jdbc") public class DruidProperties { private String driverClassName; private String url ; private String username; private String password; public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
However, there may be red explosion and so on
Let's click in to see how to solve the problem caused by SpringBoot.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
Add the above code to the corresponding POM Try again in the XML file.
At this time, there is no wrong report qwq
DruidAutoConfiguration.java
@Configuration @EnableConfigurationProperties(DruidProperties.class) public class DruidAutoConfiguration { @Autowired DruidProperties druidProperties; @Bean public DataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(druidProperties.getDriverClassName()); dataSource.setUrl(druidProperties.getUrl()); dataSource.setUsername(druidProperties.getUsername()); dataSource.setPassword(druidProperties.getPassword()); return dataSource; } }
application.properties
jdbc.username=root jdbc.password=123456 jdbc.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useUnicode=true jdbc.driverClassName=com.mysql.cj.jdbc.Driver
At this point, the DEBUG test is carried out. Do you want to see whether it is injected into the IOC container?
The answer is yes. There must be a little partner asking. Isn't the above configuration more complicated? Why say that SpringBoot simplifies the configuration???
- In fact, we only see the surface, and the official website also gives advantages: the loosely bound configuration makes our software framework more flexible ~, which is the strength of SpringBoot.
In the official document, the corresponding comparison is also given in the last 2.8 type safe configuration properties of restraint
- @ConfigurationProperties supports metadata and RelaxedBinding, which is loose binding. This is its advantages and benefits, which may not be reflected in small projects, but when you see the application of SpringBoot's automatic assembly principle, you will know its strength.
- Relaxed binding: loose binding
- The property name in the property file is not strictly required to be consistent with the member variable name. Support hump, underline, underline and other transformations, and even support object guidance. For example: user friend. Name: represents the name attribute in the friend attribute of the user object. Obviously, friend is also an object@ value annotation is difficult to complete such an injection method. The configuration file defined in the following way can also be read as the member variable driverClassName
properties
jdbc.driver_Class_Name=com.mysql.jdbc.Driver
yaml
jdbc: driver-class-name: com.mysql.jdbc.Driver
Simplified methods in practical development
When the attribute written in our configuration yaml file only needs one Bean, we can inject it directly into the method that returns the injected Bean.
Note that the premise of this method is that the set method is valid when the corresponding attribute exists.
@Configuration public class DruidAutoConfiguration { //SpringBoot will automatically inject the corresponding attributes into the data source through the set method @Bean @ConfigurationProperties(prefix = "jdbc") public DataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); return dataSource; } }
🙊 ***
The above configuration of Druid can also be based on the learning configuration in Druid's github warehouse. It is not only a simple connection to the four attributes of the database, but also can set other attributes. The minimum number of connections and maximum number of connections in pooling technology can be configured, and then follow the above method, First build a xxxProperties class to support property configuration in the xml file. After property injection, we will build xxxAutoConfiguration for automatic assembly and inject the created object into the IOC container. The original configured automatic assembly class of SpringBoot will invalidate itself because of @ ConditionOnMissingBean. In this way, our configuration will return the assembly content we have configured. Xiaofu strongly recommends that you can try customizing the automatic configuration of spring security to take over permission control. There will be a different world. At the same time, it can also strengthen the understanding of learning SpringBoot. If you're not in a hurry, you can wait for Xiaofu to update and go there. You've written that thing for several days, and you've lost more than two hairs. QwQ chongchong, time will bring a complete end to your achievements.