【问题标题】:Spring XML configuration, default-lazy-init and spring properties not workingSpring XML 配置、default-lazy-init 和 spring 属性不起作用
【发布时间】:2013-11-27 18:27:45
【问题描述】:

我想根据从属性文件加载的属性来确定我的 bean 是否是惰性初始化。

我的配置文件中有很多属性都被正确解析了,除了 default-lazy-init 中的属性。

我尝试直接在 bean 上设置惰性初始化,但我仍然遇到异常。

还有其他方法吗?

config.properties:

lazyInit=true    
...

springConfiguration-datasource.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

  <bean name="tDatasource" class="bitronix.tm.resource.jdbc.PoolingDataSource" init-method="init" destroy-method="close" lazy-init="${lazyInit}">
...

例外:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
    at com.icod.solapCore.test.SpringSupport.prepareTest(SpringSupport.java:19)
    at com.icod.solapCore.test.SolapCoreJunit4TestRunner.createTest(SolapCoreJunit4TestRunner.java:50)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:251)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:248)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at com.icod.solapCore.test.SolapCoreJunit4TestRunner.run(SolapCoreJunit4TestRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 20 in XML document from class path resource [springConfiguration-datasource.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 144; cvc-datatype-valid.1.2.1: '${lazyInit}' is not a valid value for 'NMTOKEN'.

【问题讨论】:

  • 请发布您的上下文以包括 de bean 定义和属性占位符配置器。
  • 不,编辑您的问题并添加它以及 bean 定义和完整的异常日志。
  • 您可以点击问题上的编辑按钮。在此处添加详细信息并删除这些 cmets。
  • 我也遇到了同样的问题,有人解决了吗?
  • 如果你使用的是maven,那么你可以使用maven-replacer插件

标签: spring properties lazy-initialization


【解决方案1】:

由于这个问题的上下文不清楚。 不过,我还是推荐使用 @Lazy 注解,而不是在 spring bean 配置文件中进行配置。

这肯定会避免所有错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2012-11-05
    • 2018-12-09
    • 2014-04-18
    • 1970-01-01
    相关资源
    最近更新 更多