我在使用@PropertySource
一些麻烦,使用env.getProperty(..)
当我得到一个NPE 没有任何人有任何见解春天 - @PropertySource,得到一个NPE;(
我的环境:
- ? JDK/JRE:1.6.0_06
- 操作系统:Linux Mint的13
- 春:4.1.6.RELEASE
堆栈跟踪:
Exception in thread "main" java.lang.NullPointerException
at com.mycompany.app.ExpressiveConfig.main(ExpressiveConfig.java:33)
属性文件/src/main/resources/com/mycompany/app/app.properties
:
disc.title=Sgt.PeppersLonelyHeartsClubBand
disc.artist=TheBeatles
我的配置类:
package com.mycompany.app;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
@PropertySource("classpath:/com/mycompany/app/app.properties")
public class ExpressiveConfig {
@Autowired
Environment env;
@Bean
public App get() {
return new App("hello", env.getProperty("disc.artist"));
}
public static void main(String args[]) {
App a = new ExpressiveConfig().get();
}
}
模型类:
package com.mycompany.app;
import org.springframework.stereotype.Component;
public class App {
private final String title;
private final String artist;
public App(String title, String artist) {
this.title = title;
this.artist = artist;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
}
Faile d尝试:
我曾尝试使用该文件与
@PropertySource
注释玩耍,例如:前缀和绝对路径属性文件。 在类路径之前删除反斜杠eg/@ PropertySource(“classpath:com/mycompany/app/app.properties”)。 将属性文件放在不同的位置。我也试过使用
@PropertySources
也包含@PropertySource
注释。我说:
@Bean 公共静态PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){ 返回新PropertySourcesPlaceholderConfigurer(); }
任何帮助/建议非常感谢!
你是如何初始化ExpressiveConfig的? – shruti1810
您测试的Hava: '@PropertySource(“classpath:app.properties”)'? –