xml文件中配置扫描文件:
<context:property-placeholder ignore-unresolvable="true" location="classpath:*.properties"/>
这样可以扫描项目的所有配置文件.
但是这样配置如果是通过@Value来取值的话,只能取到当前所在模块的properties文件
如果想要取其他的propertis文件的值怎么办呢?
可以通过下面的操作来完成这样的目标:
首先根据值创建一个类如:
public class JDBC{
private String driver;
private String url;
private String username;
private String password;
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
properties文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/login
jdbc.username=root
jdbc.password=123456
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 ignore-unresolvable="true" location="classpath:*.properties"/>
<bean id="ossUpload" class="day_19.com.cmeizu_01.JDBC">
<property name="driver" value="${driver}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="url" value="${url}"/>
</bean>
</beans>
在controller或service中用@Autoweired注入时要注意写法
@Autoweired(required=false)
private JDBC jdbc;
这样就可以完成了
网友评论