About tools @Autowired static method call injection service class problem

Here @Component // save annotation not (0) 
. 1 public class NtClient { 2 / ** . 3 * log . 4 * / . 5 static String Private NtClient.class.getName clazzName = (); . 6 / ** . 7 here * to use the service requires injection spring (. 1) . 8 * / . 9 @Autowired 10 Private notifyTimeService notifyTimeService; . 11 Private static NtClient ntClient; 12 is / ** 13 is here noted annotation * (2) * / 14 @PostConstruct 15 public void the init ( ) {
16 = ntClient the this; . 19 / ** . 17 ntClient.notifyTimeService = this.notifyTimeService; 18} 20 * primary usage scenarios (. 3) 21 is * / 22 is public static void insertParam (int ID) { 23 is / ** 24 * Note that calling the methods herein (. 4) 25 * / 26 is IF (ntClient.notifyTimeService.deleteNotifyTime ( ID)) { 27} 28} 29}

Note: (0) at the role of the (2) ntClient = this; this assignment (Note: If there are no notes ntClient null) 
(. 1) at is spring automatically injected, using spring framework is very common, but if in a static method this method calls inject class, found that injection is 'null'; not because spring is not injected, but is static method to 'empty', and not earlier than in static initialization method before thought of a way
   point (2) to think ways: through (Note: @PostConstruct modified method runs when you load Servle server, and the server will only be executed once .PostConstruct executed after the constructor, init before () method performs .PreDestroy () method destroy ( ) after execution of method execution) method public void init () give such assignment, then (1) coming out of the implanter. This does not affect the injection service following the call dao and so on!
   Special method static (3) to be processed at the (classical is a main () method, to think of model-based, it can be used inside a method invocation)
   (4) is that the use of such models is now invoked as a way notifyTimeService of ntClient properties

need to be injected in the Service tools, since the tools are generally static method, it is also a requirement of a static (Service). However, due next Spring / SpringBoot normal circumstances can not support the injection of static properties (null pointer exception will be reported). The main reason is: Spring dependency injection is actually dependent on the method of implanting Set value, Spring-level object-based dependency injection, and static properties / static variables actually belong to a class.
Here @Component // save annotation not (0) 
. 1 public class NtClient { 2 / ** . 3 * log . 4 * / . 5 static String Private NtClient.class.getName clazzName = (); . 6 / ** . 7 here * to use the service requires injection spring (. 1) . 8 * / . 9 @Autowired 10 Private NotifyTimeService notifyTimeService; . 11 Private static NtClient ntClient; 12 is / ** 13 is here noted annotation * (2) * / 14 @PostConstruct 15 public void the init ( ) {
16 = ntClient the this; . 17 ntClient.notifyTimeService = this.notifyTimeService; 18} 19 / ** 20 * primary usage scenarios (. 3) 21 is * / 22 is public static void insertParam (int ID) { 23 is / ** 24 * Note that calling the methods herein (. 4) 25 * / 26 is IF (ntClient. notifyTimeService.deleteNotifyTime (ID)) { 27} 28} 29}

Note: (0) at the role of the (2) ntClient = this; this assignment (Note: If there are no notes ntClient null) 
(. 1) at is spring automatically injected, using spring framework is very common, but if in a static method this method calls inject class, found that injection is 'null'; not because spring is not injected, but is static method to 'empty', and not earlier than in static initialization method before thought of a way
   point (2) to think ways: through (Note: @PostConstruct modified method runs when you load Servle server, and the server will only be executed once .PostConstruct executed after the constructor, init before () method performs .PreDestroy () method destroy ( ) after execution of method execution) method public void init () give such assignment, then (1) coming out of the implanter. This does not affect the injection service following the call dao and so on!
   Special method static (3) to be processed at the (classical is a main () method, to think of model-based, it can be used inside a method invocation)
   (4) is that the use of such models is now invoked as a way notifyTimeService of ntClient properties

need to be injected in the Service tools, since the tools are generally static method, it is also a requirement of a static (Service). However, due next Spring / SpringBoot normal circumstances can not support the injection of static properties (null pointer exception will be reported). The main reason is: Spring dependency injection is actually dependent on the method of implanting Set value, Spring-level object-based dependency injection, and static properties / static variables actually belong to a class.

Guess you like

Origin www.cnblogs.com/jpfss/p/11271473.html