Autowired injection to NULL in spring custom generic class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.