2012-02-15 125 views
12

我有我的春天控制器的问题 - 我越来越没有默认构造函数发现 - 但他们确实有这我试图通过创建applicationContext.xml的构造 - 继承人的相关位:Spring MVC没有找到默认构造函数?

<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> 
即,

也就是说。我先创建一个bean,然后试图将该bean的方法调用的结果传递给第二个bean的(CacheHandler)构造函数。

Here'e CacheHandler开始:

@Controller 
    public class CacheHandler { 

    private final CustomGxSessionIdCacheImpl gxSessionIdCache; 

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

这里是我得到的错误:

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>() 

任何帮助,非常感谢!

回答

20

您应该定义你的bean在XML或注释它们,而不是两个(如果仅仅是为了避免像你得到一个错误)。

这里的问题是,你没有自动装配构造ARGS,所以春季不知道你的控制器做什么。它知道它必须创建一个bean(@Controller注释),但它不知道如何(没有默认值,也没有自动装配构造函数)。

你可以试着这样做:

@Controller 
public class CacheHandler { 

private final CustomGxSessionIdCacheImpl gxSessionIdCache; 

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

,然后在XML:

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

所以它会自动装配构造函数的参数。

另一种选择是简单地创建默认构造函数和自动装配gxSessionIdCache属性。

+0

我们有着同样的想法;-) – 2012-02-15 16:25:36

+0

大,感谢所有的答案! – Rory 2012-02-15 16:30:51

+1

很好的证实了这一点,spring需要默认的构造函数或者自动构造函数。 – lwpro2 2013-01-04 08:54:00

2

您必须添加一个空的默认构造函数:

@Controller 
    public class CacheHandler { 

    private final CustomGxSessionIdCacheImpl gxSessionIdCache; 

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

但要小心,因为它看来你是混合基于注解配置(@Controller)和XML配置。在上面的示例中,它使用基于注释的配置(所以请从XML文件中删除bean声明)。

+1

为什么默认的构造函数?这不是强制性的权利? – kosa 2012-02-15 16:12:37

+0

我刚刚编辑了我的帖子。 – 2012-02-15 16:14:24

+0

[Spring是否需要所有bean都有默认构造函数?](http://stackoverflow.com/questions/7492652/does-spring-require-all-beans-to-have-a-default-constructor):[*] *无**](http://stackoverflow.com/a/7492713/545127) – Raedwald 2014-12-11 17:00:05

0

如果你还没有激活Spring的基于注解的配置,你也可以得到这个错误。在Spring的XML包含此:

<方面:注解配置/ >

0

其他海报指出,如果你与豆的显式实例混合自动装配/组件扫描即可获得问题。我有一个类似的问题与一个Web应用程序那样做。我能够通过告诉组件扫描程序不要自动实例化关键类的bean来解决问题。就像这样:

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

    <aop:aspectj-autoproxy /> 

    <import resource="repository.xml" /> 
    ... 

    <context:component-scan base-package="com.example.webserver"> 
     <context:exclude-filter type="regex" expression="MyRepositoryImpl" /> 
     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" /> 
    </context:component-scan> 
</beans> 

其中repository.xml包括bean的实例:

<beans xmlns=...> 
    <bean id="password" class="org.springframework.jndi.JndiObjectFactoryBean"> 
     <property name="jndiName" value="java:/comp/env/store/clientPassword" /> 
    </bean> 
    <bean id="repository" class="com.example.webserver.datalayer.MyRepositoryImpl"> 
     <constructor-arg ref="password" /> 
    </bean> 
... 
</beans> 
相关问题