PropertyPlaceholderConfigurers loading after parameterized DataSource bean

683 views Asked by At

At the top of my application context I declare a PropertyPlaceholderConfigurer

...
<context:annotation-config/>

<context:property-placeholder location="classpath:foo.properties"/>
...

Later on I declare a datasource bean parameterized by properties from that properties file

<bean id="someDataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
    <property name="connectionCachingEnabled" value="true"/>
    <property name="URL" value="${database.url}"/>
    <property name="user" value="${database.user}"/>
    <property name="password" value="${database.password}"/>
</bean>

During deployment I notice that the datasource bean is created and a connection is attempted to be established BEFORE the PropertyPlaceholderConfigurer is initialized. That's causing my datasource parameterizations to not be populated.

Any idea why this might be happening?

Is there a specific order of creation for beans? Are certain beans always initialized before others? Is there a way to ensure that the PropertyPlaceholderConfigurer is loaded before all other beans?

2

There are 2 answers

1
Daniel Lawry On BEST ANSWER

It turns out that one of the beans I defined is a MapperScannerConfigurer for mybatis that references a sqlSessionFactory.

The MapperScannerConfigurer can initialize prior to any PropertyPlaceholderConfigurer if it's initialized with deprecated bean properties

To correct the behavior, the sqlSessionFactory has to be referenced using a different bean property.

See this related post for more details

2
Gabriel J On

You need to move the <context:property-placeholder location="classpath:foo.properties"/> ahead of <context:annotation-config/>

<context:property-placeholder location="classpath:foo.properties"/> <context:annotation-config/>