5

Я вынес все данные в properties, например, с такими полями:

email.content.charset=utf-8

Я создал отдельный класс с пропертями, ResourcesProperties, в котором у меня есть это поле и геттер:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ResourcesProperties {

    @Value("${email.content.charset}")
    private String emailCharset;

    public String getEmailCharset() {
        return emailCharset;
    }
}

Проперти-файл сканируется спрингом:

@PropertySource({"classpath:/properties/mail.properties"})

Я автовайрю класс проперти в нужный мне класс:

@Autowired
private ResourcesProperties properties;

и вывожу на экран:

System.out.println(properties.getEmailCharset());

Результат:

${email.content.charset}

Делаю то же самое через Environment:

System.out.println(environment.getProperty("email.content.charset"));

Результат:

utf-8

Подскажите, что я делаю не так?

1 ответ 1

7

Добавьте в ваш класс ResourcesProperties след код.

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConf() {
    return new PropertySourcesPlaceholderConfigurer();
}

Если не инициализировать PropertySourcesPlaceholderConfigurer то считыватся через аннотацию property не будет

1

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.