Here we will learn about the Setter Injection in Spring using Annotation.
Company Bean Class
Company.java
package bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*
* This Annotation enable to scan this class
* Value will decide the ID of create bean. This ID can be used to get the bean of this class
*/
@Component(value = "company1")
public class Company {
@Autowired
@Value("VGautam")
String companyCEO;
@Autowired(required = true)
// @Qualifier("Address")
Address companyAddress;
public String getCompanyCEO() {
return companyCEO;
}
public void setCompanyCEO(String companyCEO) {
this.companyCEO = companyCEO;
}
public Address getCompanyAddress() {
return companyAddress;
}
public void setCompanyAddress(Address companyAddress) {
this.companyAddress = companyAddress;
}
@Override
public String toString() {
return "Company [companyCEO=" + companyCEO + ", companyAddress="
+ companyAddress + "]";
}
}
Address Bean Class
Address.java
package bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Address {
@Autowired(required=true)
@Value("201")
public String flatNo;
@Autowired(required=true)
@Value("2")
public String bldgNo;
@Autowired
@Value("Street No")
public String streetNo;
@Autowired
@Value("Mumbai")
public String city;
@Autowired
@Value("123456")
public int pincode;
public String getStreetNo() {
return streetNo;
}
public void setStreetNo(String streetNo) {
System.out.println("Setter method is called");
this.streetNo = streetNo;
}
public String getFlatNo() {
return flatNo;
}
public void setFlatNo(String flatNo) {
this.flatNo = flatNo;
}
public String getBldgNo() {
return bldgNo;
}
public void setBldgNo(String bldgNo) {
this.bldgNo = bldgNo;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPincode() {
return pincode;
}
public void setPincode(int pincode) {
this.pincode = pincode;
}
/*
* No-Argument Constructor or No-Argument Static factory method is required
* to make setter injection work. In case none of them exist(No arg
* Constructor/Static method) "No default constructor found;" error will be
* thrown
*/
private Address() {
super();
System.out.println("Invoking No Arg Constructor");
}
@Override
public String toString() {
return "Address [flatNo=" + flatNo + ", bldgNo=" + bldgNo
+ ", streetNo=" + streetNo + ", city=" + city + ", pincode="
+ pincode + "]";
}
}
Main Class
package main;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import bean.Address;
import bean.Company;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext();
/*
* Below line will scan the given package for Components. It will be
* refreshed then to get bean from the same.
*/
annotationContext.scan("bean");
annotationContext.refresh();
/*
* Here Class name is used to get the object of Address class
*/
Address address = annotationContext.getBean(Address.class);
/*
* Here ID name is used to get the Object of Company Class
*/
Company company = (Company) annotationContext.getBean("company1");
System.out.println("Address Object :::::" + address.toString());
System.out.println("Company Object :::::" + company.toString());
}
}
Output of the above programe
Invoking No Arg Constructor
Address Object :::::Address [flatNo=201, bldgNo=2, streetNo=Street No, city=Mumbai, pincode=123456]
Company Object :::::Company [companyCEO=VGautam, companyAddress=Address [flatNo=201, bldgNo=2, streetNo=Street No, city=Mumbai, pincode=123456]]
Iam getting ClassNotFoundException while run this application
how to solve it