构造函数依赖注入问题

时间:2017-06-29 09:59:34

标签: spring dependency-injection javabeans creation

我正在学习DI而且在尝试CI的时候是新手,我已经编写了以下代码,我认为我的语法正确但它仍然显示了bean创建错误。为什么它无法创建bean .. ?? 代码是

Constuctor.java

package beans;

public class Constructor {
private String name;
private int age;
private String email;

public void Constructor(String name, int age, String email){
    this.name=name;
    this.age=age;
    this.email=email;    
}

public void show()
{
    System.out.println("Name = "+name);
    System.out.println("Age = "+age);
    System.out.println("Email = "+email);
}

}

spring.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
        <!-- Ordered parameters -->

        <beans>

        <bean id="t" class="beans.Constructor">

        <constructor-arg value="Alok"/>
        <constructor-arg  value="24"/>
        <constructor-arg  value="alok@gmail.com"/>

        </bean>

        </beans>

Const_main.java

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import beans.Constructor;

public class Const_main {
    public static void main(String[] args) {
       ApplicationContext ap= new  ClassPathXmlApplicationContext("resources/spring.xml");
        Constructor c = (Constructor)ap.getBean("t");
            c.show();

    }

}

它发出以下错误

Jun 29, 2017 3:16:45 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b1a58a3: startup date [Thu Jun 29 15:16:45 IST 2017]; root of context hierarchy
Jun 29, 2017 3:16:45 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resources/spring.xml]
Jun 29, 2017 3:16:46 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: **Error creating bean with name 't' defined in class path resource [resources/spring.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Exception in thread "main"** org.springframework.beans.factory.BeanCreationException: Error creating bean with name 't' defined in class path resource [resources/spring.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:240)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at test.Const_main.main(Const_main.java:10)

2 个答案:

答案 0 :(得分:0)

试试这种方式

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!-- Ordered parameters -->

<beans>

<bean id="t" class="beans.Constructor">

<constructor-arg>
  <value>Alok</value>
</constructor-arg>
<constructor-arg>
  <value>24</value>
</constructor-arg>
<constructor-arg>
  <value>alok@gmail.com</value>
</constructor-arg>

</bean>

</beans>

答案 1 :(得分:0)

您正在尝试使用构造函数依赖注入而不在DTO中创建任何此类构造函数(Constructor.java

您在上面定义的方法:

public void Constructor(String name, int age, String email){
    this.name=name;
    this.age=age;
    this.email=email;    
}

只是一个简单的方法/函数而不是构造函数,请尝试删除单词void

注意:

要消除构造函数匹配的任何歧义,更倾向于使用带有构造函数参数的索引,如:

    <bean id="t" class="beans.Constructor"> 
      <constructor-arg value="Alok" index="0"/> 
      <constructor-arg value="24" index="1"/> 
      <constructor-arg value="alok@gmail.com" index="2"/> 
   </bean>