开发时有时候会在工具类中注入service,但是如果直接用@Autowired注入的话,则无法在静态方法中使用,所以需要换一种方式
1. 使用@Component注解
@Component
public class CommonUtils {
}
2. 注入service
@Autowired
private UserInterfaceService userInterfaceService;
3. 初始化静态service
private static UserInterfaceService userService;
@PostConstruct
public void init() {
if (userService == null) {
setDateSource(userInterfaceService);
}
}
private synchronized static void setDateSource(UserInterfaceService userInterfaceService) {
userService = userInterfaceService;
}