Spring实战之属性占位符配置器用法示例
作者:cakincqm 时间:2021-05-30 22:58:04
本文实例讲述了Spring实战之属性占位符配置器用法。分享给大家供大家参考,具体如下:
一 配置文件
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- PropertyPlaceholderConfigurer是一个容器后处理器,它会读取
属性文件信息,并将这些信息设置成Spring配置文件的数据。 -->
<bean class=
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>dbconn.properties</value>
<!-- 如果有多个属性文件,依次在下面列出来 -->
<!--value>wawa.properties</value-->
</list>
</property>
</bean>
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"/>
</beans>
二 数据库配置属性
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=32147
三 测试类
package lee;
import javax.sql.DataSource;
import java.sql.*;
import org.springframework.context.*;
import org.springframework.context.support.*;
public class BeanTest
{
public static void main(String[] args)throws Exception
{
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
DataSource ds = (DataSource)ctx.getBean("dataSource");
Connection conn = ds.getConnection();
PreparedStatement pstmt = conn.prepareStatement(
"insert into news_inf value(null , ? , ?)");
pstmt.setString(1 , "疯狂Java讲义1");
pstmt.setString(2 , "Struts 2权威指南2");
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
四 测试结果
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/101165100
标签:Spring,属性占位符配置器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
jdbc和mybatis的流式查询使用方法
2023-08-24 15:39:25
TKmybatis的框架介绍和原理解析
2022-08-28 21:35:42
Java技能点之SimpleDateFormat进行日期格式化问题
2023-09-09 01:50:26
![](https://img.aspxhome.com/file/2023/9/58339_0s.jpg)
Java 客户端操作 FastDFS 实现文件上传下载替换删除功能
2022-06-01 15:01:38
![](https://img.aspxhome.com/file/2023/5/62325_0s.png)
JAVA格式化时间日期的简单实例
2022-10-06 09:14:58
JavaWeb验证码校验功能代码实例
2022-07-18 23:46:52
jvm crash的崩溃日志详细分析及注意点
2022-12-18 07:49:00
java使用ftp上传文件示例分享
2021-10-23 08:33:03
Lombok中@EqualsAndHashCode注解的使用及说明
2023-11-30 04:47:05
![](https://img.aspxhome.com/file/2023/4/64204_0s.png)
SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法
2021-06-17 20:09:50
Java线程安全解决方案(synchronized,ReentrantLock,Atomic)
2022-06-13 12:51:09
深入理解Java虚拟机体系结构
2021-09-15 19:46:53
![](https://img.aspxhome.com/file/2023/6/62366_0s.png)
JAVA中的基本数据类型
2023-07-29 07:38:34
解决Android软键盘弹出覆盖h5页面输入框问题
2023-06-19 11:33:24
![](https://img.aspxhome.com/file/2023/7/83547_0s.jpg)
简单解析java方法在调用在内存中的执行过程
2022-04-12 15:46:26
![](https://img.aspxhome.com/file/2023/6/59756_0s.png)
JVM类加载,垃圾回收
2022-08-16 02:44:36
![](https://img.aspxhome.com/file/2023/1/61771_0s.png)
Java数据结构之链表、栈、队列、树的实现方法示例
2021-10-07 10:40:29
Java实现AWT四大事件的详细过程
2023-11-28 18:39:52
![](https://img.aspxhome.com/file/2023/6/60076_0s.png)
SpringBoot利用限速器RateLimiter实现单机限流的示例代码
2023-04-05 19:57:50
MyBatis快速入门
2023-11-13 06:45:14
![](https://img.aspxhome.com/file/2023/5/62155_0s.png)