【发布时间】:2023-03-18 13:44:01
【问题描述】:
我在 spring 中使用 @value 将值从属性加载到我的 java 常量中。
但是我得到 null 作为常量中的值。帮帮我。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd" >
<context:component-scan base-package="com.epro.aemcrmsync" />
<context:annotation-config />
<context:property-placeholder location="classpath:com/epro/aemcrmsync/properties/config.properties" />
</beans>
类:
package com.epro.aemcrmsync.constants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConfigConstants {
@Value("$(field_name}")
public static String USER_ID;
}
【问题讨论】:
-
如答案中所述,您不能使用静态字段,其次您应该使用
@Value("${field_name}")而不是@Value("$(field_name}")。 -
仍然只获得空值。正在使用球衣实现可能看起来有问题,对吗?即使我向 xml 注入了价值,但它不起作用的另一件事是我没有在 xml 中添加 dispatcherservlet 是否可以帮助我。
-
我没用过 Jersey 所以不知道是什么问题
-
感谢朋友问题已解决
-
你是怎么解决的?也许你应该回答你自己的问题
标签: java spring model-view-controller null