Spring MVC no default constructor found?

24,169

Solution 1

You should either define your beans in xml or annotate them, not both (if only to avoid errors like the one you're getting).

The problem here is that you're not autowiring constructor args, so spring doesn't know what to do with your controller. It knows it has to create a bean (@Controller annotation), but it doesn't know how (no default, nor autowired constructor).

You can try to do something like:

@Controller
public class CacheHandler {

private final CustomGxSessionIdCacheImpl gxSessionIdCache;

@Autowired 
public CacheHandler(CustomGxSessionIdCacheImpl gxSessionIdCache) {
   this.gxSessionIdCache = gxSessionIdCache;
} 

and then in xml:

<bean id="gxSessionIdCache"
  factory-bean="PcrfSimulator"
  factory-method="getGxSessionIdCache"/>

So it will autowire constructor parameters.

Another option is to simply create default constructor and autowire gxSessionIdCache property.

Solution 2

You have to add an empty default constructor :

    @Controller
    public class CacheHandler {

    private final CustomGxSessionIdCacheImpl gxSessionIdCache;

    @Autowired
    public CacheHandler(CustomGxSessionIdCacheImpl gxSessionIdCache) {
        this.gxSessionIdCache = gxSessionIdCache;
    }

But be carefull, because it seems that you are mixing annotation based configuration (@Controller) and XML configuration. In the example above, it uses the annotation based config (so please remove the bean declaration from your XML file).

Share:
24,169
chetan godiya
Author by

chetan godiya

please delete me

Updated on July 05, 2022

Comments

  • chetan godiya
    chetan godiya almost 2 years

    I'm having problems with my Spring controllers - I'm getting no default constructor found - but they do have a constructor which I am trying to created via the applicationContext.xml - heres the relevant bit:

    <bean id="PcrfSimulator" class="com.rory.services.pcrf.simulator.PcrfSimulator" init-method="start">  
    </bean>
    
    <bean id="CacheHandler" class="com.rory.services.pcrf.simulator.handlers.CacheHandler">
        <constructor-arg index="0" type="com.rory.services.pcrf.simulator.CustomGxSessionIdCacheImpl">
            <bean factory-bean="PcrfSimulator" factory-method="getGxSessionIdCache">
            </bean>
        </constructor-arg>       
    </bean>
    

    Ie. I'm creating a bean first, and then trying to pass the result of a method call from that bean into the second bean's (CacheHandler) constructor.

    Here'e the start of CacheHandler:

        @Controller
        public class CacheHandler {
    
        private final CustomGxSessionIdCacheImpl gxSessionIdCache;
    
        public CacheHandler(CustomGxSessionIdCacheImpl gxSessionIdCache) {
            this.gxSessionIdCache = gxSessionIdCache;
        }
    

    Here's the error I'm getting:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheHandler' defined in URL [jar:file:/users/rtorney/Documents/apache-tomcat-7.0.25/webapps/PCRFSimulator-4.0/WEB-INF/lib/PCRFSimulator-4.0.jar!/com/rory/services/pcrf/simulator/handlers/CacheHandler.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.rory.services.pcrf.simulator.handlers.CacheHandler]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.rory.services.pcrf.simulator.handlers.CacheHandler.<init>()
    

    Any help is much appreciated!

  • Jackie
    Jackie over 11 years
    great to confirm this, spring need either default constructor, or auto wired constructor.
  • Raedwald
    Raedwald over 9 years