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 "";

}