Converting from Spring to Guice, need a couple of pointers

447 views
Skip to first unread message

Adam Ruggles

unread,
Sep 28, 2008, 6:04:31 PM9/28/08
to google-guice
I'm trying to convert my simple little test web app from using Spring
2.5 to using Google Guice. I've been reading a ton of documentation
and examples, but I'm still having a couple of issues to work out
before I change a line of code. I can't seem to find a good way to
configure my datasource and entity manager using guice. Here is my
current spring config:

<code>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform"
value="org.hibernate.dialect.HSQLDialect" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
</code>

I've seen warp-persist but I don't want to have to change my DAO's to
use Provider<EntityManager> instead of just EntityManager.

How would I configure Guice to duplicate these settings?

Dhanji R. Prasanna

unread,
Sep 28, 2008, 6:13:09 PM9/28/08
to google...@googlegroups.com
You don't have to with warp-persist--just make sure you don't bind your Daos as @Singletons, then you can @Inject EntityManager directly.

Dhanji.

Adam Ruggles

unread,
Sep 28, 2008, 7:06:52 PM9/28/08
to google-guice
I still don't see a way to inject a datasource into the
entitymanager. I'd like to keep the database connection information
out of the persistence.xml.

On Sep 28, 3:13 pm, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:

Dhanji R. Prasanna

unread,
Sep 28, 2008, 7:39:41 PM9/28/08
to google...@googlegroups.com
On Mon, Sep 29, 2008 at 9:06 AM, Adam Ruggles <a.ru...@gmail.com> wrote:

I still don't see a way to inject a datasource into the
entitymanager.  I'd like to keep the database connection information
out of the persistence.xml.

If you are using Hibernate you can configure the following properties:
hibernate.connection.driver_class
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password 
hibernate.connection.pool_size

And simply bind them as follows:
bind(Properties.class).toInstance(...<an obj with the above props>...); 

warp-persist will detect the bound props and configure JPA to use the datasource specified there. You are free to store those props anywhere you like (e.g. in a resource bundle). 

Dhanji.

Adam Ruggles

unread,
Sep 28, 2008, 7:41:48 PM9/28/08
to google-guice
Thanks.

On Sep 28, 4:39 pm, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:

Robbie Vanbrabant

unread,
Sep 29, 2008, 4:41:40 AM9/29/08
to google...@googlegroups.com
On Mon, Sep 29, 2008 at 1:39 AM, Dhanji R. Prasanna <dha...@gmail.com> wrote:

And simply bind them as follows:
bind(Properties.class).toInstance(...<an obj with the above props>...); 

As you probably suspected, you forgot the JpaUnit annotation :-)
bind(Properties.class).annotatedWith(JpaUnit.class).toInstance(...);

In WP 2.0 we'll add explicit ways to configure these properties. Bindings are a little bit too magical in this case, especially when we add support for multiple WP modules per Injector.

Cheers
Robbie 
Reply all
Reply to author
Forward
0 new messages