在Spring框架中,我们可以通过@Value和@Resource/@Autoware(@Resource/@Autoware注入带@ConfigurationProperties的配置类)获取/注入需要的配置。然而,由于工具类方法通常是静态的,而Spring的依赖注入机制是基于非静态Bean的,因此不能直接在工具类的静态方法中使用@Value和@Resource/@Autoware获取/注入的配置信息。想必你还是想直接优雅地使用Util.methodName的方式来调用工具类方法吧?那我们应该如何做呢?
通过单例模式,可以确保类只有一个实例,并在静态方法中通过这个实例访问非静态属性。具体实现步骤如下:
在配置文件中添加配置信息
# 在静态方法中使用非静态属性相关配置
test.use-nonstatic-property-in-static-method.stringValue=测试成功!
test.use-nonstatic-property-in-static-method.integerValue=1
test.use-nonstatic-property-in-static-method.booleanValue=true
创建Config配置类
package priv.fc.common_parent.learn.others.use_nonstatic_property_in_static_method;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value = "classpath:application.properties", encoding = "UTF-8")
@ConfigurationProperties(prefix = "test.use-nonstatic-property-in-static-method")
@Data
public class UseNonstaticPropertyInStaticMethodConfig {
private String stringValue;
private Integer integerValue;
private Boolean booleanValue;
}
创建工具类
创建测试通过@Value获取配置的工具类
package priv.fc.common_parent.learn.others.use_nonstatic_property_in_static_method;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 在静态方法中使用非静态属性的工具类
*
* @author 付聪
* @time 2025-01-23 15:31:19
*/
// 将工具类定义为Spring管理的Bean
@Component
public class UseNonstaticPropertyInStaticMethodAtValueUtil {
@Value("${test.use-nonstatic-property-in-static-method.stringValue}")
private String stringValue;
@Value("${test.use-nonstatic-property-in-static-method.integerValue}")
private Integer integerValue;
@Value("${test.use-nonstatic-property-in-static-method.booleanValue}")
private Boolean booleanValue;
// 单例模式(定义了一个私有的静态变量,用于保存类的唯一实例)
private static UseNonstaticPropertyInStaticMethodAtValueUtil instance;
// 构造函数UseNonstaticPropertyInStaticMethodAtValueUtil()中,将当前实例赋值给instance,确保类创建时只有一个实例存在。
public UseNonstaticPropertyInStaticMethodAtValueUtil() {
instance = this;
}
// 静态方法,获取配置值
public static String getStringValue() {
return instance.stringValue;
}
public static Integer getIntegerValue() {
return instance.integerValue;
}
public static Boolean getBooleanValue() {
return instance.booleanValue;
}
}
创建测试通过@ConfigurationProperties获取配置的工具类
package priv.fc.common_parent.learn.others.use_nonstatic_property_in_static_method;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 在静态方法中使用非静态属性的工具类
*
* @author 付聪
* @time 2025-01-23 15:31:19
*/
// 将工具类定义为Spring管理的Bean
@Component
public class UseNonstaticPropertyInStaticMethodAtConfigurationPropertiesUtil {
@Resource
private UseNonstaticPropertyInStaticMethodConfig config;
// 单例模式(定义了一个私有的静态变量,用于保存类的唯一实例)
private static UseNonstaticPropertyInStaticMethodAtConfigurationPropertiesUtil instance;
// 构造函数UseNonstaticPropertyInStaticMethodAtConfigurationPropertiesUtil()中,将当前实例赋值给instance,确保类创建时只有一个实例存在。
public UseNonstaticPropertyInStaticMethodAtConfigurationPropertiesUtil() {
instance = this;
}
// 静态方法,获取配置值
public static String getStringValue() {
return instance.config.getStringValue();
}
public static Integer getIntegerValue() {
return instance.config.getIntegerValue();
}
public static Boolean getBooleanValue() {
return instance.config.getBooleanValue();
}
}
创建单元测试
package priv.fc.common_parent.main.learn.others.use_nonstatic_property_in_static_method;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import priv.fc.common_parent.common.util.PrintUtil;
import priv.fc.common_parent.learn.others.use_nonstatic_property_in_static_method.UseNonstaticPropertyInStaticMethodAtConfigurationPropertiesUtil;
import priv.fc.common_parent.learn.others.use_nonstatic_property_in_static_method.UseNonstaticPropertyInStaticMethodAtValueUtil;
import priv.fc.common_parent.main.MainApplicationTests;
/**
* 在静态方法中使用非静态属性的测试
*
* @author 付聪
* @time 2025-01-23 14:34:59
*/
public class UseNonstaticPropertyInStaticMethodTest extends MainApplicationTests {
private static final Logger logger = LoggerFactory.getLogger(UseNonstaticPropertyInStaticMethodTest.class);
@Test
public void testAtValue() {
String stringValue = UseNonstaticPropertyInStaticMethodAtValueUtil.getStringValue();
PrintUtil.println("stringValue是:" + stringValue);
Integer integerValue = UseNonstaticPropertyInStaticMethodAtValueUtil.getIntegerValue();
PrintUtil.println("integerValue是:" + integerValue);
Boolean booleanValue = UseNonstaticPropertyInStaticMethodAtValueUtil.getBooleanValue();
PrintUtil.println("booleanValue是:" + booleanValue);
}
@Test
public void testAtConfigurationProperties() {
String stringValue = UseNonstaticPropertyInStaticMethodAtConfigurationPropertiesUtil.getStringValue();
PrintUtil.println("stringValue是:" + stringValue);
Integer integerValue = UseNonstaticPropertyInStaticMethodAtConfigurationPropertiesUtil.getIntegerValue();
PrintUtil.println("integerValue是:" + integerValue);
Boolean booleanValue = UseNonstaticPropertyInStaticMethodAtConfigurationPropertiesUtil.getBooleanValue();
PrintUtil.println("booleanValue是:" + booleanValue);
}
}