Reputation: 1
First of all, I understand there is the ApplicationProperties created by JHipster to use. But for testing purpose, I created the following:
Class TestProperties
package com.xxx.yyy.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "test", ignoreUnknownFields = false)
public class TestProperties {
private String dummyValue;
public String getDummyValue() {
return dummyValue;
}
public void setDummyValue(String dummyValue) {
this.dummyValue = dummyValue;
}
}
Class TestService
package com.xxx.yyy.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.xxx.yyy.config.TestProperties;
@Service
public class TestService {
private final Logger log = LoggerFactory.getLogger(TestService.class);
private final TestProperties testProperties;
public TestService(TestProperties testProperties) {
this.testProperties = testProperties;
}
public void test() {
log.debug("show have val" + testProperties.getDummyValue());
}
}
In application-dev.yml, I have
# application:
test:
dummy-value: Test Value
However when I run mvn, I get the following error, anybody know what is wrong?
DEBUG 10760 --- [ restartedMain] c.ehcache.core.Ehcache-usersByLogin : Close successful.
ERROR 10760 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.xxx.yyy.service.TestService required a bean of type 'com.xxx.yyy.config.TestProperties' that could not be found.
Action:
Consider defining a bean of type 'com.xxx.yyy.config.TestProperties' in your configuration.
I understand that since Spring 4.3, there is no need for the annotation for @Autowired and if any arguments of the constructor are Spring beans. I tested with JHipsterProperties in my service and it works.
public TestService(JHipsterProperties jHipsterProperties ) {
this.jHipsterProperties = jHipsterProperties;
}
Anybody knows why my new properties does not work?
Upvotes: 0
Views: 2413
Reputation: 6352
For the properties to be loaded, you need to add it to @EnableConfigurationProperties
in your application's main class. For the jhipster-sample-app, it would be added here.
Upvotes: 4