通过获取配置文件的方式获取dataSource

news/2024/8/30 23:05:01 标签: 开发工具

第一步:新建工程  SecondSpring

文件目录结构如下:

第二步: 导入spring相关的jar包,已经 mysql的jar包

过程略...

 

第三步: 新建连接数据库的配置文件

db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456

 

第四步:新增spring配置文件

common.xml

<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-2.5.xsd">
    
    <import resource="xmlfolder/app1.xml" />
    <import resource="xmlfolder/innerbean.xml" />
    <import resource="xmlfolder/singleton.xml" />
    <import resource="xmlfolder/annotation.xml" />
    <import resource="xmlfolder/gather.xml" />
    <import resource="xmlfolder/date.xml" />
    <import resource="xmlfolder/db.xml" />
</beans>    

db.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
  <!--用于加载db.properties配置文件,否则下面的$ 无法使用 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>db.properties</value> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> </beans>

 

第五步:  新建测试类

JDBCTest.java

package com.xuzhiwen.spring7;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class JDBCTest {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("common.xml");
        DriverManagerDataSource ds = (DriverManagerDataSource) app.getBean("dataSource");
        System.out.println("url:"+ds.getUrl());
        System.out.println("username:"+ds.getUsername());
        System.out.println("password:"+ds.getPassword());
    }
}

 

第六步:运行结果如下

 

转载于:https://www.cnblogs.com/beibidewomen/p/7390800.html


http://www.niftyadmin.cn/n/1075530.html

相关文章

C#遍历CheckedListBox中的勾选项

正确的代码&#xff1a; string selectedItem string.Empty;for(int i0; i < checkedListBox1.Items.Count; i){if (checkedListBox1.GetItemChecked(i)){ selectedItem selectedItem " " checkedListBox1.Items[i].ToString(); }} 以下代码只能选择最后单击的…

openstack控制台出错Failed to connect to server (code: 1006)

#错误描述 &#xff1a; 打开openstack控制台后&#xff0c;控制台出错&#xff0c;显示Failed to connect to server (code: 1006)&#xff0c;如下图&#xff1a; #错误分析&#xff1a; 点击只显示控制台可看到地址是controller&#xff1a;6080…… 应该是域名解析出错。…

重启Apache出现警告信息Could not reliably determine the server's fully qualified domain name,

1.错误描述&#xff1a; 重启Apache服务出现下列警告&#xff1a; service apache2 restart * Restarting web server apache2 AH00558: apache2: Could not reliably determine the servers fully qualified domain name, using 192.168.1.1. Set the ServerName directive g…

Disk /dev/mapper/vg-var doesn't contain a valid partition table

问题描述 rootcompute-44:~# fdisk -lDisk /dev/sda: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors Units sectors of 1 * 512 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minim…

在Android中全屏显示GIF图片

1、自定义一个GifView 首先自定义一个GifView&#xff0c;用于显示Gif图片。GifView的代码参考自https://github.com/Cutta/GifView。 package cc.duduhuo.gifviewdemo.view;import android.annotation.SuppressLint; import android.content.Context; import android.graphics…

openstack多主机部署(ubuntu14.04)J版

拓扑图 部署前准备 安装ubuntu14.04 ssh设置 修改/etc/ssh/sshd_config 配置文档中的下面一句 # vi /etc/ssh/sshd_config PermitRootLogin yes # service ssh restart 网络设置 ip根据自己情况设置,这儿只列出一台的设置 # vi /etc/network/interfaces # This file …

oracle创建新用户

[转发] http://www.cnblogs.com/monkeybrother/p/6405314.html 1.首先我们可以用scott用户以sysdba的身份登录oracle.   conn scott/tiger as sysdba 2.然后我就可以来创建用户了. create user zs identified by zs; 3.修改用户的密码. alter user zs identified by 123456…

Openstack Nova Failed to allocate network(s)

错误描述&#xff1a; 上图的错误信息为 Error: 创建实例 "cirros" 失败: 请稍后再试 [错误: Build of instance 751267aa-e9e2-41c0-945b-9bbb94096849 aborted: Failed to allocate the network(s), not rescheduling.]. 计算节点中nova-compute.log的信息 2015…