com.example.RedisApplication 中方法存储库的参数 0 需要找不到类型的 bean

Parameter 0 of method repositories in com.example.RedisApplication required a bean of type that could not be found

我正在 Spring Boot + Spring Data Redis 上做 POC,参考 https://www.youtube.com/watch?v=_M8xoagybzU&t=231s 并简单地按照教程使用 Spring 引导版本 2.1.0.RELEASE 而不是 2.0.0.RELEASE.

我只是将 Redis 缓存更新到数据库中并收到以下错误。

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method repositories in com.example.RedisApplication required a bean of type 'com.example.RedisApplication$LineItemRepository' that could not be found.


Action:

Consider defining a bean of type 'com.example.RedisApplication$LineItemRepository' in your configuration.

RedisApplication.java

@Log
@SpringBootApplication
public class RedisApplication {

    private ApplicationRunner titleRunner(String title, ApplicationRunner rr) {
        return args -> {
            log.info(title.toUpperCase() + ":");
            rr.run(args);
        };
    }

    @Bean
    ApplicationRunner geography(RedisTemplate<String, String> rt) {
        return titleRunner("geography", args -> {
            GeoOperations<String, String> geo = rt.opsForGeo();
            geo.add("Sicily", new Point(13.361389, 38.155556), "Arigento");
            geo.add("Sicily", new Point(15.087269, 37.502669), "Catania");
            geo.add("Sicily", new Point(13.583333, 37.316667), "Palermo");

            Circle circle = new Circle(new Point(13.583333, 37.316667),
                    new Distance(100, RedisGeoCommands.DistanceUnit.KILOMETERS));
            GeoResults<GeoLocation<String>> radius = geo.radius("Sicily", circle);
            radius.getContent().forEach(c -> log.info(c.toString()));
        });
    }

    @Bean
    ApplicationRunner repositories(LineItemRepository  lineItemRepository) {
        return titleRunner("repositories", args -> {
            Long orderId = generateId();

            List<LineItem> itemsList = Arrays.asList(
                    new LineItem(orderId, generateId(), "plunger"),
                    new LineItem(orderId, generateId(), "soup"), 
                    new LineItem(orderId, generateId(), "cofee mug"));

            itemsList.stream().map( lineItemRepository::save).forEach(li -> log.info(li.toString()));
        });
    }

    private Long generateId() {
        long tmp = new Random().nextLong();
        return Math.max(tmp, tmp * -1);
    }

    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class, args);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @RedisHash("orders")
    public class Order implements Serializable {
        @Id
        private Long Id;

        @Indexed
        private Date when;

        @Reference
        private List<LineItem> lineItems;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @RedisHash("lineItems")
    public class LineItem implements Serializable {
        @Indexed
        private Long orderId;

        @Id
        private Long id;

        private String description;
    }

    interface LineItemRepository extends CrudRepository<LineItem, Long> {

    }

    interface OrderRepository extends CrudRepository<Order, Long> {
        Collection<Order> findByWhen(Date date);
    }
}

我能够解决这个问题。 Order 和 LineItem 的存储库参考不知何故不可用。所以我只是将 POJO 和 Repository 类 移出了 main 方法。

RedisApplication.java

@Log
@SpringBootApplication
public class RedisApplication {

    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private LineItemRepository lineItemRepository;

    private ApplicationRunner titleRunner(String title, ApplicationRunner rr) {
        return args -> {
            log.info(title.toUpperCase() + ":");
            rr.run(args);
        };
    }

    @Bean
    ApplicationRunner geography(RedisTemplate<String, String> rt) {
        return titleRunner("geography", args -> {
            GeoOperations<String, String> geo = rt.opsForGeo();
            geo.add("Sicily", new Point(13.361389, 38.155556), "Arigento");
            geo.add("Sicily", new Point(15.087269, 37.502669), "Catania");
            geo.add("Sicily", new Point(13.583333, 37.316667), "Palermo");

            Circle circle = new Circle(new Point(13.583333, 37.316667),
                    new Distance(100, RedisGeoCommands.DistanceUnit.KILOMETERS));

            GeoResults<GeoLocation<String>> radius = geo.radius("Sicily", circle);
            radius.getContent().forEach(c -> log.info(c.toString()));
        });
    }

    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class, args);
    }



    @Bean
    ApplicationRunner repositories() {
        return titleRunner("repositories", args -> {
            Long orderId = generateId();

            List<LineItem> itemsList = Arrays.asList(
                        new LineItem(orderId, generateId(), "plunger"),
                        new LineItem(orderId, generateId(), "soup"), 
                        new LineItem(orderId, generateId(), "cofee mug"));

            itemsList.stream().map(lineItemRepository::save).forEach(li -> log.info(li.toString()));

            Order order = new Order(orderId, new Date(), itemsList);
            orderRepository.save(order);

            Collection<Order> found = orderRepository.findByWhen(order.getWhen());
            found.forEach(o -> log.info("found : " + o.toString()));
        });
    }

    private Long generateId() {
        long tmp = new Random().nextLong();
        return Math.max(tmp, tmp * -1);
    }
}

这是输出 -

2018-11-08 12:25:23.469  INFO 4380 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-08 12:25:23.473  INFO 4380 --- [           main] com.example.RedisApplication             : Started RedisApplication in 4.272 seconds (JVM running for 5.071)
2018-11-08 12:25:23.473  INFO 4380 --- [           main] com.example.RedisApplication             : GEOGRAPHY:
2018-11-08 12:25:23.497  INFO 4380 --- [           main] com.example.RedisApplication             : GeoResult [content: RedisGeoCommands.GeoLocation(name=Palermo, point=null), distance: 0.0, ]
2018-11-08 12:25:23.497  INFO 4380 --- [           main] com.example.RedisApplication             : GeoResult [content: RedisGeoCommands.GeoLocation(name=Arigento, point=null), distance: 0.0, ]
2018-11-08 12:25:23.497  INFO 4380 --- [           main] com.example.RedisApplication             : REPOSITORIES:
2018-11-08 12:25:23.581  INFO 4380 --- [           main] com.example.RedisApplication             : LineItem(orderId=3202446132704215083, id=1657375889205537882, description=plunger)
2018-11-08 12:25:23.589  INFO 4380 --- [           main] com.example.RedisApplication             : LineItem(orderId=3202446132704215083, id=5508942963105786137, description=soup)
2018-11-08 12:25:23.593  INFO 4380 --- [           main] com.example.RedisApplication             : LineItem(orderId=3202446132704215083, id=2166030250698224804, description=cofee mug)
2018-11-08 12:25:23.665  INFO 4380 --- [           main] com.example.RedisApplication             : found : Order(Id=3202446132704215083, when=Thu Nov 08 12:25:23 IST 2018, lineItems=[LineItem(orderId=3202446132704215083, id=1657375889205537882, description=plunger), LineItem(orderId=3202446132704215083, id=5508942963105786137, description=soup), LineItem(orderId=3202446132704215083, id=2166030250698224804, description=cofee mug)])