@Configuration
@ImportResource(“classpath:config.xml”)
public class StoreConfig {
@Value(“${url}”)
private String url;
/* 为什么会在username前加jdbc*/
/原因如果单是username,系统会认为这是系统登录用户名/
@Value(“${jdbc.username}”)
private String username;
@Value("${password}")
private String password;
@Bean
public MyDriverManager myDriverManager() {
return new MyDriverManager(url, username, password);
}
config.xml
<?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="classpath:/config.properties"/>
</beans>
config.properties
jdbc.password=root
jdbc.url=jdbc\:mysql\://127.0.0.1.8\:3306/database
jdbc.username=root