Spring MVC Spring @Value注解总是求值为null?

mwyxok5s  于 2023-05-23  发布在  Spring
关注(0)|答案(8)|浏览(124)

因此,我有一个简单的属性文件,其中包含以下条目:

my.value=123
another.value=hello world

这个属性文件是使用PropertyPlaceHolderConfigurer加载的,它引用上面的属性文件。
我有下面的类,我试图加载这些属性,就像这样:

public class Config
{
    @Value("${my.value}")
    private String mValue;

    @Value("${another.value}")
    private String mAnotherValue;

    // More below...
}

问题是,mValuemAnotherValue总是空的。然而在我的控制器中,值被加载得很好。怎么回事?

vpfxa7rd

vpfxa7rd1#

如果Config的示例是通过new手动示例化的,那么Spring不会参与其中,因此注解将被忽略。
如果您不能更改您的代码以使Spring示例化bean(可能使用prototype范围的bean),那么另一个选择是使用Spring的load-time classloader weaving功能(参见文档)。这是一些低级的AOP,它允许你像往常一样示例化对象,但是Spring会通过应用程序上下文传递它们,让它们连接起来,配置,初始化等等。
不过,它并不适用于所有平台,所以请阅读上面的文档链接,看看它是否适用于您。

syqv5f0l

syqv5f0l2#

我也有类似的问题,但我是Spring的新手。我试图将属性加载到@Service中,并尝试使用@Value来检索属性值...

@Autowired
public @Value("#{myProperties['myValue']}") String myValue;

我花了一整天的时间尝试各种注解组合,但它总是返回null。最后,答案总是显而易见的。
1)确保Spring正在扫描你的类中的注解,方法是在你的servlet.xml中包含包hierachy(它将扫描你插入的基值以下的所有内容。
2)确保您没有“新建”您刚刚告诉Spring查看的类。相反,您可以在@Controller类中使用@Autowire。
Spring中的一切都是Singleton,发生的事情是Spring将值加载到它的Singleton中,然后我'new'了类的另一个示例,它不包含新加载的值,所以它总是null。
在@Controller中使用...

@Autowired
private MyService service;

调试中...我做的一件事是发现这是延长我的服务如下...

@Service
public class MyService implements InitializingBean

然后将debug语句放入。。

@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
LOGGER.debug("property myValue:" + myValue);        
}

这里我可以看到初始化时设置的值,后来当我在一个方法中打印它时,它是null,所以这对我来说是一个很好的线索,它不是同一个示例。
此错误的另一个线索是Tomcat抱怨尝试从Socket读取时超时,无法解析HTTPheader...这是因为Spring已经创建了服务的一个示例,我也是,所以我的示例正在做真实的的工作,而Spring在其示例上超时。

7ajki6be

7ajki6be3#

请看我的回答here
我遇到了相同的症状(@Value-注解字段为null),但有不同的潜在问题:
import com.google.api.client.util.Value;
确保导入正确的@Value注解类!特别是现在IDE的便利性,这是一个非常容易犯的错误(我使用IntelliJ,如果你自动导入太快而没有阅读你正在自动导入的内容,你可能会像我一样浪费几个小时)。
正确的导入是:
org.springframework.beans.factory.annotation.Value

ubbxdtey

ubbxdtey4#

因为它使用@Controller,所以看起来你是在自己示例化Config。让Spring示例化它。

lnlaulya

lnlaulya5#

您还可以使您的属性为private,确保您的类是使用@Service@Component注解的Spring bean,以便始终示例化,最后添加使用@Value注解的setter方法。这样可以确保您的属性将被分配到application.properties或yml配置文件中指定的值。

@Service
public class Config {
    private static String myProperty;

    private static String myOtherProperty;

    @Value("${my.value}")    
    public void setMyProperty(String myValue) {
    this.myProperty = myValue;}

    @Value("${other.value}")
    public void setMyOtherProperty(String otherValue) {
    this.myOtherProperty = otherValue;}

    //rest of your code...
}
cidc1ykv

cidc1ykv6#

<context:spring-configured />添加到应用程序上下文文件中。
然后将@Configurable注解添加到Config类。

brtdzjyr

brtdzjyr7#

在我的单元测试中,executeScenarioRequest总是为null

@RunWith(SpringRunner.class)
@ExtendWith(MockitoExtension.class)
class ScenarioServiceTestOld {

    @Value("classpath:scenario/SampleScenario.json")
    Resource executeScenarioRequest;

@ExtendWith(MockitoExtension.class)更改为@ExtendWith(SpringExtension.class)

fkaflof6

fkaflof68#

尝试确保Bean上要由@Value示例化的字段是NOT static(而是instance)。我认为Annotations工作所需的与Spring Context加载有关的事情,在static initialization的时候不会发生(而是需要将一个示例加载到Spring Context中)。
这一点:

@Value("${my.property.name}")
String myProperty

不是这个:

@Value("${my.property.name}")
static String myProperty

相关问题