Controller,Service,Repository์์์ DI
2023. 11. 5. 03:32ใ๋ฐฑ์๋/Spring Boot
๐ค Controller,Service,Repository์์ @Autowired๋ฅผ ์ด์ฉํ์ฌ DI๋ฅผ ํ ์ ์๋ ์ด์ ์ ๋ํ์ฌ ์์๋ณด์.
* ์์ ์ฝ๋
1. Controller
package com.example.demo.controller;
import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController // @RestController > @Controller > @Component ๋ฅผ ํฌํจํ๊ณ ์์ด DemoController ๊ฐ ๋น์ผ๋ก ๋ฑ๋ก๋จ
public class DemoController {
@Autowired // DI ์ฃผ์
์์ฒญ
DemoService demoService; // ์์ฑ์ ์ฃผ์
@RequestMapping("/test")
public String test() {
return demoService.find();
}
}
2. Service
package com.example.demo.service;
import com.example.demo.repository.DemoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
@Autowired
DemoRepository demoRepository;
public String find() {
return demoRepository.find();
}
}
3. Repository
package com.example.demo.repository;
import org.springframework.stereotype.Repository;
@Repository
public class DemoRepository {
public String find() {
return "Repo - Find";
}
}
๊ฒฐ๊ณผ

Controller,Service,Repository์์ @Autowired๋ฅผ ์ด์ฉํ์ฌ DI๋ฅผ ํ ์ ์๋ ์ด์
๐ ๊ฐ๊ฐ์ ์ด๋
ธํ
์ด์
๋ค์ด ๊ฐ์ง๋ ๋ฉํ ์ด๋
ธํ
์ด์
์ ์ฐพ์๊ฐ ๋ณด๋ฉด @Component๊ฐ ์ ์ธ๋์ด ์๊ณ , ๊ทธ๋ก ์ธํด ์๋์ผ๋ก Spring ์ปจํ
์ด๋์ Bean์ผ๋ก ๋ฑ๋ก์ด ๋์๊ณ @Autowired๋ฅผ ์ด์ฉํ์ฌ DI๋ฅผ ํ ์ ์์๋ ๊ฒ์ด๋ค.
1. Controller์ ๊ฒฝ์ฐ
// RestController.java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
* @since 4.0.1
*/
@AliasFor(annotation = Controller.class)
String value() default "";
}
// Controller.java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // ์ด๊ณณ์์ ์ปดํฌ๋ํธ๊ฐ ์ ์ธ๋์ด ์๋์ผ๋ก ์คํ๋ง ์ปจํ
์ด๋์ Bean์ผ๋ก ๋ฑ๋ก๋จ
public @interface Controller {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
2. Service์ ๊ฒฝ์ฐ
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // ์ด๊ณณ์์ ์ปดํฌ๋ํธ๊ฐ ์ ์ธ๋์ด ์๋์ผ๋ก ์คํ๋ง ์ปจํ
์ด๋์ Bean์ผ๋ก ๋ฑ๋ก๋จ
public @interface Service {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
3. Repository์ ๊ฒฝ์ฐ
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // ์ด๊ณณ์์ ์ปดํฌ๋ํธ๊ฐ ์ ์ธ๋์ด ์๋์ผ๋ก ์คํ๋ง ์ปจํ
์ด๋์ Bean์ผ๋ก ๋ฑ๋ก๋จ
public @interface Repository {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}'๋ฐฑ์๋ > Spring Boot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| JPA : Java Persistence API (1) | 2023.11.15 |
|---|---|
| Entity vs DTO vs VO ๊ฐ๋ ๊ณผ ํน์ง (1) | 2023.11.08 |
| ์คํ๋ง ๋น(Bean) ์ด๋? (0) | 2023.11.05 |
| ์คํ๋ง ๋ถํธ ํ๋ก์ ํธ ์์ฑ ํ Controller ์์ฑํ๊ธฐ (1) | 2023.11.03 |
| Spring ๊ณ์ธต ๊ตฌ์กฐ์ ์ญํ (0) | 2023.11.01 |