猿问

适应DRY原则的方式

我有一个网络抓取应用程序,它可以简单地从两家大书店抓取书籍。这个想法是用户在 URL 中放置类别类型,例如/romances,/biographies。


控制器:_


package bookstore.scraper.controller;


imports...


import java.util.List;

import java.util.Map;


@RestController

public class BookController {


    private final MostPreciseBookService mostPreciseBookService;

    private final CategorizedBookService categorizedBookService;

    private final BestSellersService bestSellersService;


    @Autowired

    public BookController(MostPreciseBookService bookOperationsService, CategorizedBookService categorizedBookService, BestSellersService bestSellersService) {

        this.mostPreciseBookService = bookOperationsService;

        this.categorizedBookService = categorizedBookService;

        this.bestSellersService = bestSellersService;

    }

    

.....



    @GetMapping("/romances")

    public Map<Bookstore, List<Book>> get15RomanticBooks() {

        return categorizedBookService.get15BooksFromRomanceCategory();

    }


    @GetMapping("/biographies")

    public Map<Bookstore, List<Book>> get15BiographiesBooks() {

        return categorizedBookService.get15BooksFromBiographiesCategory();

    }


    @GetMapping("/guides")

    public Map<Bookstore, List<Book>> get15GuidesBooks() {

        return categorizedBookService.get15BooksFromGuidesCategory();

    }


    @GetMapping("/fantasy")

    public Map<Bookstore, List<Book>> get15FantasyBooks() {

        return categorizedBookService.get15BooksFromFantasyCategory();

    }


    @GetMapping("/crime")

    public Map<Bookstore, List<Book>> get15CrimeBooks() {

        return categorizedBookService.get15BooksFromCrimeCategory();

    }

}


偶然的你
浏览 107回答 1
1回答

慕尼黑5688855

问题到底。在服务中使用那么多@Value 注释是好方法吗?/(使用 yml 文件存储 URL)。这不是因为您注入了太细粒度的属性。这些应该被封装到一个特定的对象中:更具可读性、可维护性和可测试性。Spring 提供@ConfigurationProperties这样做。它必须注释包含属性的类。你可以这样做:@Component@ConfigurationProperties("external.library.url")public class BookStoreUrlProperties {&nbsp; &nbsp; private Empik empik = new Empik();&nbsp; &nbsp; private Merlin merlin = new Merlin();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // getters/setters&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public BookStoreUrlProperties() {&nbsp; &nbsp; }&nbsp; &nbsp; public static class Empik {&nbsp; &nbsp; &nbsp; &nbsp; private String romances;&nbsp; &nbsp; &nbsp; &nbsp; private String biographies;&nbsp; &nbsp; &nbsp; &nbsp; private String crime;&nbsp; &nbsp; &nbsp; &nbsp; private String guides;&nbsp; &nbsp; &nbsp; &nbsp; private String fantasy;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // getters/setters&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public static class Merlin {&nbsp; &nbsp; &nbsp; &nbsp; private String romances;&nbsp; &nbsp; &nbsp; &nbsp; private String biographies;&nbsp; &nbsp; &nbsp; &nbsp; private String crime;&nbsp; &nbsp; &nbsp; &nbsp; private String guides;&nbsp; &nbsp; &nbsp; &nbsp; private String fantasy;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // getters/setters&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}然后像任何其他 bean 一样注入这个 bean:@AutowiredBookStoreUrlProperties bookStoreUrlProperties;并使用 getter 检索 url,例如:String RomanceUrl = bookStoreUrlProperties.getMerlin().getRomances();我建议的另一种选择是将属性(merlin 和 empik)拆分为两个属性类。关于服务类中的重复,您可以通过提取参数重构轻松地将它们分解出来,因为唯一的区别是 URL 值。例如 :public Map<Bookstore, List<Book>> get15BooksFromGuidesCategory() {&nbsp; &nbsp;return get15BooksFrom(guidesCategoryEmpikURL, guidesCategoryMerlinURL)}public Map<Bookstore, List<Book>> get15BooksFromBiographiesCategory() {&nbsp; &nbsp;return get15BooksFrom(biographiesCategoryEmpikURL, biographiesCategoryMerlinURL)}public Map<Bookstore, List<Book>> get15BooksFrom(String bookStoreEmpikURL, String bookStoreMerlinURL) {&nbsp; &nbsp; bookstoreWith15CategorizedBooks.put(Bookstore.EMPIK, empikBookService&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .get15BooksFromCategory(connect(bookStoreEmpikURL)));&nbsp; &nbsp; bookstoreWith15CategorizedBooks.put(Bookstore.MERLIN, merlinFetchingBookService&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .get15BooksFromCategory(connect(bookStoreMerlinURL)));&nbsp; &nbsp; return bookstoreWith15CategorizedBooks;}
随时随地看视频慕课网APP

相关分类

Java
我要回答