无法使用注解从.properties文件中获取值

45 浏览
0 Comments

无法使用注解从.properties文件中获取值

我想通过.properties文件配置我的bean的String字段。但它没有替换value键的值,意味着它回显"${value}"字符串。我的代码如下:

主类:

public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
        ValuesContainer valuesContainer = (ValuesContainer) applicationContext.getBean("container");
        System.out.println(valuesContainer.getValue()); //回显"${value}"而不是Ho-ho-ho!
    }
}

应用程序上下文:

.....


bean:

package bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("container")
@Scope("singleton")
@PropertySource(value = "classpath:app.properties")
public class ValuesContainer {
    @Value("${value}")
    private String value;
    public String getValue() {
        return value;
    }
}

app.properties:

value = Ho-ho-ho!

0
0 Comments

无法使用注解从.properties文件中提取值的原因是没有正确使用@Configuration和@PropertySource注解,并将其添加到应用程序上下文中。解决方法是创建一个单独的@Configuration类,在其中使用@PropertySource注解,并将其添加到应用程序上下文中,或者通过编程方式配置应用程序上下文的属性源。下面是解决方法的具体步骤:

1. 创建一个新的@Configuration类,并在其中使用@PropertySource注解,指定.properties文件的位置。例如,如果.properties文件名为"config.properties",并位于类路径下的"resources"文件夹中,可以使用以下代码:

@Configuration
@PropertySource("classpath:config.properties")
public class AppConfig {
    
}

2. 将新创建的@Configuration类添加到应用程序上下文中。可以通过在应用程序的主配置类中使用@ComponentScan注解来自动添加@Configuration类,或者通过手动将其添加到应用程序上下文中。例如,可以使用以下代码手动将@Configuration类添加到应用程序上下文中:

@Configuration
@ComponentScan(basePackages = "com.example")
public class ApplicationConfig {
    
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(AppConfig.class);
        context.refresh();
        
        // 其他应用程序逻辑...
    }
}

3. 如果不想使用@Configuration类和@PropertySource注解,可以通过编程方式配置应用程序上下文的属性源。可以使用ConfigurableApplicationContext的getEnvironment()方法来获取应用程序上下文的环境,并使用Environment的getPropertySources()方法来配置属性源。以下是一个示例代码:

public class ApplicationConfig {
    
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        ConfigurableEnvironment environment = context.getEnvironment();
        environment.getPropertySources().addLast(new ResourcePropertySource("classpath:config.properties"));
        context.refresh();
        
        // 其他应用程序逻辑...
    }
}

通过按照上述步骤正确配置应用程序上下文和属性源,就可以使用注解从.properties文件中提取值了。

0
0 Comments

在使用注解时无法从.properties文件中获取值的原因可能是配置中缺少了PropertySourcesPlaceholderConfigurer。下面是解决这个问题的方法:

1. 首先,在你的配置类中添加PropertySourcesPlaceholderConfigurer的Bean定义。

@Configuration
public class AppConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
    // 其他配置代码...
}

2. 然后,确保你的.properties文件在类路径下,并且已经正确配置了。

3. 最后,使用@Value注解来注入.properties文件中的值。

@Component
public class MyComponent {
    @Value("${property.key}")
    private String propertyValue;
    // 其他代码...
}

这样,你就可以成功从.properties文件中获取值并注入到相应的字段中了。如果你仍然遇到问题,可以参考上面提到的链接中的解答,可能会有更具体的解决方案。

0