I'm new to spring. I'm stuck in an assignment.

I am attaching my whole code below apart from pom.xml.

Cust.java

package com.spring.app;

public class Cust {
  private String name;
  public void setname(String name) {
      this.name=name;
  }
  public String getname() {
      return name;
  }
  
}

Member.java

package com.spring.app;

public class Member {
  private long no;
  private Cust cust;
  public void Member(long no,Cust cust) {
      this.no=no;
      this.cust=cust;
  }
@Override
public String toString() {
    return String.valueOf(this.no)+cust;    
}
}

Driver.java

package com.spring.app;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Driver {

    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("com/spring/app/ciconfig.xml");
        Member m=(Member) context.getBean("member",Member.class);
        System.out.println(m);
    }
}

ciconfig.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.com.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd">
 
 <bean class="com.spring.app.Cust" name="cust" p:name="sriyak">
 </bean>
 <bean class="com.spring.app.Member" name="member">
 <constructor-arg value="9123823421"></constructor-arg>
 <constructor-arg><ref bean="cust"/></constructor-arg>
 </bean>
 </beans>

I am getting the error below:

Apr 24, 2022 10:53:08 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1376c05c: startup date [Sun Apr 24 22:53:08 IST 2022]; root of context hierarchy
Apr 24, 2022 10:53:09 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/spring/app/ciconfig.xml]
Apr 24, 2022 10:53:11 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 'member' defined in class path resource [com/spring/app/ciconfig.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 'member' defined in class path resource [com/spring/app/ciconfig.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:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.spring.app.Driver.main(Driver.java:9)

What am I doing wrong?

0

There are 0 best solutions below