Announcement Announcement Module
Collapse
No announcement yet.
DefaultAdvisorAutoProxyCreator & CGLIB Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • DefaultAdvisorAutoProxyCreator & CGLIB

    I have used the DefaultAdvisorAutoProxyCreator successfully to add transaction advice to beans using the AnnotationTransactionAttributeSource.

    Code:
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
    
        <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
            <property name="transactionInterceptor"><ref local="txInterceptor"/></property>
        </bean>
        <bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager"><ref local="jtaTxManager"/></property>
            <property name="transactionAttributeSource">
                <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
            </property>
        </bean>

    I then wanted to apply some security advice to the same beans and used the BeanNameAutoProxyCreator as follows:

    Code:
    <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="interceptorNames">
                <list>
                    <value>securityInterceptor</value>
                </list>
            </property>
            <property name="beanNames">
                <list>
                   <value>vehicleService</value>
                </list>
            </property>
    
            <!--Ensure the method passed to the MethodSecurityInterceptor is the proxy's target, not the proxy's caller - introduces a requirement on CGLIB -->
            <property name="proxyTargetClass" value="true"/>
        </bean>
    
    <bean id="securityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
            <property name="validateConfigAttributes"><value>true</value></property>
    
            <property name="authenticationManager">
                <ref bean="authenticationManager"/>
            </property>
            <property name="accessDecisionManager">
                <ref bean="accessDecisionManager"/>
            </property>
            <property name="objectDefinitionSource">
                <value>
                    uk.co.myCo.app.revs.vehicle.VehicleServiceImpl.loadVehicle=ROLE_ADMIN
     
                </value>
            </property>
        </bean>
    This gives the following error:
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vehicleService' defined in file [C:\dev\revs\war\WEB-INF\applicationContext-revs.xml]: Initialization of bean failed; nested exception is null
    java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy6
    	at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
    	at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    	at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    	at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    	at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    	at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
    	at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:195)
    	at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:147)
    	at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:70)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:374)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:237)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:296)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:397)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:283)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313)
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:89)
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:74)
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:65)
    	at uk.co.myCo.app.revs.tests.TestVehicleService.setUp(TestVehicleService.java:40)
    	at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:32)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
    I am assuming it has applied the first advice and created a proxy and then it tries to apply the second advice to the proxy and gets the error???

    If I comment out the DefaultAdvisorAutoProxyCreator and add "txInterceptor" as an interceptor name to the BeanNameAutoProxyCreator then it works ok and applies both 'advices' to the bean.

    What I want to achieve is to use the DefaultAdvisorAutoProxyCreator to apply transaction advice and the BeanNameAutoProxyCreator to apply security - any ideas anyone??

  • #2
    Unfortunately you did not post the declaration of the vehicleService and I don't know the usage of DefaultAdvisorAutoProxyCreator in general (does it create a proxy for EVERY declared bean??). So I don't know if my answer really fits, but ...

    Would it not be possible to refrain from DefaultAdvisorAutoProxyCreator and only use the BeanNameAutoProxyCreator and apply both interceptors in one step? The double-proxy creation has other drawbacks as well.

    Jörg

    Comment

    Working...
    X