Search code examples
javaredisjedisspring-data-redis

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


I am doing POC on Spring Boot + Spring Data Redis by taking reference from https://www.youtube.com/watch?v=_M8xoagybzU&t=231s and simply following the tutorial using Spring Boot version 2.1.0.RELEASE instead of 2.0.0.RELEASE.

I am simply updating the Redis cache into DB and getting the below error.

***************************
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);
    }
}

Solution

  • I was able to solve this issue. Somehow repository reference of Order and LineItem was not available. So I simply moved the POJO and Repository classes out of main method.

    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);
        }
    }
    

    Here is the output -

    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)])