加载properties文件

/ Spring / 没有评论 / 379浏览

我们在实际的项目开发中,常常需要配置很多系统参数,来满足系统的需求。例如数据库的连接、服务接口的调用地址等。为了我们可以很方便的随时修改相关参数,通常的做法就是将这些可变的参数放到配置文件中,也就是properties文件,然后在程序启动时在加载这些参数。实现这种需求的技术有很多,spring为了更方便的让我们可以访问properties文件,于是提供了很多种的方式来供我们选择。下面我们将学习一下在spring中怎么加载properties文件。

public class User {

private String username;
private String password;
private String nickname;

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;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
app.username=admin
app.password=jilinwula
app.nickname=吉林乌拉
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer" p:location="classpath:app.properties" p:fileEncoding="utf-8"/>
<bean id="user" class="com.jilinwula.spring.user.User" p:username="${app.username}" p:password="${app.password}" p:nickname="${app.nickname}"/>
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
}
User{username='admin', password='jilinwula', nickname='吉林乌拉'}
<context:property-placeholder location="classpath:app.properties" file-encoding="utf-8"/>
<bean id="user" class="com.jilinwula.spring.user.User" p:username="${app.username}" p:password="${app.password}" p:nickname="${app.nickname}"/>
User{username='admin', password='jilinwula', nickname='吉林乌拉'}
@Component
public class User {

@Value("${app.username}")
private String username;

@Value("${app.password}")
private String password;

@Value("${app.nickname}")
private String nickname;

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;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
<context:property-placeholder location="classpath:app.properties" file-encoding="utf-8"/>
<context:component-scan base-package="com.jilinwula.spring.user"/>
User{username='admin', password='jilinwula', nickname='吉林乌拉'}
<util:properties id="properties" location="classpath:app.properties" />
<context:component-scan base-package="com.jilinwula.spring.user"/>
@Component
public class User {

@Value("#{properties['app.username']}")
private String username;

@Value("#{properties['app.password']}")
private String password;

@Value("#{properties['app.nickname']}")
private String nickname;

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;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
User{username='admin', password='jilinwula', nickname='吉林乌拉'}
@Component
public class User {

@Value("#{properties['app.username']}")
private String username;

@Value("#{properties['app.password']}")
private String password;

@Value("#{properties['app.nickname']}")
private String nickname;

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;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:app.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>
<context:component-scan base-package="com.jilinwula.spring.user"/>
User{username='admin', password='jilinwula', nickname='吉林乌拉'}