java - Use Spring annotations to automatically apply Hibernate Interceptor? -


I have a hibernate session in my service class. I currently do this in Beans .xml:

  & lt; Bean id = "Userdog" class = "org.springframework.aop.framework.ProxyFactoryBean" & gt; & Lt; Property Name = "Target" & gt; & Lt; Ref bean = "userDaoTarget" /> & Lt; / Property & gt; & Lt; Property Name = "Proxy Interface" & gt; & Lt; Price & gt; Com.app.dao.UserDao & lt; / Pricing & gt; & Lt; / Property & gt; & Lt; Property Name = "Interceptor Name" & gt; & Lt; List & gt; & Lt; Price & gt; HibernateInterceptor & lt; / Pricing & gt; & Lt; / List & gt; & Lt; / Property & gt; & Lt; Ability value = "Proxy" /> & Lt; / Bean & gt; ... & lt; Bean id = "Hibernate Interceptor" class = "org.springframework.orm.hibernate3.HibernateInterceptor" & gt; & Lt; Property Name = "sessionFactory" & gt; & Lt; Ref bean = "sessionFactory" /> & Lt; / Property & gt; & Lt; Beans & gt;  

(can be copied by hand, some typo may be ..)

I am using annotations on XML, I was thinking that Was there a way to use them to configure a proxy such as I have with hibernate interceptor above? If not - there is no such way that I can reduce the amount of XML (with about 7 DAOs)

OK, let's say you have

I use Annotation on XML

I'm moving forward

  package br.com.ar.aop; @ Public Square Hibernate Interceptor Device {@ etvoir private hibernate internet hibernate interceptor; / ** * I think your DAO com.app.dao package * / @ around ("exec (* com.app.dao. * (..))" public object intercept call (ProceedingJoinpointPoint Joint Point) Throwable { ProxyFactory throws ProxyFactor = new proxyfatter (joinPoint.getTarget ()); proxyFactory.addAdvice (hibernateInterceptor); square [] squareAre = new square [joinPoint.getArgs (). Length]; for (int i = 0; i & lt ; ClassArray.length; i ++) squareAre [i] = joinPoint.getArgs () [i] .getClass (); Return proxyFactory .getProxy () .getClass () .getDeclaredMethod (joinPoint.getSignature (.) GetName (), ClassArray). Invoke (Prok Sepactiator.jetprocoxy (), joinPoint.getArgs ());}}  

but keep in mind that your DAO applies some interface, it just works (e.g. For example, UserAIPL has implemented UserDown.) Spring AOP uses the JDK dynamic proxy in this case. If you do not have any interface, then you can trust your IDE Remove Interface < / P>

E declared under your XML to Rifactor aware (I Spring'm using 2.5 Akssdi Schema)

  & lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: reference = "http: // www. Springframework Org / schema / context "xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "xsi: schema location =" 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"> & Lt ;! --SessionFile settings go here - & gt; & Lt; Bean square = "org.springframework.orm.hibernate3.HibernateInterceptor" & gt; & Lt; Property Name = "sessionFactory" ref = "SessionFire" / & gt; & Lt; Beans & gt; & Lt ;! - To enable Aspecze AOP - & gt; & Lt; Aop: AspectJ-autoproxy / & gt; & Lt ;! - Your advice - & gt; & Lt; Bean class = "br.com.ar.aop.HibernateInterceptorAdvice" /> & Lt ;! - com.app.dao looks for any annotated spring bean in the package - & gt; & Lt; Reference: component-scan base-package = "com.app.dao" /> & Lt ;! - Enable @Autowired Annotation - & gt; & Lt; Reference: Annotation-config / & gt; & Lt; / Bean & gt;  

In addition to spring libraries, insert into a classpiece

  & lt; SPRING_HOME & gt; / Lib / asm & lt; SPRING_HOME & gt; / Lib / aopalliance & lt; SPRING_HOME & gt; / Lib / aspectj  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -