0

I have a spring boot 2.3.x application with dynamic properties and have loaded them using @PropertySource. The application is able to get the property values using @Autowired Environment getProperty() method and this works fine with Tomcat

custom_config.properties

system_abc=1
system_def=1
system_xyz=5

Code Snippet

@Component
public class SystemResolver {

    @Autowired
    private Environment env;

    public String getSystemValue(final String sysId) {
         return env.getProperty("system_" + sysId);
    }

The problem I am facing is when trying to run the application in JBoss EAP 7.x, @Autowired Environment getProperty() method returns org.jboss.as.naming.NamingContext instead of java.lang.String

Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.jboss.as.naming.NamingContext] to type [java.lang.String]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
at org.springframework.core.env.AbstractPropertyResolver.convertValueIfNecessary(AbstractPropertyResolver.java:265)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:91)
at org.springframework.core.env.PropertySoutrcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:62)
at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:535)

I tried excluding the JBoss submodule 'naming' from my standalone.xml, however it threw issues on application startup

Please share some inputs as how to overcome this behavior in JBoss EAP 7.x

Rayyan
  • 107
  • 12
  • Ehhh... What is `@Environment`? I can't seem to find any Spring Boot *annotation* named `Environment`. – Andreas Sep 20 '20 at 17:47
  • Sorry, I meant the @Autowired Environment – Rayyan Sep 20 '20 at 17:56
  • Then please **edit** and fix the question. – Andreas Sep 20 '20 at 17:58
  • Since the method is declared as [`String getProperty(String key)`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/PropertyResolver.html#getProperty-java.lang.String-), it's impossible for it to return a `NamingContext`. Please show the code and the full stacktrace of the error, since I'm assuming you get some kind of class cast error. – Andreas Sep 20 '20 at 18:00
  • Don't post code and/or stacktraces in a comment. **Edit** the question and show it there, appropriately formatted for human readability. --- And **show the code**, i.e. what property are you trying to get? – Andreas Sep 20 '20 at 18:19
  • Apologies, not a frequent user. Thx for guiding – Rayyan Sep 20 '20 at 18:21
  • Ok, for the third time: **SHOW THE CODE**. What property are you trying to get. --- *FYI:* If this is about `DataSource` lookup, and you're trying to use a JNDI lookup, Perhaps going the other way and ensure that Tomcat uses JNDI too, is the correct way to go. See [How to create JNDI context in Spring Boot with Embedded Tomcat Container](https://stackoverflow.com/q/24941829/5221149). – Andreas Sep 20 '20 at 18:27
  • this is not any JNDI lookup and I am trying to fetch a String value by constructing the key dynamically – Rayyan Sep 20 '20 at 18:42
  • Since `custom_config.properties` is a text file that is loaded by Spring Boot, it is weird that JBoss would resolve the property, unless you just happen to be looking for a property name that is also defined by JBoss. Do you know what the value of `sysId` is for that call? – Andreas Sep 20 '20 at 18:48
  • `sysId` would be one of those 3 - abc, def, xyz and it is very weird behavior, works fine with Tomcat :( – Rayyan Sep 20 '20 at 18:59

0 Answers0