package in.sp.beans;
import org.springframework.stereotype.Component;
@Component
public class Student
{
private String name;
private int rollno;
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);
}
}
package in.sp.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "in.sp.beans")
public class AppConfig
{
// No bean definitions are required here
}
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
package in.sp.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import in.sp.beans.Student;
import in.sp.config.AppConfig;
public class MainApp
{
public static void main(String[] args)
{
//Spring container is created
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
//Bean object is requested from Spring Container and stored in Student reference
Student std = context.getBean(Student.class);
std.display();
}
}
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("103")
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);
}
}
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.