spring 容器
<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<array>
<value>classpath:salesman.properties</value> <!-- 如果在包下文件 需要 扫描包 src下可直接使用次方法-->
</array>
</property>
<property name="fileEncoding" value="UTF-8" />
</bean>
@Value获取文件值 的key
@Service
public class SMSScheduler {
@Value("#{prop[username]}")
public String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Value("#{prop[password]}")
public String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
salesman.properties 文件内容如下
username=administrator
password=123456
@Controller
public class UserAction {
@Autowired
private SMSScheduler e;
@RequestMapping("sadasdsadsadas")
public void find_properties(HttpServletRequest request ,HttpServletResponse response) throws IOException {
response.getWriter().println(e.getUsername()+e.getPassword());
System.out.println(e.getUsername());
}
}
OK 结束