package in.sp.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Student
{
@Value("Deepak")
private String name;
@Value("104")
private int rollno;
@Value("deepak@gmail.com")
private String emailid;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public String getEmailid() {
return emailid;
}
public void setEmailid(String emailid) {
this.emailid = emailid;
}
public void display()
{
System.out.println("Name : "+name);
System.out.println("Roll No : "+rollno);
System.out.println("Email Id : "+emailid);
}
}
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
package in.sp.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import in.sp.beans.Student;
@Configuration
@ComponentScan(basePackages = "in.sp.beans")
public class MainApp
{
public static void main(String[] args)
{
// Spring container is created with MainApp as configuration class
ApplicationContext context = new AnnotationConfigApplicationContext(MainApp.class);
// Bean object is requested from Spring Container and stored in Student reference
Student std = context.getBean(Student.class);
std.display();
}
}
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.