Spring third-party resource configuration management

insert image description here
[Spring Technology] Spring Architecture
[Spring Technology] Spring IOC and DI Entry Case
[Spring Technology] Spring Bean Configuration and Examples
[Spring Technology] Spring Dependency Injection (DI Configuration)
[Spring Technology] Spring Third-Party Resource Configuration Management
[Spring Technology] Spring Container Technology
[Spring Technology] Spring Annotation Development
[Spring Technology] Spring Integration Mybatis&Junit Unit Test
[Spring Technology] Spring AOP
[Spring Technology] Spring Transaction Management

Description: Take the management of DataSource connection pool objects as an example to explain third-party resource configuration management

1. Manage DataSource connection pool objects

problem import

When configuring the database connection parameters, should the injected driver class name be driverClassName or driver?

1.1 Manage Druid connection pool [key points]

database preparation

create database if not exists spring_db character set utf8;
use spring_db;
create table if not exists tbl_account(
    id int primary key auto_increment,
    name varchar(20),
    money double
);
insert into tbl_account values(null,'Tom',1000);
insert into tbl_account values(null,'Jerry',1000);

[Step 1] Add Druid connection pool dependency

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

Note: In addition to adding the above two dependencies, don't forget to add spring-context dependencies.

[Step 2] Configure the DruidDataSource connection pool Bean object

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/spring_db"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>

[Step 3] Obtain the connection pool object from the IOC container in the test class and print it

public class App {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);
    }
}

1.2 Manage c3p0 connection pool

[Step 1] Add c3p0 connection pool dependency

<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>

[Step 2] Configure the c3p0 connection pool Bean object

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_db"/>
    <property name="user" value="root"/>
    <property name="password" value="root"/>
    <property name="maxPoolSize" value="1000"/>
</bean>

Note: There cannot be two connection pools with id="dataSource" in the same Spring container.

[Step 3] Obtain the connection pool object from the IOC container in the test class and print it

public class App {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);
    }
}

2. Load the properties property file [key]

Purpose: Extract the connection parameters of the database into a separate file and decouple it from the Spring configuration file.

problem import

Question 1: How to solve the problem that the system property is read as a result of using EL expression to read the value in the property file?

Question 2: How to write the standard writing method of loading properties file?

2.1 Basic usage

[Step 1] Write the jdbc.properties property file

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=root

[Step 2] Open the context namespace in applicationContext.xml and load the jdbc.properties property file

image-20210730101826913

Tip: If students feel that the above copy and paste method is not easy to change or easy to correct mistakes, in fact, idea has a prompt function, just be careful not to choose the wrong one. Some versions of the idea do not have this prompt, so follow the above copy and paste method to modify it. After the modification, it can be made into a live template template, which can be used directly later.

image-20210730102053281

<context:property-placeholder location="jdbc.properties"/>

[Step 3] Use the EL expression to obtain the value in the jdbc.properties property file where the connection pool bean is configured

<bean class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

After the configuration is complete, run the previous code to obtain the Druid connection pool, and if the connection pool object can be obtained, the configuration is successful.

2.2 Configuration does not load system properties

question

If username=root666 is configured in the property file instead of jdbc.username, then using ${username} to get the name of the computer is not root666.

reason

The system property takes precedence over the one in our properties file, replacing our username=root666.

solve

Solution 1: Change the name, for example, instead of username, call jdbc.username.

Solution 2: Use the system-properties-mode="NEVER" attribute to indicate that system properties are not used.

<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>

2.3 Loading properties file writing method

  • Do not load system properties
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
  • Load multiple properties files
<context:property-placeholder location="jdbc.properties,msg.properties"/>
  • Load all properties files
<context:property-placeholder location="*.properties"/>
  • Load properties file**standard format**
<context:property-placeholder location="classpath:*.properties"/>
  • Load properties file standard format
<context:property-placeholder location="classpath*:*.properties"/>

Guess you like

Origin blog.csdn.net/qq_51808107/article/details/130317170