Constructor dependency Injection issue

我与影子孤独终老i 提交于 2021-01-28 06:01:06

问题


I am learning DI and new to spring while trying out CI I have written following code and I think I am correct in syntax still it's showing bean creation error. why it is unable to create bean..?? The code is

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

    }

}

it's giving the following error

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)

回答1:


Try this way

<!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>



回答2:


You are trying to use Constructor Dependency Injection without creating any such constructor in your DTO (Constructor.java)

The method you defined above:

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

is just a simple method/function not a constructor, try to remove the word void.

Note:

To remove any ambiguities for constructor matching, it is more prefer to use indexes with constructor's parameters like:

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


来源:https://stackoverflow.com/questions/44821991/constructor-dependency-injection-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!