【问题标题】:spring boot - How to load context configuration filespring boot - 如何加载上下文配置文件
【发布时间】:2018-10-28 08:15:47
【问题描述】:

我正在尝试将 spring mvc 应用程序转换为 spring boot。我曾经在tomcat中部署这个应用程序并进行测试。现在使用 spring boot 我正在尝试做同样的事情,但我面临加载 xml 文件配置的问题。

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Data sources -->
    <Environment name="/source/schema" value="${schema}" type="java.lang.String" />

    <Resource auth="Container" driverClass="org.postgresql.Driver" 
            factory="org.apache.naming.factory.BeanFactory" 
            idleConnectionTestPeriod="30" jdbcUrl="${url}" 
            maxAdministrativeTaskTime="0" maxConnectionAge="30" maxIdleTime="9" maxPoolSize="3" minPoolSize="2" 
            name="/source/DataSource" password="${password}" 
            preferredTestQuery="select 1" testConnectionOnCheckout="true" type="com.mchange.v2.c3p0.ComboPooledDataSource" user="${user}"/>
</Context>

这是我正在尝试加载的配置文件。当我把

@ImportResource({"classpath:applicationContext.xml", "classpath:context.xml"})

我能够从 applicationcontext.xml 加载所有 bean 配置,但是在加载 context.xml 时它给出了

Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Context'.

在tomcat中部署spring boot app时应该如何加载这些条目?

【问题讨论】:

    标签: spring-mvc spring-boot tomcat8


    【解决方案1】:

    默认情况下,嵌入式 Tomcat 中禁用 JNDI。您需要调用 Tomcat.enableNaming() 来启用它。

    如果你可以通过 Java config 生活,你可以尝试在 sn-p 下面使用 java config 从 context.xml 添加 JNDI 和其他配置。

    @Bean
    public TomcatEmbeddedServletContainerFactory tomcatFactory() {
        return new TomcatEmbeddedServletContainerFactory() {
    
            @Override
            protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                    Tomcat tomcat) {
                tomcat.enableNaming();
                return super.getTomcatEmbeddedServletContainer(tomcat);
            }
        };
    }
    

    例子:

    @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("jdbc/myDataSource");
                resource.setType(DataSource.class.getName());
                resource.setProperty("driverClassName", "your.db.Driver");
                resource.setProperty("url", "jdbc:yourDb");
    
                context.getNamingResources().addResource(resource);
            }
        };
    }
    
    @Bean(destroyMethod="")
    public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
        JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiName("java:comp/env/jdbc/myDataSource");
        bean.setProxyInterface(DataSource.class);
        bean.setLookupOnStartup(false);
        bean.afterPropertiesSet();
        return (DataSource)bean.getObject();
    }
    

    查看此github link 以获取相关示例

    【讨论】:

    • 此设置适用于嵌入式 tomcat。我想创建一个战争并部署在 tomcat 服务器上。
    【解决方案2】:

    context.xml 应该进入你的战争文件中的/META-INF/ 目录。这是对Tomcat服务器的指令,不需要在Spring中配置任何东西来尝试加载它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      相关资源
      最近更新 更多