从JNDI为SpringBoot设置Spring @Profile

时间:2017-01-17 09:08:48

标签: spring tomcat spring-boot jndi

我将SpringBoot应用程序部署到Tomcat 8.5容器。 与此处描述的类似:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html我修改了SpringBootApplication,因此它可以作为战争进行部署。

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
}

与此问题类似Activating Spring @Profile Using JNDI我希望应用程序使用JNDI条目选择活动的配置文件。

我添加的EnvironmentApplicationContextInitializer与接受的答案中显示的相同。

但是:我不使用任何web.xml。因此,不会选择和使用该配置文件。

如何使用EnvironmentApplicationContextInitializer生成SpringBootApplication?

1 个答案:

答案 0 :(得分:0)

感谢M Deinum's评论,我找到了一个解决方案:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        // this would be used if run via java -jar service.war
        // SpringApplication app = new SpringApplication(Application.class);
        // CustomApplicationContextInitializer initializer = new CustomApplicationContextInitializer();
        // app.addInitializers(initializer);
        // app.run(args);
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // this will be used within an app container
        CustomApplicationContextInitializer initializer = new CustomApplicationContextInitializer();
        return builder.initializers(initializer).sources(Application.class);
    }
}

请注意builder.initializers(initializer)部分。