Почему свойство сообщения возвращает значение Null в Spring MVC - PullRequest
0 голосов
/ 08 ноября 2019

Я работаю над проектом, в котором я хочу вывести сообщение . свойства файл. Так что операционная команда может измениться в соответствии с требованием.

Для этого я написал класс конфигурации, подобный этому

@Configuration
@ComponentScan(basePackages = {"lk.ideahub.symphony.modules", "lk.ideahub.symphony.product"})
@PropertySources(value = {@PropertySource(value = {"classpath:messages.properties"})})
public class MessageConfig {
    private static final Logger log = LoggerFactory.getLogger(MessageConfig.class);

    @Bean
    public MessageSource messageSource() {
        log.info("Message properties loading into the project");
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setCacheSeconds(10);
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
}

В свойствах сообщения

axipay.failure.account.already.exist=Sorry, the biller account number is already added.

В моем классе

public Payee myPayees{

//Asking the property into a variable 
private static final String PAYEE_ACCOUNT_ALREADY_EXIST = "axipay.failure.account.already.exist";

@Autowired
Environment environment;

public void myMethod(){
 String message = getEnvironment().getProperty(PAYEE_ACCOUNT_ALREADY_EXIST);
}
}

1 Ответ

0 голосов
/ 08 ноября 2019

вы можете использовать MessageSource Bean, предоставляемый spring https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/MessageSource.html

@Autowired
private MessageSource messageSource;
//use this method to get message
public String getMessage(){        
return messageSource.getMessage("axipay.failure.account.already.exist", null, new Locale("en"));
}
...