Spring Learning notes--about spring annotations scanning cannot inject new object issues

Source: Internet
Author: User

These days leader let me use the Factory mode to reconstruct part of the business logic code, the process is painful (here is not detailed), the results are very sweet. Here's a note that I encountered a problem during the refactoring process.
Some of the code is as follows:

@service(orderfactory) Public  class orderfactory implements iorderfactory{     PublicOrdercreateFactory(String type) {if(Type! =NULL&& Type.indexof ("1")! = -1){return NewOrderOfType1 (); }    }}@service Public  class OrderOfType1{    @Autowired     PublicOrderdao Orderdao;//At this time the Orderdao is empty and spring is not injected .}

After I found this problem I first looked at whether OrderOfType1 was in the spring container, then @service custom aliases, using @qualifier injection, and so on. I suddenly found that I used the new keyword.
It turns out that after using new, the OrderOfType1 class is not managed by spring, and spring annotations are scanned when spring is instantiated, and if the new object is clearly not managed by spring after the spring instantiation is complete. So you can use the following methods:

@service(orderFactory)publicclass OrderFactory implements IOrderFactory{    @Autowried    private OrderOfType1 orderOfType1;    publicfactory(String type){        ifnull && type.indexOf("1")! = -1){            return orderOfType1();        }    }}

The problem is resolved after the modification.

Spring Learning notes--about spring annotations scanning cannot inject new object issues

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.