How to use Service (Spring Framework) in the static method of Util class

Preface

Before discussing the topic of this article, we need to know a few prerequisites:

  1. The methods of the Util class are generally of static type, which are convenient for users to call, such as:
    ObjectUtil.isNotEmpty() HttpUtil.sendPost().
  2. When using Service in the Spring framework, we generally use the automatic injection mechanism that comes with the framework, allowing Spring to help us manage the creation and destruction of Bean objects.
  3. Static type methods are 类名.方法名()called directly, not using objects of the class.

analyze

Regarding the question of how to use Service in the static method of the Util class , as a CV warrior , the first impression may be that you want to use automatic injection annotations similar to the Controller layer, such as: , @Autowired, @AllArgsConstructorbut we found that the automatic injection of the code does not take effect at runtime. , report a null pointer exception. In other words, automatic injection annotations do not take effect on static type variables.

In this case, I will write a static type of Service to occupy a pit, and do not automatically inject it first.

// 企微通用工具
// 使用@Component注解表示该类的实例化由Spring管理
public class WeWorkUtil {
    
    
	// 企微相关参数
    public static MessagePushWeWorkProperties weWorkProperties;
    // 企微发送消息的方法
    public static boolean sendMsg(String userId, String content) {
    
    
		//此处为方法逻辑,需要调用静态成员变量
		String xxx = weWorkProperties.get("xxx");	
	}
}

If this tool class remains like this, it has nothing to do with Spring. Because this class does not indicate that Spring is required for management, we need to add an annotation to the class and use the @ComponentSpring framework to complete the subsequent variable injection.

Method 1: Use Autowire

Add a member method so that static member variables can be assigned values ​​normally.

// 使用@Component注解表示该类的实例化由Spring管理
@Component
public class WeWorkUtil {
    
    
	// 静态成员变量
    public static MessagePushWeWorkProperties weWorkProperties;
    // 使用注解修饰方法
    @Autowired
    public void setWeWorkProperties(MessagePushWeWorkProperties weWorkProperties) {
    
    
        WeWorkUtil.weWorkProperties = weWorkProperties;
    }
    // 企微发送消息的方法
    public static boolean sendMsg(String userId, String content) {
    
    
		//此处为方法逻辑,需要调用静态成员变量
		String xxx = weWorkProperties.get("xxx");	
	}
}

Here we add annotations to a method @Autowiredso that the method can be executed when the container initializes the bean, and the input parameter type is declared as the type of the static member variable, because Spring will automatically inject the input parameters of the method, then inside the method we can Assign the created input parameter variable to the static member variable to complete the assignment of the static member variable.

Method 2: Use static code blocks

private static IDictService dictService;

	static {
    
    
		dictService = SpringUtil.getBean(IDictService.class);
}

@Component
public class WeWorkUtil {
    
    
	// 静态成员变量
    public static MessagePushWeWorkProperties weWorkProperties;
    // 使用静态代码块及springUtil获取对象
    static {
    
    
		weWorkProperties= SpringUtil.getBean(MessagePushWeWorkProperties .class);
    }
    
    // 企微发送消息的方法
    public static boolean sendMsg(String userId, String content) {
    
    
		//此处为方法逻辑,需要调用静态成员变量
		String xxx = weWorkProperties.get("xxx");	
	}
}

Reference article

[spring container startup] Bean instantiation and initialization

Guess you like

Origin blog.csdn.net/Ka__ze/article/details/132531955