public class OpenSessionInterceptor extends Object implements MethodInterceptor, InitializingBean
MethodInterceptor
implementation that binds a new
Hibernate Session
for each method invocation, if none bound before.
This is a simple Hibernate Session scoping interceptor along the lines of
OpenSessionInViewInterceptor
, just for use with AOP setup instead of
MVC setup. It opens a new Session
with flush mode "MANUAL" since the
Session is only meant for reading, except when participating in a transaction.
OpenSessionInViewInterceptor
,
OpenSessionInViewFilter
,
HibernateTransactionManager
,
TransactionSynchronizationManager
,
SessionFactory.getCurrentSession()
Constructor and Description |
---|
OpenSessionInterceptor() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by the containing
BeanFactory after it has set all bean properties
and satisfied BeanFactoryAware , ApplicationContextAware etc. |
SessionFactory |
getSessionFactory()
Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
|
Object |
invoke(MethodInvocation invocation)
Implement this method to perform extra treatments before and
after the invocation.
|
protected Session |
openSession()
Deprecated.
as of 5.0, in favor of
openSession(SessionFactory) |
protected Session |
openSession(SessionFactory sessionFactory)
Open a Session for the given SessionFactory.
|
void |
setSessionFactory(SessionFactory sessionFactory)
Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
|
public void setSessionFactory(@Nullable SessionFactory sessionFactory)
@Nullable public SessionFactory getSessionFactory()
public void afterPropertiesSet()
InitializingBean
BeanFactory
after it has set all bean properties
and satisfied BeanFactoryAware
, ApplicationContextAware
etc.
This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
afterPropertiesSet
in interface InitializingBean
public Object invoke(MethodInvocation invocation) throws Throwable
MethodInterceptor
Joinpoint.proceed()
.invoke
in interface MethodInterceptor
invocation
- the method invocation joinpointJoinpoint.proceed()
;
might be intercepted by the interceptorThrowable
- if the interceptors or the target object
throws an exceptionprotected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException
The default implementation delegates to the SessionFactory.openSession()
method and sets the Session
's flush mode to "MANUAL".
sessionFactory
- the SessionFactory to useDataAccessResourceFailureException
- if the Session could not be createdFlushMode.MANUAL
@Deprecated @Nullable protected Session openSession() throws DataAccessResourceFailureException
openSession(SessionFactory)