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

Spring 5.0.5中@Value无法解析为正确类型问题求助

Fixing Spring 5 TypeMismatchException When Replacing XML with PropertiesConfig

Hey there, let's work through this TypeMismatch issue you're facing after refactoring to use a PropertiesConfig class instead of applicationContext.xml for property placeholders. The error about converting a String to int for secureExpirationHours usually boils down to a few common misconfigurations—let's break them down step by step:

1. Double-Check Your Property Value

First, verify the value in your properties file (like application.properties) for secure.expiration.hours:

  • Make sure it's a pure integer with no extra characters (spaces, letters, symbols). For example, secure.expiration.hours= 24 (with leading/trailing spaces) or secure.expiration.hours=twenty will fail the conversion.
  • Ensure the value isn't empty or missing entirely—if it's missing, Spring might inject the placeholder string ${secure.expiration.hours} directly, which obviously can't be converted to int.

2. Validate Your PropertiesConfig Setup

When using JavaConfig for property placeholders, you need to explicitly declare a PropertySourcesPlaceholderConfigurer bean—this is easy to forget, and without it, Spring might not resolve your placeholders correctly. Here's the correct setup:

@Configuration
@PropertySource("classpath:application.properties") // Confirm the path is correct!
public class PropertiesConfig {

    // This bean must be static to ensure it's initialized early
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        // Optional: Set encoding if your properties file uses non-ASCII characters
        // configurer.setFileEncoding("UTF-8");
        return configurer;
    }
}
  • Double-check the @PropertySource path: if your properties file is in a subdirectory (like src/main/resources/config/application.properties), update the path to match.

3. Check Your Field Injection

Ensure the field in your target class is properly annotated and typed:

  • Use @Value with the correct property key:
    @Component
    public class YourSecureService {
    
        // Use int only if you're 100% sure the property will always exist and be an integer
        @Value("${secure.expiration.hours}")
        private int secureExpirationHours;
    
        // Or use Integer with a default value to handle missing properties gracefully
        // @Value("${secure.expiration.hours:24}")
        // private Integer secureExpirationHours;
    
        // ... rest of your class code
    }
    
  • If the property might be missing or nullable, use Integer instead of int—primitive types can't handle null values, which will throw an error if the property isn't found.

4. Rule Out Property Overrides

Sometimes, system environment variables or command-line arguments can override your properties file values. Check if there's a system variable named secure.expiration.hours (or its uppercase/snake_case equivalent, like SECURE_EXPIRATION_HOURS) that's set to a non-integer value. Spring prioritizes environment variables over properties files, so this could be the culprit.

Quick Troubleshooting Tip

If you're still stuck, add a debug log to print the raw value being injected. For example, temporarily change the field type to String and log it:

@Value("${secure.expiration.hours}")
private String tempValue;

@PostConstruct
public void debugValue() {
    System.out.println("Raw value for secureExpirationHours: '" + tempValue + "'");
}

This will show you exactly what's being injected—if it's the placeholder string itself, your PropertySourcesPlaceholderConfigurer is missing or misconfigured. If it's a non-integer string, you know to fix the property source.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:56:52