interface - implements
와 @Qualifier
을 연습해본 예제이다.
package com.example.demo.test;
import org.springframework.stereotype.Service;
@Service
public interface Car {
public final String a="시동 걸기";
public final String b="D 기어 변경";
public final String c="엑셀 밟기";
public final String d="전진";
public String drive1();
public String drive2();
}
먼저 final
로 변하지 않는 값을 저장한 문자형 변수 a,b,c,d와
String형을 반환할 drive1, drive2 메서드를 가진 부모 Car 추상 클래스
(interface 이용)를 만들었다.
package com.example.demo.test;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Qualifier;
@Service
@Qualifier("A")
public class Drive1 implements Car {
public String drive1() {
System.out.println(a);
return a;
}
public String drive2() {
System.out.println(d);
return d;
}
}
package com.example.demo.test;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Qualifier;
@Service
@Qualifier("B")
public class Drive2 implements Car{
public String drive1() {
System.out.println(b);
return b;
}
public String drive2() {
System.out.println(c);
return c;
}
}
그리고 자식 클래스 Drive1과 Drive2를 만들어 implements
로 위의 Car를 상속받았다.
에러가 발생하지 않도록 Qualifier를 이용해 각각 A, B로 설정해 주었다.
package com.example.demo.Control;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.demo.test.*; // 패키지가 서로 다르므로 import해준다.
@Controller
public class Control {
@Autowired
@Qualifier("A")
private Car c1;
@Autowired
@Qualifier("B")
private Car c2;
@RequestMapping(value="/", method=RequestMethod.GET)
public String con(){
return "index";
}
@RequestMapping(value="abstract1", method=RequestMethod.GET)
public String abstractA(){
return "abstract1";
}
@RequestMapping(value="abstract2", method=RequestMethod.GET)
public String abstractB(Model mo){
c1.drive1(); // Console창에 출력하는 용도
c2.drive1();
c2.drive2();
c1.drive2();
mo.addAttribute("c1", c1.drive1()); // html에 return한 데이터를 넘길 용도
mo.addAttribute("c2", c2.drive1());
mo.addAttribute("c3", c2.drive2());
mo.addAttribute("c4", c1.drive2());
return "abstract2";
}
}
그리고 html로 정보를 넘겨주기 위해 먼저 Control 클래스로 이동했다.
@Autowired
와 @Qualifier
를 이용해 Drive1()과 Drive2() 클래스로 설정한 Car의 내용을 각각 c1, c2에 저장한다.
그리고 Model
을 이용해 c1의 drive1() drive2() 메서드, c2의 drive1() drive2() 메서드를 변수에 저장한다.
이는 각각의 메서드에서 return하는 String형 데이터를 html로 보내주는 역할을 한다.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>abstract2</title>
</head>
<body>
<div th:text="${c1}"></div>
<div th:text="${c2}"></div>
<div th:text="${c3}"></div>
<div th:text="${c4}"></div>
</body>
</html>
마지막으로 html 파일로 넘어와 타임 리프를 이용해 받아온 변수를 출력해주면 끝 !
또는 Car가 가진 final 변수를 바로 가져와 보내는 방법도 가능하다.
mo.addAttribute("a", c1.a); // c1 안의 변수를 찾아와 가져온다.
mo.addAttribute("b", c1.b);
mo.addAttribute("c", c1.c);
mo.addAttribute("d", c1.d);
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>abstract2</title>
</head>
<body>
<div th:text="${a}"></div>
<div th:text="${b}"></div>
<div th:text="${c}"></div>
<div th:text="${d}"></div>
</body>
</html>
'Spring Boot' 카테고리의 다른 글
[Thymeleaf] button th:onclick으로 페이지 이동하기 (0) | 2023.05.25 |
---|---|
[스프링 부트] html의 데이터 추상 클래스에 저장해 출력하기 (0) | 2023.05.24 |
[스프링 부트] interface - implements 추상 메소드 상속하기 (0) | 2023.05.22 |
[스프링 부트] NoSuchBeanDefinitionException: 에러 해결하기 (0) | 2023.05.22 |
[스프링 부트] @Configuration과 @Bean 이용하기 (0) | 2023.05.18 |