【问题标题】:@Value in spring returns null春天的@Value 返回null
【发布时间】: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


【解决方案1】:

Spring 不处理 static 成员。使该字段成为实例字段。将ConfigConstants bean 注入任何您需要使用它的地方。

【讨论】:

  • 另外,问题中的SpEL不好。
  • 仍然只有 null
  • @sridhar 编辑您的问题并向我们展示您如何使用 bean。
  • 包 com.epro.aemcrmsync.constants;导入 org.springframework.beans.factory.annotation.Value;导入 org.springframework.stereotype.Component; @Component("configConstants") public class ConfigConstants { // @Value("#{config['aem_field_name']}") @Value("${aem_field_name}") public String AEM_USER_ID; }
  • 这是我的服务类 .. @Component(value="userService") @Path("user") public class UserService { @Autowired private ConfigConstants configConstants;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 2014-12-06
  • 2018-02-12
  • 2017-08-12
  • 1970-01-01
相关资源
最近更新 更多