Error creating bean with name 'person' defined in class path resource [config.xml]: Cannot resolve reference to bean 'add' while setting bean property 'address'
I am getting the above error but cannot understand why it is hapenning. All of my classes are in the same package.
Below is my config file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.lcwd.withoutboot.Person" id="person">
<property name="name" value="Sourabh"/>
<property name="address" ref="add"/>
</bean>
<bean class="com.lcwd.withoutboot.Address" id="add">
<property name="city" value="Mumbai"/>
</bean>
</beans>
App.java
public class App
{
public static void main( String[] args ) {
ApplicationContext context=new ClassPathXmlApplicationContext("config.xml"); System.out.println( "Hello World!" );
Person person=context.getBean("person", Person.class);
System.out.println(person.getName());
System.out.println(person.getAddress());
}
}
Person.java
package com.lcwd.withoutboot;
public class Person { Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Address.java
package com.lcwd.withoutboot;
public class Address { String city;
@Override
public String toString() {
return "Address{" +
"city='" + city + '\'' +
'}';
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lcwd.withoutboot</groupId>
<artifactId>SpringWithoutBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringWithoutBoot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
To fix this issue, you could add the method setter
for "city" field in the "Address":
public void setCity(String city) {
this.city = city;
}
Or you could create a constructor of class Address:
public Address(String city) {
this.city = city;
}
You got this issue because Spring tries to create an instance of the "Address" object for the "add" bean, but the "Address" class does not have a constructor or setter method for the "city" property. I hope this help!