Here below I added some classes, Student class depends on Address Class. object of student class created how to add 'ad' value which is inside Address using @Component and @Value
Student Class
package com.spring.stereotype;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("obj")
public class Student {
private Address ad;
public Address getAd() {
return ad;
}
public void setAd(Address ad) {
this.ad = ad;
}
}
Address Class
package com.spring.stereotype;
public class Address {
private String ad;
public String getAd() {
return ad;
}
public void setAd(String ad) {
this.ad = ad;
}
@Override
public String toString() {
return "Address [ad=" + ad + "]";
}
}
Main class
package com.spring.stereotype;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/spring/stereotype/stereo.xml");
Student st = context.getBean("obj",Student.class);
System.out.println(st.getAd());
}
}
Is showing an error if I add an XML file, here I typed how I created the object <context:component-scan base-package="com.spring.stereotype"/>