I encountered in the actual development of a piece of public code, a few methods need to be dropped, but if the individual pull out to write a method, the arguments are different, so think of the generic type.
For example, this is a public code:
To save the order information, more than one method call Orderfilterrequest orderfilterrequest = new orderfilterrequest () is required; Btorderfilterrequest.setuserid ("1"); Btorderfilterrequest.setstatus ("0"); List<order> 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);}
The
refines this code into a generic class:
@Transactional (readonly = true) @Componentpublic class orderrequest<t>{@ autowiredprivate orderservice orderservice; @Transactionalpublic order orderinfosave (T request) {orderfilterrequest orderfilterrequest = new orderfilterrequest (); Btorderfilterrequest.setuserid ("1"); btorderfilterrequest.setstatus ("0"); List<Order> 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); &Nbsp; orderresult.setupdatetime (New date ()); result = orderservice.updateorder (Btorderresult); } return result;}}
The first thing I did was call this:
public void Method1 (Request1 request) {orderrequest<request1> ordercreaterequest = new Orderrequest<request 1> (); Order result = Ordercreaterequest.orderinfosave (request);} public void Method2 (Request2 request) {orderrequest<request2> ordercreaterequest = new Orderrequest<request 2> (); Order result = Ordercreaterequest.orderinfosave (request);}
However, OrderService in orderrequest is null. ( injecting the service interface into the controller layer, injecting orderservice into the service layer is a value ), trying a lot of workarounds, and finally discovering that the original spring custom class instantiation needs to be injected , you cannot use new, or you are out of spring management . Change it to the following way:
@Autowiredprivate orderrequest<request1> request1; @Autowiredprivate orderrequest<request2> request2; public void Method1 (Request1 request) {Order result = Request1.orderinfosave (request);} public void Method2 (Request2 request) {Order result = Request2.orderinfosave (request);}
Autowired injection to NULL in spring custom generic class