Class OpenSessionInterceptor
- java.lang.Object
- org.springframework.orm.hibernate5.support.OpenSessionInterceptor
- All Implemented Interfaces:
Advice
,Interceptor
,MethodInterceptor
,InitializingBean
public class OpenSessionInterceptor extends Object implements MethodInterceptor, InitializingBean
Simple AOP AllianceMethodInterceptor
implementation that binds a new HibernateSession
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 newSession
with flush mode "MANUAL" since the Session is only meant for reading, except when participating in a transaction.- Since:
- 4.2
- Author:
- Juergen Hoeller
- See Also:
OpenSessionInViewInterceptor
,OpenSessionInViewFilter
,HibernateTransactionManager
,TransactionSynchronizationManager
,SessionFactory.getCurrentSession()
Constructor Summary
Constructors Constructor Description OpenSessionInterceptor()
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,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 ofopenSession(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.
Constructor Detail
OpenSessionInterceptor
public OpenSessionInterceptor()
Method Detail
setSessionFactory
public void setSessionFactory(@Nullable SessionFactory sessionFactory)
Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
getSessionFactory
@Nullable public SessionFactory getSessionFactory()
Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
afterPropertiesSet
public void afterPropertiesSet()
Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,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.
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
invoke
public Object invoke(MethodInvocation invocation) throws Throwable
Description copied from interface:MethodInterceptor
Implement this method to perform extra treatments before and after the invocation. Polite implementations would certainly like to invokeJoinpoint.proceed()
.- Specified by:
invoke
in interfaceMethodInterceptor
- Parameters:
invocation
- the method invocation joinpoint- Returns:
- the result of the call to
Joinpoint.proceed()
; might be intercepted by the interceptor - Throws:
Throwable
- if the interceptors or the target object throws an exception
openSession
protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException
Open a Session for the given SessionFactory.The default implementation delegates to the
SessionFactory.openSession()
method and sets theSession
's flush mode to "MANUAL".- Parameters:
sessionFactory
- the SessionFactory to use- Returns:
- the Session to use
- Throws:
DataAccessResourceFailureException
- if the Session could not be created- Since:
- 5.0
- See Also:
FlushMode.MANUAL
openSession
@Deprecated @Nullable protected Session openSession() throws DataAccessResourceFailureException
Deprecated.as of 5.0, in favor ofopenSession(SessionFactory)
Open a Session for the given SessionFactory.