current position:Home>On Spring notes (annotation-based development)
On Spring notes (annotation-based development)
2023-01-25 11:29:54【white 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&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
The sidebar is recommended
- Based on VirtualBox centos7 virtual machine installation
- JVM process cache - Caffeine
- Code Caprice No24 | Backtracking Algorithm Theoretical Basis, 77. Combination
- The Road to Algorithm Practice——[String] Leetcode 824 Goat Latin
- Git summary - Prompt to submit or temporarily store changes when switching to a branch
- Productivity tools - [gitlab configuration] One-time configuration of GitLab account
- Echo+Vue+ElementUI management background source code
- Revel+Vue+ElementUI framework use and build tutorial
- Basic practice tutorial of Revel+Vue+ElementUI framework
- Go Web Development Revel+Vue+ElementUI Framework Practical Tutorial
guess what you like
Construction and deployment tutorial of Revel+Vue+ElementUI framework
8. Java loop advanced comprehensive exercises - infinite loop and jump control statement, every seven passes, square root, judging whether it is a prime number, guessing number games
Control statement + exception handling perfect lucky draw applet-java basics
9. Java Array Knowledge Encyclopedia
Lucky draw applet-java basics
Java basic implementation calculator applet
The front end of actual combat: copy write millet's official website on the first day
1. Linux application programming and network programming---file IO notes in Linux system
7. Linux application programming and network programming --- thread full solution Notes
[Front-end notes - CSS] 10. Cascading and inheritance + selector
Random recommended
- 8. Linux application programming and network programming---Linux network programming notes
- [Java|golang] 1828. Count the number of points in a circle
- Add environment variables under Linux system and will not overwrite the previous method of adding environment variables
- 2 · Linux application programming and network programming - file attributes notes
- Redis interview questions (classic 7 questions)
- [Front-end notes——CSS] 11. Box model + background and border
- C + + large Numbers together, according to a combined
- 3. Linux application programming and network programming --- get system information notes
- The use and principle of Kafka message queue
- 5. Linux application programming and network programming---signal notes in Linux
- 6 · Linux application programming and system programming - senior IO notes
- Java collection common interview questions (4)
- 072-JAVA project training: Imitation QQ instant messaging software series lecture 7 (explaining the realization of the chat interface and functions)
- Coordination center performance comparison: how zookeeper solves the load balancing problem
- 070-JAVA project training: imitation QQ instant messaging software series lecture five (explain user registration function)
- Ubuntu installation and configuration (brief)
- 073-JAVA project training: imitation QQ instant messaging software series lecture eight (explain query and add friend function)
- SQL injection classification and error injection EXP
- All basic commands in linux fail, showing that the command cannot be found
- 4. Linux application programming and network programming---Linux process full solution notes (difference between process and program)
- Linux system - basic IO
- Hanlp's understanding of user-defined dictionaries (java version)
- Brief description and configuration of Maven
- 071-JAVA project training: imitation QQ instant messaging software series lecture six (explaining the function of QQ main interface)
- 【Maximum LeetCode】January Algorithm Training (12) Linked List
- 【Max LeetCode】January Algorithm Training (13) Doubly Linked List
- [Big Data Management] Java implements Bloom filter
- [Maximum LeetCode] Algorithm training in January (14) stack
- [Machine Learning] Adaboost Integrated Algorithm
- [Big Data Management] Java implements cuckoo filter (CF)
- Chaozhou Xiangqiao: "Charming Ancient City, Cultural Sharing" Spring Festival Intangible Cultural Heritage Market Opens
- [Big data management] Java realizes the dictionary tree TireTree
- [Max LeetCode] January Algorithm Training (11) Matrix
- New Express (Web framework based on HTTP module encapsulation NodeJS)
- JavaScript error-prone questions (stack processing, call function, prototype chain questions)
- Space "travel", lion and crane dance, intangible cultural heritage experience...During the Spring Festival, Zhuhai Jinwan is so fun!
- Wine 8.0 official release: better support for running Windows applications on Linux and other systems
- Zhongke Sugon: Sugon's new computer "participates in" "The Wandering Earth 2"
- Linux actual combat notes finishing (1.24)
- Automatically execute the specified sql when the springBoot project starts