我是 spring 和 spring-roo 的新手。我尝试构建一个应用程序并从属性文件中读取一些键值对。
我创建了一个 myconfig.properties 文件并将其保存到 src/main/resources/META-INF/spring/。该文件的内容是:
## My Configuration settings
myconfig.url=https://1.2.3.4/api.php
myconfig.username=user1
myconfig.password=password1
现在我在同一目录中的 appilcationContext.xml 中添加了一个 bean 配置
<bean id="MyConfig" class="com.test.client.MyClient">
<property name="url" value="${myconfig.url}" />
<property name="username" value="${myconfig.username}" />
<property name="password" value="${myconfig.password}" />
</bean>
在我的类文件中,我尝试访问这些值,但我得到一个 File not found 错误
package com.test.client;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class MyClient {
private String url;
private String username;
private String password;
public static String login()
{
// Construct the spring application context
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyClient config = (MyClient) context.getBean("MyConfig");
// Register hook to shutdown Spring gracefully
// See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-shutdown
context.registerShutdownHook();
String token = null;
final String url = config.getUrl();
final String username = config.getUsername();
final String password = config.getPassword();
....
谢谢你的帮助!