我有一个简单的RestController
:
@RestController
@PropertySource("classpath:application.properties")
public class Word2VecRestController {
private final static Logger LOGGER = Logger.getLogger(Word2VecRestController.class);
// @Resource is not working as well
@Autowired
Environment env;
// This is working for some reason
// but it's null inside the constructor
@Value("${test}")
String test;
public Word2VecRestController() {
LOGGER.info(env.getProperty("test"));
System.out.println("");
}
@GetMapping("/dl4j/getWordVector")
public ResponseEntity<List<Double[]>> getWordVector(String word) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,env
总是这样null
。我见过的地方,我可以尝试使用@Resource
,而不是@Autowired
,但没有帮助。
应用程序属性:
test=helloworld
Run Code Online (Sandbox Code Playgroud)
我试过用
@Value("${test}")
String test;
Run Code Online (Sandbox Code Playgroud)
但这里的问题是这些是null
在我需要的对象的构建过程中。
Chr*_*e L 15
字段注入是在调用构造函数后由 Spring完成的。这就是为什么Environment
在Word2VecRestController
构造函数中为 null 。如果在构造函数中需要,可以尝试构造函数注入:
@RestController
@PropertySource("classpath:application.properties")
public class Word2VecRestController {
private final static Logger LOGGER = Logger.getLogger(Word2VecRestController.class);
@Autowired
public Word2VecRestController(Environment env, @Value("${test}") String test) {
LOGGER.info(env.getProperty("test"));
System.out.println("");
}
@GetMapping("/dl4j/getWordVector")
public ResponseEntity<List<Double[]>> getWordVector(String word) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
PS:如果您使用 Spring Boot,则不需要@PropertySource("classpath:application.properties")
,这是自动为您完成的。
归档时间: |
|
查看次数: |
10921 次 |
最近记录: |