AmazonProperties에 amazon.associateId 구성 프로퍼티를 추가했으므로 코드 3-14와 같이 ReadingListController를 변경하여 주입된 AmazonProperties에서 아마존 제휴 ID를 가져올 수 있다.
코드 3-14 AmazonProperties가 주입된 ReadingListController
package readinglist; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/") public class ReadingListController { private ReadingListRepository readingListRepository; private AmazonProperties amazonProperties; @Autowired public ReadingListController(ReadingListRepository readingListRepository, AmazonProperties amazonProperties) { // AmazonProperties 주입 this.readingListRepository=readingListRepository; this.amazonProperties=amazonProperties; } @RequestMapping(method=RequestMethod.GET) public String readersBooks(Reader reader, Model model) { List<Book> readingList=readingListRepository.findByReader(reader); if (readingList != null) { model.addAttribute("books", readingList); model.addAttribute("reader", reader); // 제휴 ID를 모델에 추가 model.addAttribute("amazonID", amazonProperties.getAssociateId()); } return "readingList"; } @RequestMapping(method=RequestMethod.POST) public String addToReadingList(Reader reader, Book book) { book.setReader(reader); readingListRepository.save(book); return "redirect:/"; } }
더는 ReadingListController에 구성 프로퍼티를 직접 주입하지 않는다. 그 대신 주입된 Amazon Properties 빈에서 필요한 정보를 얻어 온다.
앞에서 살펴보았듯이 구성 프로퍼티는 자동 구성될 컴포넌트와 애플리케이션 빈에 주입될 세부 항목을 조정할 때 유용하게 사용할 수 있다. 하지만 여러 배포 환경에 맞춰 서로 다른 프로퍼티를 구성해야 한다면 어떻게 해야 할까? 이제 스프링 프로파일을 이용하여 특정 환경에 적합하게 구성하는 방법을 알아보자.