<?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 http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="jdbc.properties"/> <bean id="myDataSource" class="com.powernode.spring6.jdbc.MyDataSource"> <!-- 引入context标签,利用placeholder的location来指定属性配置文件的路径,默认从类的根路径下开始加载--> <!-- 老方法,全写死的方法,非常不灵活--> <property name="driver" value="${jdbc.Driver}"></property> <property name="url" value="${jdbc.url}"></property> <!-- ${}方法加载的释放会优先加载windows的username,导致错误出现--> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> </beans>
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="jdbc.properties"/>
<bean id="myDataSource" class="com.powernode.spring6.jdbc.MyDataSource">
<!-- 引入context标签,利用placeholder的location来指定属性配置文件的路径,默认从类的根路径下开始加载-->
<!-- 老方法,全写死的方法,非常不灵活-->
<property name="driver" value="${jdbc.Driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<!-- ${}方法加载的释放会优先加载windows的username,导致错误出现-->
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>
jdbc.Driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:13306/spring6 jdbc.username=root jdbc.password=abc123
jdbc.Driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:13306/spring6
jdbc.username=root
jdbc.password=abc123
package com.powernode.spring6.jdbc; import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.util.logging.Logger; public class MyDataSource implements DataSource { private String Driver; private String url; private String username; private String password; @Override public String toString() { return "MyDataSource{" + "Driver='" + Driver + '\'' + ", url='" + url + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } public void setDriver(String driver) { Driver = driver; } public void setUrl(String url) { this.url = url; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } @Override public Connection getConnection() throws SQLException { return null; } @Override public Connection getConnection(String username, String password) throws SQLException { return null; } @Override public PrintWriter getLogWriter() throws SQLException { return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { } @Override public void setLoginTimeout(int seconds) throws SQLException { } @Override public int getLoginTimeout() throws SQLException { return 0; } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { return null; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; } }
package com.powernode.spring6.jdbc;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
public class MyDataSource implements DataSource
{
private String Driver;
private String url;
private String username;
private String password;
@Override
public String toString() {
return "MyDataSource{" +
"Driver='" + Driver + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public void setDriver(String driver) {
Driver = driver;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public Connection getConnection() throws SQLException {
return null;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return null;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
}
@Override
public int getLoginTimeout() throws SQLException {
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}
@Test public void TestProperties() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring-properties.xml"); MyDataSource MyDataSource = applicationContext.getBean("myDataSource", MyDataSource.class); System.out.println(MyDataSource); }
@Test
public void TestProperties()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring-properties.xml");
MyDataSource MyDataSource = applicationContext.getBean("myDataSource", MyDataSource.class);
System.out.println(MyDataSource);
}