【问题标题】:Spring @Autowired constructor gives No default constructor foundSpring @Autowired 构造函数给出没有找到默认构造函数
【发布时间】:2012-01-10 05:44:55
【问题描述】:

这里是 Spring 3.0 的一些奇怪行为。

package com.service.schedule;

import org.springframework.stereotype.Component;

@Component("outroJob")
public class OutroJob {

    public void printMe() {
        System.out.println("running...");
    }

}

package com.service.schedule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

@Component("testeAutowired")
public class TesteAutowired {

    @Autowired
    public TesteAutowired(OutroJob outroJob) {
        outroJob.printMe();
    }

    public static void main(String[] args) {
        ClassPathResource res = new ClassPathResource("applicationContext.xml");
        XmlBeanFactory ctx = new XmlBeanFactory(res);

        OutroJob outroJob = (OutroJob) ctx.getBean("outroJob");
        outroJob.printMe(); // gives: running...

        ctx.getBean("testeAutowired");
    }
}

这些 bean 都没有在 applicationContext.xml 中声明

所以,行 outroJob.printMe();工作正常...打印“正在运行...”

但是当我尝试获取“testeAutowired”bean 时,它会说:

无法实例化 bean 类 [com.service.schedule.TesteAutowired]:未找到默认构造函数; 嵌套异常是 java.lang.NoSuchMethodException: com.service.schedule.TesteAutowired。

问题是:为什么,如果 Spring 找到了“outroJob”bean,它不会在 TesteAutowired 构造函数中自动装配它?

它必须做什么似乎很明显......

【问题讨论】:

  • 如果使用 ApplicationContext 而不是 XmlBeanFactory 会发生什么?我看到 XmlBeanFactory 在 3.1 中已被弃用,也许这就是原因之一。

标签: java spring autowired default-constructor


【解决方案1】:

我收到相同的错误消息,但有不同的问题。我正在使用 XML 配置并将 @Autowired 放在类构造函数上。

我通过在我的 XML 配置文件中启用注释驱动配置解决了这个问题:

<?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"
       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">


    <context:annotation-config/>

【讨论】:

    【解决方案2】:

    为组件创建一个接口并尝试使用自动装配的构造函数自动装配接口和类。

    【讨论】:

      【解决方案3】:

      尝试使用 ApplicationContext 而不是 XmlBeanFactory。 XmlBeanFactory 不会对注释进行后处理,即不使用 AutowiredAnnotationBeanPostProcessor 来解释您遇到的行为。

      Here's some more explanation

      【讨论】:

        【解决方案4】:

        尝试使用

        @Autowired(required=true)
        public TesteAutowired(OutroJob outroJob) {
            outroJob.printMe();
        }
        

        这应该迫使 Spring 使用该构造函数。否则,它会构建一个构造函数列表并选择最佳候选者。显然它确实想要一个默认构造函数作为候选者,我猜。

        参考:http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.html

        【讨论】:

        • required 的默认值为true 时,我无法想象这会有什么帮助。 IE。 @Autowired == @Autowired(required=true)
        猜你喜欢
        • 2012-03-06
        • 2023-03-20
        • 2016-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多