Java @2.1.1.1版本中SpringBoot中的值
我在Java @2.1.1.1版本中SpringBoot中的值,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,我在application.properties文件中定义了这个值 tdk.date.format=yyyy-MM-dd'T'HH:mm:ss'.000Z' 我在本课程中使用的: public class TdkDateUtils { private static final Logger LOG = LoggerFactory.getLogger(CryptoDateUtils.class); @Value("${tdk.date.format}") priv
application.properties
文件中定义了这个值
tdk.date.format=yyyy-MM-dd'T'HH:mm:ss'.000Z'
我在本课程中使用的:
public class TdkDateUtils {
private static final Logger LOG = LoggerFactory.getLogger(CryptoDateUtils.class);
@Value("${tdk.date.format}")
private static String tdkDateFormat;
public static boolean afterYesterday2(String strDate) throws ParseException {
LOG.debug("tdkDateFormat -> {} ", tdkDateFormat);
SimpleDateFormat format = new SimpleDateFormat(tdkDateFormat);
Date yesterdayDate = Date.from(Instant.now().minus(1, ChronoUnit.DAYS));
return format.parse(strDate).after(yesterdayDate);
}
}
但是is null
TdkDateUtils
是一个实用类,而afterYesterday2
是一个静态方法。它完全存在于您的应用程序Spring上下文之外,像@Value
这样的注释不会在这里处理
要使
@Value
起作用,您必须创建TdkDateUtils
一个bean,因为只有Spring bean才会由PropertyPlaceHolderConfigure
处理。或者,您可以自己在TdkDateUtils
静态初始化块中读取并设置属性,但这违反了Spring的依赖注入原理。TdkDateUtils
是一个实用程序类,afterYesterday2
是一个静态方法。它完全存在于您的应用程序Spring上下文之外,像@Value
这样的注释不会在这里处理
要使
@Value
起作用,您必须创建TdkDateUtils
一个bean,因为只有Spring bean才会由PropertyPlaceHolderConfigure
处理。或者,您可以自己在TdkDateUtils
静态初始化块中读取并设置属性,但这违背了Spring的依赖注入原理。Spring不允许将值注入静态变量。创建一个非静态setter方法来注入值
private static String tdkDateFormat;
@Value("${tdk.date.format}")
public void setDateFormat(String s){
tdkDateFormat = s;
}
Spring不允许向静态变量中注入值。创建一个非静态setter方法来注入值
private static String tdkDateFormat;
@Value("${tdk.date.format}")
public void setDateFormat(String s){
tdkDateFormat = s;
}
是
TdkDateUtils
acomponent
还是service
type bean??我没有在上面看到任何注释。如果Spring无法扫描类并从中生成bean,Spring将无法向其注入值。我认为您的问题不是由于Spring Boot的版本造成的。是TdkDateUtils
acomponent
还是service
type bean??我没有在上面看到任何注释。如果Spring无法扫描类并从中生成bean,Spring将无法向其注入值。我认为您的问题不是因为Spring Boot的版本。