Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource

user548656 picture user548656 · Dec 20, 2010 · Viewed 63.1k times · Source

PROBLEM :

I am creating a pointcut for execution of a method in a class . This class is a controller class and denoted by annotation @Controller and hence no bean is needed for the same which is required by the aspect. I am attaching the dispathcher servlet code , aspect and the controller class.Can somebody identify what the problem is.

DISPATCHER SERVLET :

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 <context:annotation-config />
 <context:spring-configured />

 <aop:aspectj-autoproxy /> 
  <bean id="LoggerBean" class="com.persistent.eap.aop.LoggerAspect" /> 


 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
 <property name="order" value="0" />
 </bean>
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 <bean class="com.persistent.eap.validators.UserRegistrationValidator" />

 <bean id="userRegistrationService" class="com.persistent.eap.service.impl.UserRegistrationServiceImpl" />
 <bean id="userOperationsService" class="com.persistent.eap.service.impl.UserOperationsServiceImpl" />

 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
    </bean>

 <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
 </bean> 
 <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" p:definitions="/WEB-INF/tiles-defs.xml" />     


 <context:component-scan base-package="com.persistent.eap.controllers" />
 <context:component-scan base-package="com.persistent.eap.service" />
    <context:component-scan base-package="com.persistent.eap.dao" />

    <import resource="databaseContext.xml"/>


 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
  <property name="order" value="1" />
  <property name="mappings">
   <value>
    /*.htm=urlController
            </value>
  </property>
 </bean>

 <bean id="urlController"
  class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />


</beans>

ASPECT :

package com.persistent.eap.aop;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.annotation.Before;


@Aspect
public class LoggerAspect {


  @Pointcut("execution(* com.persistent.eap.controllers.UserRegistrationController.setUpForm(..))")
  public void controllerMethods(){}

     @Around("controllerMethods()")
     public Object controllerMethodCall(ProceedingJoinPoint call) throws Throwable{
        System.out.println("[LOGGING INFO : Entering Method "+call.toShortString()+" ]");

     Object point=call.proceed();

     System.out.println("[LOGGING INFO : Exiting Method "+call.toShortString()+" with return as "+point+" ]");
     return point;
     }



}

CONTROLLER CLASS :

package com.persistent.eap.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.persistent.eap.domain.UserDetails;
import com.persistent.eap.service.intfc.UserRegistrationService;
import com.persistent.eap.validators.UserRegistrationValidator;

@Controller
@RequestMapping("/userRegistrationForm.htm")
public class UserRegistrationController {

 private UserRegistrationService userRegistrationService;
 private UserRegistrationValidator userRegistrationValidator;

 @Autowired
 public UserRegistrationController(UserRegistrationService userRegistrationService
                            ,UserRegistrationValidator userRegistrationValidator) {
  this.userRegistrationService = userRegistrationService;
  this.userRegistrationValidator = userRegistrationValidator;
 }

 @RequestMapping(method=RequestMethod.GET)
 public String setUpForm(ModelMap model){
  UserDetails userDetails = new UserDetails();
  model.addAttribute("userDetails", userDetails);
  return "userRegistrationForm";
 }

 @RequestMapping(method=RequestMethod.POST)
 public String onSubmit(@ModelAttribute("userDetails") UserDetails userDetails,BindingResult result){
  userRegistrationValidator.validate(userDetails, result);
  if(result.hasErrors()){
   return "userRegistrationForm";
  }
  else{
   System.out.println(userDetails.getId()+"  "+userDetails.getUsername());
   userRegistrationService.registerUserDetails(userDetails); 
   return "userRegistrationSuccess";
  }
 }

}

ERROR ON CONSOLE :

SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRegistrationController' defined in file [C:\Documents and Settings\samiksha_kalra\Desktop\VMforce\EclipseWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\EAPProject\WEB-INF\classes\com\persistent\eap\controllers\UserRegistrationController.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.persistent.eap.controllers.UserRegistrationController]: 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
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
 at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
 at javax.servlet.GenericServlet.init(GenericServlet.java:212)
 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
 at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
 at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4350)
 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4659)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
 at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
 at org.apache.catalina.core.StandardService.start(StandardService.java:519)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRegistrationController' defined in file [C:\Documents and Settings\samiksha_kalra\Desktop\VMforce\EclipseWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\EAPProject\WEB-INF\classes\com\persistent\eap\controllers\UserRegistrationController.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.persistent.eap.controllers.UserRegistrationController]: 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
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
 at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
 at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:383)
 at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:362)
 at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.detectHandlers(AbstractDetectingUrlHandlerMapping.java:82)
 at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext(AbstractDetectingUrlHandlerMapping.java:58)
 at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119)
 at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:72)
 at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73)
 at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:106)
 at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:85)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:394)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1413)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
 ... 31 more
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.persistent.eap.controllers.UserRegistrationController]: 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
 at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:212)
 at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
 at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476)
 at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362)
 at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
 ... 49 more
Caused by: java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
 at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:718)
 at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:499)
 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:200)
 ... 56 more
Dec 20, 2010 6:28:42 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /EAPProject threw load() exception
java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
 at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:718)
 at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:499)
 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:200)
 at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
 at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476)
 at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362)
 at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
 at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
 at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:383)
 at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:362)
 at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.detectHandlers(AbstractDetectingUrlHandlerMapping.java:82)
 at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext(AbstractDetectingUrlHandlerMapping.java:58)
 at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119)
 at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:72)
 at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73)
 at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:106)
 at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:85)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:394)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1413)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
 at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
 at javax.servlet.GenericServlet.init(GenericServlet.java:212)
 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
 at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
 at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4350)
 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4659)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
 at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
 at org.apache.catalina.core.StandardService.start(StandardService.java:519)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

Answer

skaffman picture skaffman · Dec 20, 2010

This is a limitation in Spring AOP. When you use AspectJ pointcuts to weave aspects into beans, Spring will use CGLIB to generate a subclass of the target, and invoke the aspects from that subclass.

If the target class does not have a public default constructor, however, this will fail. CGLIB does have the ability to handle this, but this is all hidden behind the Spring AOP stuff and you can't change that behaviour.

I can only suggest that you go back to using setter injection for your controller, rather than constructor injection. It's not ideal, I know, but I can't think of any other workaround.