I'm trying to configure property for test, using #ConfigurationProperties.
My config-class with properties:
#Data
#Configuration
#ConfigurationProperties(prefix = "data")
public class TestFileSettings {
private String dockerMountPath;
}
Property-file "application-test.properties" contains:
data.docker_mount_path=testMountHostDirectory/
And test-class:
#ActiveProfiles("test")
#TestPropertySource(locations = "classpath:application-test.properties")
#EnableConfigurationProperties(value = {TestFileSettings.class})
#RunWith(SpringRunner.class)
#SpringBootTest()
public class PropertyAcessTest {
#Autowired
private TestFileSettings testFileSettings;
#Test()
public void testPropertyAcess() {
String getDockerMountPath = testFileSettings.getDockerMountPath();
assertEquals("testMountHostDirectory/", getDockerMountPath)
{
But I get the following error:
error: cannot find symbol
String getDockerMountPath = testFileSettings.getDockerMountPath();
^
symbol: method getDockerMountPath()
location: variable testFileSettings of type TestFileSettings
What am I doing wrong?
Related
application.yml
foo:
name: hun
FooConfigurationProperty
foo.name in application.yml is bound.
#Getter
#Setter
#Component
#ConfigurationProperties("foo")
public class FooConfigurationProperty {
private String name;
}
For quick testing, the FooConfigurationProperty bean was added using the classes property.
FooTest
#SpringBootTest(classes = FooConfigurationProperty.class)
public class FooTest {
#Autowired
FooConfigurationProperty fooConfigurationProperty;
#Test
public void fooTest() {
System.out.println(fooConfigurationProperty.getName());
}
}
The above results are output null rather than hun.
Why is null output when classes have specified to use a specific bean?
I am trying to load the api key value from the application.properties file and below are the class files. I am unable to start the application as it is not able to find the unique bean. Not sure what i am missing. Can someone please help.
This is our AppProperties.java
#Component
#PropertySource("classpath:application.properties")
#ConfigurationProperties(prefix = AppProperties.APP_PROPERTIES_PREFIX)
public class AppProperties {
public static final String APP_PROPERTIES_PREFIX = "bi";
private String accessTokenUri;
private String clientId;
private String clientSecret;
private String basicAuth;
private String apiKey;
//getters and setters
}
This is our DiagnosticProperties.java
#Component
#PropertySource("classpath:application.properties")
#ConfigurationProperties(prefix = "bi")
public class DiagnosticProperties extends AppProperties {
private String diagnosisUrl;
//getters and setters
}
This is our ObservationProperties.java
#Component
#PropertySource("classpath:application.properties")
#ConfigurationProperties(prefix = "bi")
public class ObservationProperties extends AppProperties {
private String observationUrl;
//getters and setters
}
This is our DiagnosticServiceImpl.java
#Service
public class DiagnosticServiceImpl implements DiagnosticService {
private static final Logger LOGGER =
LoggerFactory.getLogger(ObservationServiceImpl.class);
private final WebClient webClient;
private final DiagnosticProperties diagnosticProperties;
public DiagnosticServiceImpl(final WebClient webClient,final DiagnosticProperties
diagnosticProperties) {
this.webClient = webClient;
this.diagnosticProperties = diagnosticProperties;
}
#Override
public Mono<DiagnosticResponse> getPatientDiagnosticDetails(final String uri) {
return diagnosticDetails(uri);
}
You should not put any annotations on the AppProperties (that could have been an abstract class). The classes that inherit from it only need #ConfigurationProperties(prefix = "..") and #Component or they could be also enabled with #EnableConfigurationProperties from another configuration class.
When you inject - be specific about which configuration properties you want to inject - either by specifying a type - like you did in your example, or by adding #Qualifier("bean-name") to the parameter on the injection point.
Spring Boot out-of-the-box configures application.properties property source.
I need to set data from application.yml file to my config class but when I trying to do it I get an error:
TestConfig is annotated with #ConstructorBinding but it is defined as a regular bean which caused dependency injection to fail.
My application.yml file looks like the following:
test:
app:
id: app_id
My TestConfig class looks like this:
#Configuration
#ConfigurationProperties(prefix = "test.app")
#ConstructorBinding
public class TestConfig {
private final String id;
public TestConfig(String id) {
this.id = id;
}
}
I'm trying to do like this but it doesn't work for me.
Where I was wrong?
According to :
https://www.baeldung.com/configuration-properties-in-spring-boot#immutable-configurationproperties-binding
You will need to remove the #Configuration from your TestConfig.class.
Furthermore, it's important to emphasize that to use the constructor binding, we need to explicitly enable our configuration class either with #EnableConfigurationProperties or with #ConfigurationPropertiesScan.
--------- Edited -----
#ConfigurationProperties(prefix = "test.app")
#ConstructorBinding
public class TestConfig {
private final int id;
public TestConfig (int id)
this.id = id
}
public String getId() {
return id;
}
}
#SpringBootApplication
#ConfigurationPropertiesScan
public class YourApp{
public static void main(String[] args) {
SpringApplication.run(YourApp.class, args);
}
}
I've been trying to write on Elasticsearch with Multiple Indexes. Currently I have installed Spring v5.2.3.RELEASE and Spring Boot v2.2.4.RELEASE.
I found several solutions that allow to use multiple indexes using Spring's SPEL technology but I can't make it work.
I currently have these files:
ElasticDBDbConfig.java
package edu.unifi.disit.datamanager.config;
#Configuration
#EnableTransactionManagement
#EnableElasticsearchRepositories(basePackages = { "edu.unifi.disit.datamanager.datamodel.elasticdb"})
public class ElasticDBDbConfig {
#Value("${elasticsearch.protocol}")
private String esProtocol;
#Value("${elasticsearch.host}")
private String esHost;
#Value("${elasticsearch.port}")
private int esPort;
#Value("${elasticsearch.clustername}")
private String esClusterName;
#Bean(destroyMethod = "close")
public RestHighLevelClient elasticsearchClient() {
return new RestHighLevelClient(RestClient.builder(new HttpHost(esHost,esPort,esProtocol)));
}
#Bean
public ElasticsearchRestTemplate elasticsearchTemplate() {
ElasticsearchRestTemplate elasticsearchTemplate = new
ElasticsearchRestTemplate(elasticsearchClient());
elasticsearchTemplate.putMapping(KPIElasticValue.class);
return elasticsearchTemplate;
}
ConfigIndexBean.java
package edu.unifi.disit.datamanager.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
#Configuration
#Component("configIndexBean")
public class ConfigIndexBean {
#Value("${elasticsearch.dummyIndex}")
private String indexName;
public String getIndexName() {
return indexName;
}
public void setIndexName(String indexName) {
this.indexName = indexName;
}
}
KPIElasticValue.java
package edu.unifi.disit.datamanager.datamodel.elasticdb;
#JsonInclude(JsonInclude.Include.NON_NULL)
#Entity
#JsonSerialize(using = KPIElasticValueSerializer.class)
#JsonDeserialize(using = KPIElasticValueDeserializer.class)
#ComponentScan(basePackages = "edu.unifi.disit.datamanager.config")
#Document(type = "_doc", indexName = "#{#configIndexBean.getIndexName()}")
public class KPIElasticValue {
Do you have any idea?
I also try
#Document(type = "_doc", indexName = "#{configIndexBean.getIndexName()}")
#Document(type = "_doc", indexName = "#{configIndexBean.indexName}")
The exception is:
EL1057E: No bean resolver registered in the context to resolve access to bean 'configIndexBean'
I solved like this:
ConfigIndexBean.java
package edu.unifi.disit.datamanager.config;
public class ConfigIndexBean {
private static String indexName = "dummy";
public static final String getIndexName() {
return indexName;
}
public static void setIndexName(String indexName) {
ConfigIndexBean.indexName = indexName;
}
}
and the annotation on KPIElasticValue.java:
#Document(type = "_doc",indexName = "#
{T(edu.unifi.disit.datamanager.config.ConfigIndexBean).getIndexName()}")
In the services, before call the repository I change the index like this:
ConfigIndexBean.setIndexName("newindex");
I have a ConfigurationProperties class and want to test it using junit. But the object is always null. What might be missing in the following code?
#EnableAutoConfiguration
#ComponentScan
#EnableConfigurationProperties(MyProperties.class)
public class AppConfig {
}
#Service
public class MyService {
#Autowired
private MyProperties props;
public void run() {
props.getName();
}
}
#Component
#ConfigurationProperties(prefix = "my")
public class MyProperties {
private String name;
//getter,setter
}
application.properties:
my.name=test
test:
#Configuration
#ComponentScan(basePackageClasses = {MyService.class, MyProperties.class},
includeFilters = #ComponentScan.Filter(value = {MyService.class, MyProperties.class},
type = FilterType.ASSIGNABLE_TYPE),
lazyInit = true
)
#PropertySources(
#PropertySource("application.properties")
)
class AppTest {
#Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
}
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = ApplicationConfigTest.class)
public class MyTest extends AbstractJUnit4SpringContextTests {
#Autowired
private MyService service;
#Test
public void testService() {
service.run();
}
}
The following will load it for you:
#ContextConfiguration(classes = Application.class, initializers = ConfigFileApplicationContextInitializer.class)