1. 자바빈 객체 생성
✔ Student.java >>
import org.springframework.stereotype.Component;
@Component
public class Student {
private String name;
private String grade;
public Student(){
name = "기본 이름";
grade = "기본 등급";
}
public Student(String name, String grade) {
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return String.format("%s[%s]", name, grade);
}
}
2. 컨트롤러에 Autowired 어노테이션 추가
✔ HomeController.java >>
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@Autowired
Student student;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
System.out.println("stu : " + student);
return "home";
}
}
📺 출력 결과 >>
-> Student를 직접 생성하지 않아도 Autowired으로 자동으로 생성됨을 확인할 수 있다