Spring注入中需要默认构造函数? [英] Is default constructor required in Spring injection?

查看:1243
本文介绍了Spring注入中需要默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图注入一个构造函数,需要一些参数。编译后Spring抱怨它找不到默认构造函数(我没有定义它),并抛出BeanInstatiationException和NoSuchMethodException。

I'm trying to inject a constructor that takes some arguments. After compiling Spring complains it couldn't find a default constructor (I haven't defined it) and throws BeanInstatiationException and NoSuchMethodException.

定义一个默认构造函数后, t出现了,然而我的对象从来没有使用参数构造函数初始化,只调用默认的。 Spring在这种情况下真的需要一个默认的构造函数吗?如果是的话,我如何使用参数构造函数而不是默认参数构造函数?

After defining a default constructor the exceptions don't appear anymore, however my object is never initialized with the argument constructor, only the default one is called. Does Spring really require a default constructor in this case? And if yes, how can I make it use the argument constructor instead of the default one?

这是我连接所有东西的方法:

This is how I wire everything:

public class Servlet {

  @Autowired
  private Module module;

  (code that uses module...)
}

@Component
public class Module {

  public Module(String arg) {}
  ...
}



<

Bean configuration:

<beans>
  <bean id="module" class="com.client.Module">
    <constructor-arg type="java.lang.String" index="0">
    <value>Text</value>
    </constructor-arg>
  </bean>

  ...
</beans>

堆栈跟踪:

WARNING: Could not get url for /javax/servlet/resources/j2ee_web_services_1_1.xsd
ERROR  initWebApplicationContext, Context initialization failed
[tomcat:launch] org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'module' defined in URL [...]: Instantiation of bean failed;  
nested exception is org.springframework.beans.BeanInstantiationException: Could not 
instantiate bean class [com.client.Module]: No default constructor found; nested 
exception is java.lang.NoSuchMethodException: com.client.Module.<init>()


推荐答案

如果你计划实例化它没有任何参数,Spring只需要一个默认的构造函数。

Spring only "requires" a default constructor if you plan on instantiating it without any arguments.

如果你的类是这样的;

for example, if your class is like this;

public class MyClass {

  private String something; 

  public MyClass(String something) {
    this.something = something;
  }

  public void setSomething(String something) {
    this.something = something;
  }

}

像这样;

<bean id="myClass" class="foo.bar.MyClass">
  <property name="something" value="hello"/>
</bean>

你会得到一个错误。原因是Spring实例化你的类 new MyClass()然后尝试设置调用 setSomething(..)

you're going to get an error. the reason is that Spring instantiates your class new MyClass() then tries to set call setSomething(..).

因此,Spring xml应该像这样;

so instead, the Spring xml should look like this;

<bean id="myClass" class="foo.bar.MyClass">
  <constructor-arg value="hello"/>
</bean>

因此请查看您的 com.client.Module 并查看它在Spring xml中的配置

so have a look at your com.client.Module and see how its configured in your Spring xml

这篇关于Spring注入中需要默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆