Java Reference
In-Depth Information
In Spring, @Repository is a stereotype annotation. By annotating it, a component class can be auto-
detected through component scanning. You can assign a component name in this annotation and have
the session factory auto-wired by the Spring IoC container with @Autowired .
package com.apress.springenterpriserecipes.course.hibernate;
...
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("courseDao")
public class HibernateCourseDao implements CourseDao {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
...
}
Then you can simply enable the <context:component-scan> element and delete the original
HibernateCourseDao bean declaration.
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xmlns:context=" http://www.springframework.org/schema/context"
xmlns:tx=" http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan
base-package="com.apress.springenterpriserecipes.course.hibernate" />
...
</beans>
3-11. Persisting Objects with JPA's Context Injection
Problem
In a Java EE environment, a Java EE container can manage entity managers for you and inject them
into your EJB components directly. An EJB component can simply perform persistence operations on
an injected entity manager without caring much about the entity manager creation and transaction
management.
 
Search WWH ::




Custom Search