Inject service with annotation in static tool class, tool class service
It is feasible to directly use the service in the Controller and encapsulate the service as a tool class before calling an error because the service cannot be loaded using the new method. The service loading process must be performed when the server is running.
This is the problem I encountered at the time and the Final Solution conclusion: http://bbs.csdn.net/topics/392162820
This is the solution that someone provided at the time of the question Article address: http://blog.csdn.net/p793049488/article/details/37819121
To solve the problem of using annotations to inject services in static tool classes, there are two points:
1. Declare a static internal object of the tool class in the tool class
2. Use the annotation @ PostConstruct to load the service to the static internal object defined above
For example, the following code focuses on line 29-37:
1 package com. shanshan. bo. utils; 2 3 import java. util. list; 4 5 import javax. annotation. postConstruct; 6 import javax. annotation. resource; 7 8 import org. springframework. stereotype. component; 9 10 import com. shanshan. bo. pojo. ssFundDimBank; 11 import com. shanshan. bo. pojo. ssFundDimCurrencyType; 12 import com. shanshan. bo. pojo. ssbiUser; 13 import com. shanshan. bo. service. ssFundDimBankService; 14 import com. Shanshan. bo. service. ssFundDimCurrencyTypeService; 15 import com. shanshan. bo. service. ssbiUserService; 16 17 @ Component18 public class DataFactoryUtil {19 20 @ Resource21 private SsbiUserService userService; 22 23 @ Resource24 private SsFundDimBankService bankService; 25 26 @ Resource27 private #currencytypeservice; 28 29 private static DataFactoryUtil dataFactoryUtil; 30 31 @ Post Construct32 public void init () {33 dataFactoryUtil = this; 34 dataFactoryUtil. userService = this. userService; 35 dataFactoryUtil. bankService = this. bankService; 36 dataFactoryUtil. currencyTypeService = this. currencyTypeService; 37} 38 39/** 40 * query the user in the database based on the user name and return 41 * Ps: questions about duplicate usernames are not considered for the moment 42 * @ param username43 * @ return User44 */45 public static SsbiUser selectByUsername (String username) {46 if (username! = Null &&! ("". Equals (username) {47 return dataFactoryUtil. userService. selectByUserCode (username); 48} 49 return null; 50} 51 52/** 53 * query all bank data and return 54 */55 public static List <SsFundDimBank> selectAllBank () {56 return dataFactoryUtil. bankService. selectAll (); 57} 58 59/** 60 * query all currency data and return 61 */62 public static List <SsFundDimCurrencyType> selectAllCurrencyType () {63 return dataFactoryUtil. currencyTypeService. selectAll (); 64} 65}