问题
This is my configuration for Spring Bean:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
And then I use it in DAO layer like this:
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
At last it's where my question come from:
public T getById(int id) {
Session session = sessionFactory.getCurrentSession();
return (T) session.get(clz, id);
}
In all, my question is that:
In class "org.springframework.orm.hibernate4.LocalSessionFactoryBean"
,there is no implementation for SessionFactory's
method -- "getCurrentSessio
".
I traced LocalSessionFactoryBean's
hierachy system through, but I didn't find the implementation for the method "getCurrentSession
".
I'm expecting your answers, thanks very much!
回答1:
A factory bean, as its name indicates, acts as a factory. Its role is to create a bean of another type. Spring injects the product of this factory bean, i.e. the object that the factory bean has created. A LocalSessionFactoryBean
produces a SessionFactory
.
For more information, read the documentation.
回答2:
You don't see
LocalSessionFactoryBean's getCurrentSession()
Its bacause from api docs here
protected abstract SessionFactory buildSessionFactory() throws Exception
Build the underlying Hibernate SessionFactory. Returns: the raw SessionFactory (potentially to be wrapped with a transaction-aware proxy before it is exposed to the application) and see this
This factory bean will by default expose a transaction-aware SessionFactory proxy, letting data access code work with the plain Hibernate SessionFactory and its getCurrentSession() method, while still being able to participate in current Spring-managed transactions:
来源:https://stackoverflow.com/questions/22283376/why-doesnt-localsessionfactorybean-implement-getcurrentsession-meanwhile-the-i