current position:Home>On Spring notes (annotation-based development)

On Spring notes (annotation-based development)

2023-01-25 11:29:54white bean five

一、第三方资源配置管理


以DataSourceTake the connection pool object as an example,Perform third-party resource configuration management.

1. 管理DataSource连接池对象


spring整合Druid、C3P0数据库连接池

1.1 管理Druid连接池


1、准备数据

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);

2、导入druid的依赖坐标

<dependencies>
    <!--spring-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
    <!--druid连接池-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.16</version>
    </dependency>
    <!--mysql驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
</dependencies>

3、配置druid连接池的bean对象(在spring核心配置文件中配置)

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- 数据源配置,使用druid数据库连接池-->
    <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?useSSL=false&amp;serverTimezone=Asia/Shanghai"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

</beans>

在这里插入图片描述

mysql5.7的数据库驱动:一般会在url后面添加参数useSSL=false(关闭安全验证)

mysql8的数据库驱动:Time zone needs to be addedserverTimezone=Asia/Shanghai.


4、编写测试(从IOC容器中获取连接池对象)

public class App {
    
    public static void main(String[] args) {
    
        // 创建IoC容器对象,加载spring核心配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 从IOC容器中获取连接池对象
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);
    }
}

在这里插入图片描述


1.2 管理C3P0连接池


1、导入C3P0的依赖坐标

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

2、配置c3p0连接池的bean对象(在spring核心配置文件中配置)

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- 数据源配置,使用c3p0数据库连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///spring_db?useSSL=false"/>
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
        <property name="maxPoolSize" value="1000"/>
    </bean>

</beans>

3、编写测试(从IOC容器中获取连接池对象)

public class App {
    
    public static void main(String[] args) {
    
        // 创建IoC容器对象,加载spring核心配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 从IOC容器中获取连接池对象
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);
    }
}

在这里插入图片描述


2. 加载properties属性文件


Extract database connection parameters into a separate configuration file,与SpringCore profile decoupling.

1、编写jdbc.properties属性文件

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

需要加前缀jdbc.xxx,Otherwise, use it laterelThe expression cannot get the attribute value in the configuration file.


2、在spring核心配置文件中开启context命名空间,加载jdbc.properties属性文件

在这里插入图片描述

sudden tips:使用idea提示,生成上面的context命名空间配置.

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 加载数据库配置文件-->
    <context:property-placeholder location="jdbc.properties"/>

</beans>

3、在配置连接池Bean的地方使用EL表达式获取jdbc.properties属性文件中的值

<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>

在这里插入图片描述


Spring加载properties文件写法(XML配置)

1、加载properties文件标准格式:

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

2、不加载系统属性:

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

3、加载多个properties文件:

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

4、加载所有properties文件:

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

二、Spring容器


copyright notice
author[white bean five],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2023/025/202301251124076796.html

Random recommended