无法使用@Autowired注释自动装配字段

时间:2017-09-20 05:42:31

标签: spring

我尝试从属性文件中自动装配字段。我只是将属性文件保存在类路径中。

@Autowired
public BlankDisk(@Value("${disk.title}") String title, @Value("${disk.artist}") String artist) {

        this.title = title;
        this.artist = artist;
    }

app.properties

disc.title =新歌 disc.artist = Illayarajah

我已经编写了上面的代码来自动填充字段。但我得到的是异常。

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'autowireDemo': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.springinaction.autowiring.BlankDisk com.springinaction.autowiring.AutowireDemo.blankDisk; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'disk.title' in string value "${disk.title}"

有人可以建议我为什么会遇到这个问题吗?

3 个答案:

答案 0 :(得分:1)

如果在更改拼写错误后仍然出现异常,请确保在配置类之上添加了@PropertySource("classpath:application.properties")

答案 1 :(得分:0)

你在道具中有c的光盘和在课堂上有k的磁盘......

答案 2 :(得分:0)

我遇到了类似的问题,但发现错误出在我的src/main/resources/application.properties之间的差异

src/test/resources/application.properties
我要注入的属性不在后者文件中,并且当SpringBoot尝试将测试作为构建的一部分运行时,找不到该属性。

相关问题