2

I have a very small Spring Boot program that attempts to add its own properties in the relevant class

package com.findology.testboot2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Configuration
@PropertySource({ /*"classpath:/application.properties",*/ "classpath:/countrycounts.properties" })
@Component
public class CountryCountsWebSetup implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

@Value("${countrycounts.server.port}")
int port;

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(port);
    }

}

and a properties file

countrycounts.kafka.bootstrapServers= kafka1.data.int.dc1.ad.net:9092,kafka2.data.int.dc1.ad.net:9092,kafka3.data.int.dc1.ad.net:9092,kafka4.data.int.dc1.ad.net:9092,kafka5.data.int.dc1.ad.net:9092

countrycounts.kafka.topic= COUNTRY_COUNTS_WINDOWS

countrycounts.kafka.listener.id= country_counts_window

countrycounts.server.port= 8989

The problem I'm having is that while running with debug turned on Spring Boot appears to find and load the countrycounts.properties file

2018-08-29 17:14:10.697 [main; MutablePropertySources] DEBUG -- Adding PropertySource 'class path resource [countrycounts.properties]' with search precedence immediately higher than 'class path resource [f2-traffic.properties]'

but cannot complete startup because it claims the countrycounts.server.port property is missing.

2018-08-29 17:14:12.705 [main; AbstractApplicationContext] WARN -- Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JettyServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedJetty.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'countryCountsWebSetup': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'countrycounts.server.port' in value "${countrycounts.server.port}"

If I set the value on the java command line using -D, it works fine but still implies that the property was not read from the file.

Any ideas why the property file gets loaded but the values within are not used?

UPDATE

As part of the maven dependencies for this program, a large (and complicated) jar file is included which uses Spring 4.1 and includes a propertyPlaceHolder and it also defines location. Could that be destroying the path to the properties file I need thus causing those properties not to be seen?

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Allan Wax
  • 141
  • 7
  • Why are you not leveraging spring-boot's property configuration? why do you have two properties files and mapping them manually? – Amit Phaltankar Aug 30 '18 at 00:58
  • most probably the issue is how you are mapping the countrycounts.properties , also does the f2-traffic.properties contain the same value as well ? ? check this [answer](https://stackoverflow.com/questions/39047333/spring-boot-value-properties) as well – AntJavaDev Aug 30 '18 at 01:27
  • @AmitPhaltankar I tried putting the property values in `application.properties` and let them be loaded automatically but that did not work either – Allan Wax Aug 30 '18 at 16:43
  • @AntJavaDev The properties I'm using only exist in the `countrycounts.properties` file so there are no overrides. Could you explain more about what you mean by _mapping the countrycounts.properties_ Regarding the answer you referred, I tried all of those and none of them seemed to work. – Allan Wax Aug 30 '18 at 17:38
  • With mapping the properties i meant on how you are referring to the file and whether if that is available on your classpath during runtime. I am guessing that you are creating a runnable jar(Spring boot app) so if you could double check that all the files are properly packaged inside the jar. – AntJavaDev Aug 30 '18 at 22:59

1 Answers1

0

I resolved this issue but not in a very clean way. I don't like the solution but it seems to work.

To resolve it I extracted the embedded properties defining xml file from the jar file dependency. I then removed the placeholderProperties from it and created a new file with a new placeholderProperties that pointed to my directories. That was followed by adding a ImportSource to my new xml file and making sure the class which used the large jar file explicitly included the copied and modified property/xml files it needed.

At that point, my properties were seen and everything ran fine but I think this is a big kludge and I would still like to find a real Spring Boot way to do this cleanly.

Allan Wax
  • 141
  • 7