【问题标题】:Spring MVC no default constructor found?Spring MVC没有找到默认构造函数?
【发布时间】:2012-03-06 23:58:58
【问题描述】:

我的 Spring 控制器有问题 - 我没有找到默认构造函数 - 但它们确实有一个我试图通过 applicationContext.xml 创建的构造函数 - 相关位如下:

<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) 构造函数。

这里是 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>()

非常感谢任何帮助!

【问题讨论】:

    标签: java spring spring-mvc web.xml


    【解决方案1】:

    您应该在 xml 中定义您的 bean 或对它们进行注释,而不是同时使用两者(如果只是为了避免像您遇到的错误那样)。

    这里的问题是你没有自动装配构造函数参数,所以 spring 不知道如何处理你的控制器。它知道它必须创建一个 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 属性。

    【讨论】:

    • 很高兴确认这一点,spring 需要默认构造函数或自动连接构造函数。
    【解决方案2】:

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

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

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

    【解决方案3】:

    如果您还没有激活 Spring 的基于注解的配置,您也可能会收到此错误。将其包含在您的 Spring Xml 中:

    【讨论】:

      【解决方案4】:

      其他发帖者指出,如果您将自动装配/组件扫描与 bean 的显式实例化混合使用,您可能会遇到问题。我在一个 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>
      

      【讨论】:

        猜你喜欢
        • 2012-01-10
        • 1970-01-01
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 2016-07-18
        • 1970-01-01
        相关资源
        最近更新 更多