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