2

PropertyPlaceholderConfigurerをプログラムでロードし、XML構成ファイルのプレースホルダーを使用しようとしているカスタムApplicationContextクラスがあります。

これまでに3つの異なるアプローチを試しましたが、毎回次のようなエラーが発生します。

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> 

また、Spring の構成ディレクトリを maven でフィルタリングされたリソースに含める必要があります。

<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、それをアプリケーション コンテキストに として配置するだけbeanで済みます。残りはすべて実行されます。

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