Java Reference
In-Depth Information
<bean id="courseDao"
class="com.apress.springenterpriserecipes.course.jpa.JpaCourseDao">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
Now you can test this JpaCourseDao instance with the Main class by retrieving it from the Spring IoC
container.
package com.apress.springenterpriserecipes.course;
...
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(" beans-jpa.xml");
CourseDao courseDao = (CourseDao) context.getBean("courseDao");
...
}
}
In a Java EE environment, you can look up an entity manager factory from a Java EE container with
JNDI. In Spring, you can perform a JNDI lookup by using the <jee:jndi-lookup> element.
<jee:jndi-lookup id="entityManagerFactory" jndi-name="jpa/coursePU" />
LocalEntityManagerFactoryBean creates an entity manager factory by loading the JPA configuration
file (i.e., persistence.xml ). Spring supports a more flexible way to create an entity manager factory by
another factory bean, LocalContainerEntityManagerFactoryBean . It allows you to override some of the
configurations in the JPA configuration file, such as the data source and database dialect. So, you can
take advantage of Spring's data access facilities to configure the entity manager factory.
<beans ...>
...
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="org.apache.derby.jdbc.ClientDriver" />
<property name="url"
value="jdbc: derby://localhost:1527/course; create=true" />
<property name="username" value="app" />
<property name="password" value="app" />
</bean>
Search WWH ::




Custom Search