Results 1 to 4 of 4

Thread: proxy for @Controller annotated class

  1. #1
    Join Date
    Jan 2008
    Posts
    5

    Default proxy for @Controller annotated class

    Hi there,

    converting Controllers of a spring mvc project from traditional style to new 2.5 annotation based controllers give me following proxy problems.

    The interesting part of the class:
    Note that the class implements no interface and has no default constructor.
    PHP Code:
    @Controller
    @RequestMapping("/login")
    @
    SessionAttributes("login")
    public class 
    LoginController {

        @
    Autowired
        
    public LoginController(IUserService userService) {
            
    _userService userService;
        }
    ... 

    The aop setup look as follow:
    PHP Code:
    <bean
            
    class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <
    property name="beanNames">
                <list>
                    <
    value>*Controller</value>
                </list>
            </
    property>
            <
    property name="interceptorNames">
                <list>
                    <
    value>permissionAdvice</value>
                </list>
            </
    property>
        </
    bean

    The controller class gets instantiated via DefaultAnnotationHandlerMapping.class.
    Loading the context results in following exception:
    PHP Code:
    org.springframework.beans.factory.BeanCreationExceptionError creating bean with name 'loginController'Initialization of bean failednested exception is org.springframework.aop.framework.AopConfigExceptionCould not generate CGLIB subclass of class [class com.project.web.controller.LoginController]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentExceptionSuperclass has no null constructors but no arguments were given
            at org
    .springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
            
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:485)
            
    at java.security.AccessController.doPrivileged(Native Method)
            
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
            
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
            
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
            
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
            
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:170)
            
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:413)
            
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:735)
            
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
            
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:332
    If i use setter- instead of contructor-injection everythings work fine.
    If i let the controller implement any interface (like Serializable) the context-loading happens without exception, but it comes to
    PHP Code:
    javax.servlet.ServletExceptionNo adapter for handler [com.project.web.controller.LoginController@ef77e9]: Does your handler implement a supported interface like Controller?
        
    at org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1086
    Does anyone have a clue whats happening here ?
    Johannes

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,626

    Default

    For CGLib proxies to work, as the error message also clearly stated, you need to have a no-arg constructor. It is a requirement for CGLib proxies.

    If you implement an interface like Serializable a JDK Dynamic proxy is created which only works with interfaces not classes. So your proxy is only an instance/castable to Serializable it isn't a Controller anymore (in the case of a @Controller, if you would implement the Controller interface it would work).
    Last edited by Marten Deinum; Jan 4th, 2008 at 01:52 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2008
    Posts
    5

    Default

    Thanks,

    that makes sense. So the best workaround seems to me to add a protected default constructor.
    PHP Code:
    @Controller
    @RequestMapping("/login")
    @
    SessionAttributes("login")
    public class 
    LoginController {

        protected 
    LoginController() {
            
    // needed for aop cglib
        
    }

        @
    Autowired
        
    public LoginController(IUserService userService) {
            
    _userService userService;
        } 
    No everything works. Proxy instantiation and IOC.
    Thanks!

  4. #4
    Join Date
    Nov 2011
    Posts
    1

    Default Similar Problem

    I'm facing similar issue

    I'm using spring AOP's around advice to capture processing time of a transaction. I'm getting the following error during application startup

    Code:
    error creating bean "coreMessageResourceAccesor"
       Could not generate CGLIB subclass of class 
         [class org.springframework.context.support.MessageSourceAccessor]: 
    Common causes of this problem include using a final class or a non-visible class; 
    nested exception is java.lang.IllegalArgumentException: 
       Superclass has no null constructors but no arguments were given
    Below is the configuration for the bean

    Code:
    <bean id="coreMessageSourceAccessor"  class="org.springframework.context.support.MessageSourceAccessor" >
        <constructor-arg type="org.springframework.context.MessageSource" ref="coreMessageSource" />
    </bean>
    In my case, I cannot add no-arg constructor since the bean is using "MessageSourceAccessor" a Spring class. And also "MessageSourceAccessor" doesn't implement any interface so, jdk proxy will not work.

    please advise.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •