【问题标题】:How to define resource in SpringBoot application, which is equivalent to <resource-ref> in traditional web.xml?Spring Boot应用中如何定义资源,相当于传统web.xml中的<resource-ref>?
【发布时间】:2017-09-21 00:08:48
【问题描述】:

我正在做一个使用 SpringBoot 1.4.3-RELEASE 开发的项目。

根据公司内部文件,需要我们在WEB-INF/web.xml中定义一个,每个应用。示例如下:

<resource-ref>
    <res-ref-name>connectivityConfiguration</res-ref-name>
    <res-type>com.hide-my-company-name.ConnectivityConfiguration</res-type>
</resource-ref>

然后使用JNDI查找来获取某个对象。但是我没有WEB-INF/web.xml。

因此,我在本地 tomcat context.xml 中定义资源,而不是 WEB-INF/web.xml。

<Context>
    <Resource name="connectivityConfiguration" 
              type="com.hide-my-company-name.ConnectivityConfiguration" />
</Context>

它有效。但是,仅在我的本地开发环境中。 因为部署后我无法更改“context.xml”或“server.xml”。

问题: 有没有其他方法可以使用 SpringBoot 定义相同的资源?例如。通过。 Java 代码,还是通过 application.properties?

【问题讨论】:

    标签: spring-boot jndi servlet-3.0


    【解决方案1】:
    @SpringBootApplication
    public class WebApplication extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(WebApplication.class);
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(WebApplication.class, args);
        }
    
        @Bean
        public TomcatEmbeddedServletContainerFactory tomcatFactory() {
            return new TomcatEmbeddedServletContainerFactory() {
    
                @Override
                protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
                    tomcat.enableNaming();
                    return super.getTomcatEmbeddedServletContainer(tomcat);
                }
    
                @Override
                protected void postProcessContext(Context context) {
                    ContextResource resource = new ContextResource();
                    resource.setName("connectivityConfiguration");
                    resource.setType("com.hide-my-company-name.ConnectivityConfiguration");
                    context.getNamingResources().addResource(resource);
                }
            };
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-30
      • 2019-01-26
      • 2012-07-17
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      • 2015-07-17
      相关资源
      最近更新 更多