Java-Seekar
Java-Seekar

Reputation: 1770

Could not autowire field using @Autowired annotation

I tried to autowire the fields from property file. I just keep the property file in class path.

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

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

app.properties

disc.title=New Songs
disc.artist=Illayarajah

I have written the above code to autowire the fields. But I am getting below exception.

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}"

Can anyone please suggest why I am getting this issue?

Upvotes: 0

Views: 773

Answers (2)

OneXer
OneXer

Reputation: 355

I had a similar issue, and I found the error to be in the discrepancies between my src/main/resources/application.properties

src/test/resources/application.properties
The property I was trying to inject was not in the latter file and when SpringBoot tried to run the test as part of the build, it could not find the property.

Upvotes: 0

Rakesh
Rakesh

Reputation: 466

If you still get the exception after changing the spelling mistake, please make sure if you have added @PropertySource("classpath:application.properties") on top of the configuration class

Upvotes: 1

Related Questions