In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
In the actual development, I encountered a common piece of code, several methods need to be dropped, but if you pull it out to write a method, the input parameters are different, so I thought of using generics.
For example, this is a common code:
/ / to save order information, multiple method calls are required: OrderFilterRequest orderFilterRequest = new OrderFilterRequest (); btOrderFilterRequest.setUserId ("1"); btOrderFilterRequest.setStatus ("0"); List orderResultList = orderService.findOrders (orderFilterRequest); Order result = null; if (CollectionUtils.isEmpty (orderResultList)) {Order order = CopierUtils.convert (request, Order.class); order.setRealName (customer.getRealName ()); order.setIdcardNo (customer.getIdcardNo ()); order.setOrderNo ("order" + DateUtil.getYMDHMS ()) Order.setStatus ("0"); order.setCreateTime (new Date ()); order.setUpdateTime (new Date ()); order.setUserId ("1"); result = orderService.createOrder (Order);} else {Order orderResult = orderResultList.get (0); CopierUtils.copy (request, orderResult); orderResult.setUpdateTime (new Date ()); result = orderService.updateOrder (btOrderResult);}
Refine this code into a generic class:
@ Transactional (readOnly = true) @ Componentpublic class OrderRequest {@ Autowired private OrderService orderService; @ Transactional public Order orderInfoSave (T request) {OrderFilterRequest orderFilterRequest = new OrderFilterRequest (); btOrderFilterRequest.setUserId ("1"); btOrderFilterRequest.setStatus ("0"); List orderResultList = orderService.findOrders (orderFilterRequest); Order result = null If (CollectionUtils.isEmpty (orderResultList)) {Order order = CopierUtils.convert (request, Order.class); order.setRealName (customer.getRealName ()); order.setIdcardNo (customer.getIdcardNo ()); order.setOrderNo ("order" + DateUtil.getYMDHMS ()); order.setStatus ("0") Order.setCreateTime (new Date ()); order.setUpdateTime (new Date ()); order.setUserId ("1"); result = orderService.createOrder (Order);} else {Order orderResult = orderResultList.get (0); CopierUtils.copy (request, orderResult) OrderResult.setUpdateTime (new Date ()); result = orderService.updateOrder (btOrderResult);} return result;}}
Here's how I called it at first:
Public void method1 (Request1 request) {OrderRequest orderCreateRequest = new OrderRequest (); Order result = OrderCreateRequest.orderInfoSave (request);} public void method2 (Request2 request) {OrderRequest orderCreateRequest = new OrderRequest (); Order result = OrderCreateRequest.orderInfoSave (request);}
But orderService in OrderRequest is null. (injecting service interface into controller layer, injecting orderService into service layer is valuable), try many solutions, and finally find that the original spring custom class instantiation also needs to be instantiated by injection, not new, otherwise it is out of the management of spring. Just change it to the following way:
@ Autowiredprivate OrderRequest request1;@Autowiredprivate OrderRequest request2;public void method1 (Request1 request) {Order result = request1.orderInfoSave (request);} public void method2 (Request2 request) {Order result = request2.orderInfoSave (request);}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.