5

I am normally the Tomcat guy but we use Widlfly on one of our client project.

With Tomcat, I can set "per application" properties by creating a separate context for each application, just as Tomcat documentation very nicely says.

This way, my WebApp1.war can run with my.property.value=Cat and WebApp2.war can run with my.property.value=Dog at the same time.

I haven't found any similar documentation / feature with Wildfly. Could you please advice me how to set properties to applications individually, or point me to the documentation?

Thank you. :-)

Petr Dvořák
  • 750
  • 1
  • 9
  • 21
  • What types of properties are you looking for? Like system properties? – James R. Perkins Dec 09 '16 at 23:48
  • Application properties. Like these: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html I would like to set for example "spring.datasource.url", with different values for different applications. – Petr Dvořák Dec 11 '16 at 14:25
  • 1
    It looks like you'd just include an `application.properties` file in the deployment unless I'm missing something. It depends really on the application and how these properties are read where they can be set. – James R. Perkins Dec 12 '16 at 16:58
  • This will not work - I need to set them at the container level, not application level, since different deployments may have different values. Just as I linked with Tomcat. With Tomcat, I can create `$CATALINA_HOME/conf/Catalina/localhost/MyApp1.xml` file where I set the properties like so: ``. I am looking for the same thing with Wildfly. – Petr Dvořák Dec 12 '16 at 20:05
  • Looking at the Tomcat docs, "context parameters" may be the good keyword here... – Petr Dvořák Dec 12 '16 at 20:07
  • 1
    I'm just not sure what could be done here. Using the `application.properties` or `web.xml` seems like the only way to me to keep the configuration in the deployment. A container configuration is global generally speaking. – James R. Perkins Dec 12 '16 at 22:29
  • 1
    OK, I will assume this is a Wildfly limitation for now and I will try to find a workaround for it. Tomcat allows per-context properties in a single container - I can have more config files for each application. Thank you for your help anyway - at least I know I must use a workaround. – Petr Dvořák Dec 13 '16 at 16:06
  • Well I'm not sure I'd consider it a limitation of WildFly. It all depends on how your properties are read. System properties are global, so those can't be defined there. You could use a properties file in your deployment. You could use the web.xml. You could use JNDI. There are many options, but they all depend on how the properties are read by the application. – James R. Perkins Dec 13 '16 at 17:15
  • No, I don't need to care about how properties are read by my application - Spring handles this for me, the way I work with properties is for example via @Value("my.property") annotation on a class property. On Tomcat, you can set context parameters that are defined per context - on application deploy, these properties are visible to your application within a single container instance. This way, you can for example specify different Spring database connections for different apps on a single container using the same property. I linked all the docs that are related. The same works on WebSphere. – Petr Dvořák Dec 13 '16 at 17:39
  • @PetrDvořák did you get any solution for this. i am getting same problem and have spend many hours but didn't get nay solution. if you have any alternate way or solution. please post as a answer. Thanks in advance. – Awanish Kumar Jan 25 '18 at 03:00
  • @AwanishKumar Unfortunately, there seems to be no way to do this "per context" configuration. On Wildfly, you can use JNDI where applicable (this way, you can set up more data sources), or use different property / ENV variable names for different application purpose, or multiple wildfly server instances (for example, if you use Docker infrastructure, this is not a big deal)... – Petr Dvořák Jan 26 '18 at 10:04

3 Answers3

4

In Wildfly, you can create modules holding properties:

  • Under the ${JBOSS_HOME}/modules, add a directory like my/group/app1/conf/main.

  • Under the ${JBOSS_HOME}/modules/my/group/app1/conf/main, create the file module.xml with content:

    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.1" name="my.group.app1.conf">
            <resources>
                    <resource-root path="." />
                    <!-- Insert resources here -->
            </resources>
    </module>
    
  • Copy your *.properties file(s) under the ${JBOSS_HOME}/modules/my/group/app1/conf/main

  • Add as dependency <module name="my.group.app1.conf" export="true" /> in the jboss-deployment-structure.xml of the WebApp1.war

  • In a Spring XML, assuming you have in the configuration module a file named my-app.properties the properties can be loaded into the context with:

    <context:property-placeholder
            location="classpath*:*my-app.properties"
            local-override="false"
            ignore-unresolvable="false"/>
    

To have a configuration module for the WebApp2.war, just repeat the steps above but the new module must have its own unique name.

Ceddaerrix
  • 304
  • 1
  • 14
0

I think, I found a workaround for this problem. A asume, that app1.war and app2.war are different wars and not the same with different names.

Then you can use the Application initialization process at runtime to define per application a different spring.config.name. Instead of application.properties then every WAR (and of corse every jar) looks for a application specific called properties file.

/** Main Entry Point for this Application */
@SpringBootApplication(scanBasePackages = "de.mach.selfservices")
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer {

  // JAR init
  public static void main(String[] args) {
    SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
    if (!System.getProperties().containsKey("spring.config.name")) {
      builder = builder.properties("spring.config.name:app1");
    }
    builder.run(args);
  }

  // WAR init
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    if (!System.getProperties().containsKey("spring.config.name")) {
      builder = builder.properties("spring.config.name:app1");
    }    
    return builder.sources(Application.class);
  }
}

The second application has then app2 in spring.config.name. In both cases the default behaviour of SpringBoot is like expected. It looks inside and outside the WAR/JAR for app1.properties or app2.properties. So you can put two files in wildfly/standalone/configuration and can configure both WARs independently.

TRW
  • 876
  • 7
  • 23
0

For me the following approach worked. First i changed name of application properties. Second i changed the path to configuration file using Wildfly configuration path.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

     @Override
     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
       String configPath = System.getProperties().getProperty("jboss.server.config.dir") + "/";
       return builder
                    .properties("spring.config.name:my-app")
                    .properties("spring.config.location:" + configPath)
                    .sources(Application.class);
     }
     
     public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
     }
}