2

我有一个自定义 ApplicationContext 类,我试图以编程方式加载 PropertyPlaceholderConfigurer,然后在我的 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>

用法

MyApplicationContext context = new MyApplicationContext(
    new String[] { "test.xml" });
Assert.assertEquals("this is the test value", context.getBean("testId"));
4

3 回答 3

2

我很欣赏有关如何定义 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"));
于 2013-02-26T00:31:31.340 回答
0

这是使您的属性在配置文件中可访问的标准方式。

<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:您使用相同的语法${property-name},但在 maven 构建过程中更早地提供了值 - 这是 maven 配置的相关部分:

<properties>
<jdbc.url>jdbc:mysql://localhost:3306/dummyuserdb</jdbc.url>
<jdbc.user>root</jdbc.user>
<jdbc.pw>admin</jdbc.pw>
</properties> 

并且您必须在 Maven 过滤资源中包含 Spring 的配置目录:

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

或者,如果您更喜欢 java 配置:

 @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;
    }
 }

}

于 2013-02-25T20:25:03.080 回答
0

如果您只想使用自己的自定义MyPropertyPlaceholderConfigurer(假设它 extends PropertyPlaceholderConfigurer,那么您只需将其作为 abean放在您的应用程序上下文中,它将为您完成所有其余的工作:

<bean class="my.pkg.MyPropertyPlaceholderConfigurer" />
于 2013-02-25T20:30:36.943 回答