I have acquired a Java Springboot project and am performing some modifications on it. Mind you, this is my first Spring boot interaction so please take that into account if you choose to answer.
In my code, I need to connect to RabbitMQ. To do that, I'd have to add some application properties inside application.properties
and application-dev.properties
.
Here's what those files contain
#RabbitMQ
rabbitmq.host=localhost
rabbitmq.password=guest
rabbitmq.username=guest
rabbitmq.port=15672
rabbitmq.queue=resonance
#DataSource
spring.datasource.url=jdbc:mysql://localhost:3306/other_options
The spring.datasource.url
property was there before I received the code. The rabbitmq.*
properties are what I added to my code.
After building the project, I could access the target/classes/
directory and would see the above props correctly placed in the target props file.
To read these props, I have created a file called RabbitMQConfigProps.java
which contains the following:
@Configuration
@ConfigurationProperties()
public class RabbitMQConfigProps {
@Value("${rabbitmq.host}")
private String host;
// = "localhost";
@Value("${rabbitmq.username}")
private String username;
}
}
Running the code, I receive the following error: Error creating bean with name 'rabbitMQConfigProps': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'rabbitmq.host' in value "${rabbitmq.host}"
I figured that might be an issue with the props I added, so I replaced @Value("${rabbitmq.host}")
with @Value("${spring.datasource.url}")
to test it out, and the error for that value disappeared, and a new error appeared for the following field Error creating bean with name 'rabbitMQConfigProps': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'rabbitmq.username' in value "${rabbitmq.username}"
To conclude:
- Props files are found correctly in
target/
directory - Old props work just fine in my piece of code
- New props I'm adding are not being picked up for some reason
- I've: 4.1. Cleaned cache and restarted 4.2. Ran "Fix IDE" for IntelliJ 4.3. Turned the planet off and on again
Nothing seems to work, any help would be amazing.
CodePudding user response:
Try to write:
@ConfigurationProperties(prefix = "rabbitmq")
public class RabbitMQConfigProps {
private String host;
private String username;
}
}
And add:
@EnableConfigurationProperties(RabbitMQConfigProps.class)
@SpringBootApplication
public class MainClass{
public static void main(String[] args) {
SpringApplication.run(MainClass.class, args);
}
}