How can you reuse properties in "application.conf" from Spring's "application-context.xml" considering the double quotes e.g. with colons in the database url ?

884 views
Skip to first unread message

jsamot

unread,
May 16, 2012, 9:42:51 AM5/16/12
to play-framework
What is the best solution for avoiding duplication of the database
connection string/url in "application.conf" if you want to use JPA/
Hibernate in Spring's configuration file "application-context.xml" ?
(and please do stick to the question, and avoid off-topic question
such as "why do you want to use JPA")

I mean, the problem seem to be that if you define the property with
double quotes, then these double quotes become included when Spring
are referencing the properties with PropertyPlaceholderConfigurer.

Here is an example, illustrating the configuration I mean:
Some of the content in file "[PLAY_PROJECT_HOME]/conf/
application.conf:
db.default.url="jdbc:jtds:sqlserver://URL_TO_SQL_SERVER:PORT/
DATABASE_NAME;instance=NAME_OF_SQL_INSTANCE"
...

Some of the content in file "[PLAY_PROJECT_HOME]/conf/application-
context.xml (Spring configuration file):
...
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>application.conf</value>
</property>
</bean>
<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="net.sourceforge.jtds.jdbc.Driver"
p:url="${db.default.url}"
p:username="${db.default.user}"
p:password="${db.default.password}"
/>

If not using the double quotes, then I get this problem:
Expecting end of input or a comma, got ':' (if you intended ':' to be
part of the value for 'db.default.url', try enclosing the value in
double quotes, or you may be able to rename the file .properties
rather than .conf)
(
And indeed, according to the following discussion, the database url
need the double quotes:
http://groups.google.com/group/play-framework/msg/5011e0d8f88fe762
)

However, if I am using the double quotes, then it does not work with
Spring configuration.
I think the problem is that those will effectively become substituted
into the spring configuration like this:
The following part of the Spring config file:
p:url="${db.default.url}"
will becomes
p:url=""jdbc:jtds:sqlserver://URL_TO_SQL_SERVER:PORT/
DATABASE_NAME;instance=NAME_OF_SQL_INSTANCE""
(or kind of like this:
p:url="\"jdbc:jtds:sqlserver://URL_TO_SQL_SERVER:PORT/
DATABASE_NAME;instance=NAME_OF_SQL_INSTANCE\""
)

So, what would the best solution be for being able to reuse the
database url without duplication ?

Guillaume Bort

unread,
May 16, 2012, 10:31:23 AM5/16/12
to play-fr...@googlegroups.com
I don't know how configuration values are injected in the Spring
context (probably by the Spring plugin you are using?), but the bug is
there. It should not copy the double quotes.
> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>



--
Guillaume Bort

Eric Jain

unread,
May 16, 2012, 1:37:11 PM5/16/12
to play-framework
On May 16, 6:42 am, jsamot <samo...@gmail.com> wrote:
> What is the best solution for avoiding duplication of the database
> connection string/url in "application.conf" if you want to use JPA/
> Hibernate in Spring's configuration file "application-context.xml" ?
> (and please do stick to the question, and avoid off-topic question
> such as "why do you want to use JPA")
>
> I mean, the problem seem to be that if you define the property with
> double quotes, then these double quotes become included when Spring
> are referencing the properties with PropertyPlaceholderConfigurer.

Spring's PropertyPlaceholderConfigurer reads properties files, but
application.conf is not a Java properties file [1] (properties files
don't have a notion of quoted strings). It might be possible to write
a custom PropertyPlaceholderConfigurer that reads properties from
application.conf with `Play.application().configuration()`.


[1] https://github.com/typesafehub/config/blob/master/HOCON.md

Havoc Pennington

unread,
May 23, 2012, 12:10:55 PM5/23/12
to play-fr...@googlegroups.com
It should also be possible to include a foo.properties file from inside application.conf, if you want to have some settings in a properties file that's shared between Play and Spring.

Havoc
Reply all
Reply to author
Forward
0 new messages