(七)Spring Boot配置篇 之 应用程序 Bean 的配置外置
package readinglist; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; 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("/") @ConfigurationProperties(prefix = "amazon") public class ReadingListController { private String associateId; private ReadingListRepository readingListRepository; @Autowired public ReadingListController( ReadingListRepository readingListRepository) { this.readingListRepository = readingListRepository; } public void setAssociateId(String associateId) { this.associateId = associateId; } @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); model.addAttribute("amazonID", associateId); } return "readingList"; } @RequestMapping(method = RequestMethod.POST) public String addToReadingList(Reader reader, Book book) { book.setReader(reader); readingListRepository.save(book); return "redirect:/"; } }
如你所见,ReadingListController现在有了一个associateId属性,还有对应的setAssociateId()方法,用它可以设置该属性。readersBooks()现在能通过amazonID这个键把associateId的值放入模型。棒极了!现在就剩一个问题了——从哪里能取到associateId的值。请注意,ReadingListController上加了@ConfigurationProperties注解,这说明该Bean的属性应该是(通过setter方法)从配置属性值注入的。说得更具体一点,prefix属性说明ReadingListController应该注入带amazon前缀的属性。综合起来,我们指定ReadingListController的属性应该从带amazon前缀的配置属性中进行注入。ReadingListController只有一个setter方法,就是设置associateId属性用的setter方法。因此,设置Amazon Associate ID唯一要做的就是添加amazon.associateId属性,把它加入支持的任一属性源位置里即可。
package readinglist; import org.springframework.boot.context.properties. ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties("amazon") public class AmazonProperties { private String associateId; public void setAssociateId(String associateId) { this.associateId = associateId; } public String getAssociateId() { return associateId; } }
有了加载 amazon.associateId 配置属性的 AmazonProperties 后,我们可以调整ReadingListController(如代码清单3-6所示),让它从注入的AmazonProperties中获取Amazon Associate ID。
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) { 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); 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不再直接加载配置属性,转而通过注入其中的AmazonProperties Bean来获取所需的信息。如你所见,配置属性在调优方面十分有用,这里说的调优不仅涵盖了自动配置的组件,还包括注入自有应用程序Bean的细节。但如果我们想为不同的部署环境配置不同的属性又该怎么办?让我们看看如何使用Spring的Profile来设置特定环境的配置。