You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring中@Value注入配置值异常,availableHumanAgents始终为0求助

问题描述

我尝试从application.properties或环境变量中读取配置值human.agents.available,注入到HumanAgentSocket类的availableHumanAgents变量中,但该变量始终为0,无法获取配置文件中指定的预期值。

相关代码:

public class HumanAgentSocket extends TextWebSocketHandler {
      
    @Value("${human.agents.available}")
    private int availableHumanAgents;
    private List<WebSocketSession> sessions = new ArrayList<>();
    private List<String> sessionIds = new ArrayList<>();
    private HashMap<WebSocketSession,WebSocketSession> socketConnections = new HashMap<>();
    private int noOfHumanAgents = 1;
    private ObjectMapper objectMapper = new ObjectMapper();
    
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws IOException {
       if (noOfHumanAgents<=5){
           System.out.println("Agents "+availableHumanAgents);
           sessions.add(session);
        sessionIds.add(session.getId());
        System.out.println("List "+sessionIds);
        if(sessions.size() == 2){
            socketConnections.put(sessions.get(0),sessions.get(1));
            socketConnections.put(sessions.get(1),sessions.get(0));
            System.out.println("SocketConnections "+socketConnections);
            sessions.clear();
            noOfHumanAgents++;
            System.out.println("Sessios "+sessions.size());
        }
    }else {
        throw  new HumanAgentException("agents are busy");
    }
}

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    WebSocketSession webSocketSession = socketConnections.get(session);
    String payload = message.getPayload();
    JsonNode jsonNode = objectMapper.readTree(payload);
    String content = jsonNode.get("message").asText();
    if (content.equalsIgnoreCase("CLOSE")){
        afterConnectionClosed(session,null);
    }
    if (webSocketSession.isOpen()){
        webSocketSession.sendMessage(message);
    }
}

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
    WebSocketSession humanAgentSession = socketConnections.get(session);
    if(humanAgentSession!=null){
        sessions.add(humanAgentSession);
    }
    socketConnections.remove(session);
    socketConnections.remove(humanAgentSession);
}

public String getWebSocketSession(String sessionId){
    System.out.println("List "+sessionIds);
    String webSocketSession = sessionIds.get(Integer.parseInt(sessionId));
    return webSocketSession;
}
}
解决方法
  • 确保类被Spring容器管理:HumanAgentSocket必须是Spring Bean,否则@Value注解不会生效。在类上添加@Component注解,或者在配置类中通过@Bean方法注册该类:

    // 方式1:添加@Component注解
    @Component
    public class HumanAgentSocket extends TextWebSocketHandler {
        // ... 原有代码
    }
    
    // 方式2:在配置类中注册Bean
    @Configuration
    public class WebSocketConfig {
        @Bean
        public HumanAgentSocket humanAgentSocket() {
            return new HumanAgentSocket();
        }
    }
    

    注意:如果是通过new HumanAgentSocket()手动创建实例,Spring不会注入配置值,必须从Spring容器获取实例(比如通过@Autowired注入)。

  • 检查配置文件的正确性:

    1. 确认application.properties文件位于src/main/resources目录下(Spring Boot默认加载路径)。
    2. 配置项拼写完全正确:human.agents.available=你的预期数值,注意大小写、标点符号不能错。
    3. 如果使用多环境配置(如application-dev.properties),确保激活了对应的环境(比如通过spring.profiles.active=dev指定)。
  • 验证配置值的有效性:
    availableHumanAgents是int类型,如果配置文件中没有该配置项、配置值为空或不是合法整数,Spring会使用int的默认值0。所以要确保配置值是有效的整数,比如human.agents.available=5。

  • 检查是否需要指定配置源:
    如果配置值不在默认的application.properties中,而是自定义配置文件,需要在类上添加@PropertySource注解指定配置文件路径:

    @Component
    @PropertySource("classpath:custom-config.properties")
    public class HumanAgentSocket extends TextWebSocketHandler {
        // ... 原有代码
    }
    

内容的提问来源于stack exchange,提问作者Sai Pavirala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 17:35:24