2

I'm currently learning EclipseLink and Spring 3.0 MVC:

  • I wrote a simple standalone application using EclipseLink : it uses a META-INF/persistence.xml file and it reads and writes data from a mysql database.

  • I also wrote a simple 'HelloWorld' application using Spring3 MVC under apache tomcat. It is based on the following tutorial: http://www.mkyong.com/spring-mvc/spring-3-rest-hello-world-example

now, how should I link both technologies ? As far as I understand from http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch14s06.html I need to create a LocalContainerEntityManagerFactoryBean:

 <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="someDataSource"/>
  <property name="loadTimeWeaver">
    <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
  </property>
 </bean>

but what is "someDataSource" ? should I use another library like http://commons.apache.org/dbcp/ ? then what should I do with eclipselink ?

And once the JPA will be configured, how should I access it from my spring Controller ?

Thank you

Pierre
  • 34,472
  • 31
  • 113
  • 192
  • [Here](https://github.com/paukiatwee/webapp-bootstrap) have Spring + JPA (Hibernate) example that you might find it helpful. – Pau Kiat Wee May 24 '12 at 09:34

1 Answers1

1

someDataSource would just be another bean, for example:

<bean id="someDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

Here I'm also using a property replacement for the properties, this is designated by the following bean:

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

Since context is under a different xml namespace add:

xmlns:context="http://www.springframework.org/schema/context"

to your XML namespaces, and

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

to your schemaLocation string within the bean xml document.

classpath:properties/runtime.properties

This property file would be found at the following location in a standard Maven setup:

src/main/resources/properties/runtime.properties

Within this runtime.properties file you'd need to define values for each of the properties that have placeholders (i.e. jdbc.driverClassName, jdbc.url, etc.). An example of this would be:

jdbc.driverClassName=com.mysql.jdbc.Driver

If you're using MySQL for your database.

Generally speaking, you might want a service layer between your controller and repository (DAO). The reason being is controller should concentrate only on handling requests, and responding. Any business logic should be done in a different layer, through business objects, etc. Service layer acts as an intermediary between web layer, and repository layer. This not only separates concerns, but also makes things infinitely more unit testable, a huge part of any Spring application (at least it should be!).

As to how to wire things up, try inserting this bean into your Context as well:

<context:component-scan base-package="xx.yy..." />

Where xx and yy are your package names, this will tell Spring to scan your packages for "components" or @Controller objects, @Service, and @Repository (there are a few other ones but those are the main things to know about). With this information provided to Spring, you can @Autowire beans (dependency inject) into other beans.

For example:

@Service
public class SomeServiceImpl implements SomeService
{
  private SomeRepository someRepository;

  @Autowired
  public void setSomeRepository(SomeRepository someRepository)
  {
    this.someRepository = someRepository;
  }
  ...
}

This will inject the repository into the service allowing you to access that repositories methods.

Another design tip, always program for interfaces in Spring. This is especially true when it comes to repositories due to the fact that early in development, you might want to implement a Hibernate repository, but down the line DBAs might scoff at that, and require a JDBC repository using straight SQL. This way, you just need to implement the Repository using a JDBC approach rather than Hibernate, inject the JDBC repository instead of Hibernate, and you're done.

Hope this gets you started!

dardo
  • 4,870
  • 4
  • 35
  • 52