-
Controller에 Autowired 사용하기Spring-diary/WEB 2020. 2. 11. 17:44
- Controller에도 멤버 필드에 Autowired를 사용할 수 있다.
- DB에 접속하는 Controller를 만드는 경우 DB 공통 Interface를 만든 후 Bean을 이용해
DB의 종류를 바꿔가며 사용할 수 있다.
ex)
Mysql 사용하기
- Interface 생성
- Mysql에서 특정 DB에 table 생성
- MysqlDAO class에 CommonDAO Interface 연결
- DB연결 metho와 어느 DB에 연결할지 명령어 입력
- Dispatcher에 bean 생성
- Controller Method
*url에 어느 db에 접속할지 미리 명시해 줘도 된다.
jdbc:mysql://localhost:3306/접속할db이름?serverTimezone=UTC#
- package에 있는 모든 Controller를 일일이 Bean으로 등록하지 않고 한번에 등록할 수 있다.
대신 Annotation이 표시된 Controller만 등록한다.
context:component-scan을 사용한다. 'Spring-diary > WEB' 카테고리의 다른 글
URL에 대해서 (개념 보완 필요) (0) 2020.02.12 Configuration File 설정 (0) 2020.02.12 [Controller]Post방식만 받기 (0) 2020.02.11 [STS][Spring-web][MVC]Spring Elements (0) 2020.02.11 Annotation으로 Controller Interface 구현하기 (0) 2020.02.11