Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring PropertyPlaceHolderConfigure未以编程方式加载_Spring_Spring 3 - Fatal编程技术网

Spring PropertyPlaceHolderConfigure未以编程方式加载

Spring PropertyPlaceHolderConfigure未以编程方式加载,spring,spring-3,Spring,Spring 3,我有一个自定义的ApplicationContext类,在该类中,我试图以编程方式加载PropertyPlaceHolderConfiger,然后使用XML配置文件中的占位符 到目前为止,我已经尝试了三种不同的方法,每次都会出现如下错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '${domain}.testId' is defined 我做错了什么 上下文类 public c

我有一个自定义的ApplicationContext类,在该类中,我试图以编程方式加载PropertyPlaceHolderConfiger,然后使用XML配置文件中的占位符

到目前为止,我已经尝试了三种不同的方法,每次都会出现如下错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '${domain}.testId' is defined
我做错了什么

上下文类

public class MyApplicationContext extends GenericApplicationContext {
    public MyApplicationContext (String... locations) throws IOException {

        // method one
        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this);
        scanner.scan("com.my.package");

        // method two
        new MyPropertyPlaceholderConfigurer().postProcessBeanFactory(getBeanFactory());

        // method three
        getBeanFactory().registerSingleton("propertyPlaceholderConfigurer", new MyPropertyPlaceholderConfigurer());

        // load XML config files
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(this);
        for (String location : locations) {
            xmlReader.loadBeanDefinitions(location);
        }
    }
}
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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="test.testId" class="java.lang.String">
        <constructor-arg value="this is the test value" />
    </bean>

    <bean id="prod.testId" class="java.lang.String">
        <constructor-arg value="this is the prod value" />
    </bean>

    <alias name="${domain}.testId" alias="testId" />

</beans>

这是在配置文件中访问属性的标准方法

<util:list id="locations">
    <value>classpath:appconfig.properties</value>
    <value>classpath:jdbc.properties</value>
</util:list>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:ignoreResourceNotFound="true"
      p:locations-ref="locations" />

<!-- bean that uses properties -->
<bean id="dataSource"
      class="com.mchange.v2.c3p0.ComboPooledDataSource"
      p:driverClass="${jdbc.driver}"
      p:jdbcUrl="${jdbc.url}"
      p:user="${jdbc.user}"
      p:password="${jdbc.pw}"
      p:initialPoolSize="5"
      p:minPoolSize="5"
      p:maxPoolSize="50"
      p:idleConnectionTestPeriod="5" />
您必须在maven筛选资源中包含spring的配置目录:

<build>
    <resources>
    <resource>
        <directory>${basedir}/src/main/webapp/WEB-INF/spring-config</directory>
        <filtering>true</filtering>
        </resource>
</resources>

}

如果您只想使用自己的自定义
MyPropertyPlaceHolderConfigure
(假设它扩展了
PropertyPlaceHolderConfigure
,那么您所需要做的就是将其作为
bean
放在您的应用程序上下文中,它将为您完成所有剩余的工作:

<bean class="my.pkg.MyPropertyPlaceholderConfigurer" />

我很欣赏关于如何定义bean的答案,但事实证明问题要简单得多。我加载bean时很好,但加载所有bean后没有正确初始化上下文。只需调用refresh()即可

MyApplicationContext context = new MyApplicationContext(
    new String[] { "test.xml" });

context.refresh();   // this runs BeanFactoryPostProcessors like 
                     // MyPropertyPlaceholderConfigurer, among other things

Assert.assertEquals("this is the test value", context.getBean("testId"));

为什么您需要一个自定义上下文来包含
属性PlaceHolderConfigure
甚至您自己的自定义上下文?我的上下文类还进行包扫描和日志初始化。我只是没有在示例代码中包含这些额外的内容。我对Spring很陌生,但它似乎是一个包含所有这些内容的合理位置。是吗ng?那么这需要配置文件吗?不能用代码完成?您已经有了配置XML(
test.XML
)为什么使用配置XML比将bean作为组件扫描进来更好或不同?我的系统比问题中显示的三个文件要复杂一些,我希望找到一个比将其添加到主XML或创建第二个更好的解决方案。在我看来,创建自己的应用程序上下文是非常困难的这不是一个好主意,因为有不同类型的上下文(例如
XmlWebApplicationContext
ClassPathXmlApplicationContext
),所以您必须对它们进行子类化。这是合理的,尽管对于我的应用程序,我可以选择一个基类并坚持使用它(在我的例子中是GenericaApplicationContext)我不是在建一个图书馆,但是知道最好的方法不是子类是很好的。我会考虑尽快修好。
 @Configuration
 @PropertySource(value = "config/jdbc.properties")
 public class BaseWebConfig {

    @Autowired Environment env;

    @Bean
    public MyBean myBean() {
       MyBean myBean = new MyBean();
       String propertyValue = env.getProperty("my-property-name");
       // do something with myBean and propertyValue
       return myBean;
    }
 }
<bean class="my.pkg.MyPropertyPlaceholderConfigurer" />
MyApplicationContext context = new MyApplicationContext(
    new String[] { "test.xml" });

context.refresh();   // this runs BeanFactoryPostProcessors like 
                     // MyPropertyPlaceholderConfigurer, among other things

Assert.assertEquals("this is the test value", context.getBean("testId"));