我想通過.proprerties文件配置我的bean String字段。但它不會替換值鍵,意味着它會回顯「$ {value}」字符串。我下面的代碼:無法使用註釋從.properties文件中提取值
主類:
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
ValuesContainer valuesContainer = (ValuesContainer) applicationContext.getBean("container");
System.out.println(valuesContainer.getValue()); //echoes "${value}" instead of Ho-ho-ho!
}
}
應用程序上下文:
.....
<context:annotation-config />
<context:component-scan base-package="bean"/>
豆:
package bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("container")
@Scope("singleton")
@PropertySource(value = "classpath:app.properties")
public class ValuesContainer {
@Value("${value}")
private String value;
public String getValue() {
return value;
}
}
個app.properties:
value = Ho-ho-ho!
你是對的,謝謝 –