我正在 Spring Boot + Spring Data Redis 上进行POC,方法是参考 https://www.youtube.com/watch?v=_M8xoagybzU&t=231s ,并使用Spring Boot版本{{1} },而不是2.1.0.RELEASE
。
我只是将Redis缓存更新到数据库中,并出现以下错误。
2.0.0.RELEASE
RedisApplication.java
***************************
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.
答案 0 :(得分:1)
我能够解决此问题。某种程度上,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)])