0

Я пишу web приложение на Java, это много-модульный maven проект. В нем два модуля mmmp-service и mmmp-web. Тот что mmmp-web собирается в war файл. Тот что mmmp-service собирается в jar. Модуль mmmp-web зависит от mmmp-service. В модуле mmmp-service имеется файл свойств application.property, в котором объявлено свойство app.service.url=какая-то строка. В этом же модуле (mmmp-service) это свойство внедряется через spring-аннотацию @Value. Но когда стартует приложение mmmp-servive spring не может найти это свойство и оставляет его равным null. Вопрос почему?

Класс настройки контекста и класс сервиса в который внедряется свойство модуля mmmp-service:

Это файл класса настройки контекста модуля mmmp-service

package ru.monsterdev.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySource(value = {"classpath:application.properties"})
public class AppConfig {
  @Bean
  public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
  }
}

Это файл класса сервиса модуля mmmp-service:

package ru.monsterdev.servises.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import ru.monsterdev.servises.IHttpService;

@Service
public class HttpService implements IHttpService {
  private static final Logger LOGGER = LoggerFactory.getLogger(HttpService.class);

  @Value("${app.service.url}")
  private String url;

  @Override
  public String getRequestBody(String param) {
    LOGGER.debug("URL is {}", url);
    return param;
  }
}

Класс модуля mmmp-web в котором вызывается сервис из модуля mmmp-service

package ru.monsterdev.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.monsterdev.servises.IHttpService;

@Controller
@RequestMapping("/")
public class IntexController {
  @Autowired
  private IHttpService httpService;

  @GetMapping("/")
  public String indexAction(Model model) {
    model.addAttribute("response", httpService.getRequestBody("Hello"));
    return "index";
  }
}

Есть предположение, что приложение mmmpweb.war не видит файл application.properties, потому что оно не лежит в его classpath, но дело в том, что модуль mmmpservice.jar лежит в WEB-INF/libs приложения mmmpweb.war, разве этого не достаточно?

1 ответ 1

0

Задай его программно:

package ru.monsterdev.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;


@Configuration
public class AppConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer configurator = new PropertySourcesPlaceholderConfigurer();
        configurator.setLocation(new ClassPathResource("/application.properties", AppConfig.class));
        return configurator;
    }
}

Ваш ответ

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

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