Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 932891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:44:34+00:00 2026-05-15T20:44:34+00:00

In my service class I need the hibernate session available. I currently do this

  • 0

In my service class I need the hibernate session available. I currently do this in the beans.xml:

<bean id = "userDao" class="org.springframework.aop.framework.ProxyFactoryBean">
 <property name="target">
   <ref bean="userDaoTarget" />
 </property>

 <property name="proxyInterfaces">
   <value>com.app.dao.UserDao</value>
 </property>

 <property name="interceptorNames">
   <list>
     <value>hibernateInterceptor</value>
   </list>
 </property>

 <qualifier value="proxy" />
</bean>

...

<bean id="hibernateInterceptor" 
   class="org.springframework.orm.hibernate3.HibernateInterceptor">
 <property name="sessionFactory">
   <ref bean="sessionFactory" />
 </property>
<bean>

(copied by hand, may be some typos..)

I’m moving to using annotations over XML, I was wondering if there was a way to use them to configure the proxy as I have above including the hibernate interceptor? If not – is there a way that I can reduce the amount of XML (with about 7 DAOs it makes it very cluttered)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:44:34+00:00Added an answer on May 15, 2026 at 8:44 pm

    Ok, Let’s go. You said

    I am moving to using annotations over XML

    Enable an aspect as follows

    package br.com.ar.aop;
    
    @Aspect
    public class HibernateInterceptorAdvice {
    
         @Autowired
         private HibernateInterceptor hibernateInterceptor;
    
         /**
           * I suppose your DAO's live in com.app.dao package
           */
         @Around("execution(* com.app.dao.*(..))")
         public Object interceptCall(ProceedingJoinPoint joinPoint) throws Throwable {
             ProxyFactory proxyFactory = new ProxyFactory(joinPoint.getTarget());
             proxyFactory.addAdvice(hibernateInterceptor);
    
             Class [] classArray = new Class[joinPoint.getArgs().length];
             for (int i = 0; i < classArray.length; i++)
                 classArray[i] = joinPoint.getArgs()[i].getClass();
    
             return
                 proxyFactory
                     .getProxy()
                     .getClass()
                     .getDeclaredMethod(joinPoint.getSignature().getName(), classArray)
                     .invoke(proxyFactory.getProxy(), joinPoint.getArgs());
         }
    
    }
    

    But keep in mind It just works if your DAO’s implements some interface (For instance, UserDAOImpl implements UserDAO). Spring AOP uses JDK dynamic proxy in this case. If you does not have any interface, you can rely on your IDE To refactor your code by using Extract interface

    Declare your xml as follows (Be aware i am using Spring 2.5 xsd schema)

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context-2.5.xsd 
                            http://www.springframework.org/schema/aop  
                            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <!--SessionFactory settings goes here-->
        <bean class="org.springframework.orm.hibernate3.HibernateInterceptor">
            <property name="sessionFactory" ref="sessionFactory"/>
        <bean>
        <!--To enable AspectJ AOP-->
        <aop:aspectj-autoproxy/>
        <!--Your advice-->
        <bean class="br.com.ar.aop.HibernateInterceptorAdvice"/>
        <!--Looks for any annotated Spring bean in com.app.dao package-->
        <context:component-scan base-package="com.app.dao"/>
        <!--Enables @Autowired annotation-->
        <context:annotation-config/>
    </beans>
    

    Do not forget To put in the classpath besides Spring libraries

    <SPRING_HOME>/lib/asm
    <SPRING_HOME>/lib/aopalliance
    <SPRING_HOME>/lib/aspectj
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm setting up a standalone Java service with an in-process, in-memory HSQL database. Persistence.xml
I need to create a class that gets a delegate does some calculation and
So I'm still not sure if this is on my side, the service side,
class Service < ActiveRecord::Base establish_connection( :adapter => mysql, :host => myip, :username => myusername,
I added a method to our Service Base Class to get all unexported instances
How do I register something like this in the xml configuration (i know, i
Using NHibernate I need to insert an entity into a database that has a
I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1)
I have a need to run multiple WCF services at the same time, from
I ve created a sample REST web service which writes some data in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.