Skip to content

Instantly share code, notes, and snippets.

@prakapenka
Created October 10, 2018 08:30
Show Gist options
  • Save prakapenka/ebdd06621204c647bd84a21c8ca0dab8 to your computer and use it in GitHub Desktop.
Save prakapenka/ebdd06621204c647bd84a21c8ca0dab8 to your computer and use it in GitHub Desktop.
костыль для загрузки конфигов из одного места
(META-INF/spring.factories)
org.springframework.context.ApplicationListener=mock.config.LocationsSettingConfigFileApplicationListener
...
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import static java.util.Objects.isNull;
public class LocationsSettingConfigFileApplicationListener implements
ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered
{
@Override
public int getOrder()
{
return ConfigFileApplicationListener.DEFAULT_ORDER - 1;
}
/**
* see
* https://stackoverflow.com/questions/29897802/spring-boot-override-convention-used-to-find-application-properties-config-file
*
* @param event
*/
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event)
{
SpringApplication app = event.getSpringApplication();
ConfigurableEnvironment env = event.getEnvironment();
String defaultLocation = env.getProperty(ConfigFileApplicationListener.CONFIG_LOCATION_PROPERTY);
if (isNull(defaultLocation) || defaultLocation.isEmpty())
{
return;
}
for (ApplicationListener<?> listener : app.getListeners())
{
if (listener instanceof ConfigFileApplicationListener)
{
ConfigFileApplicationListener cfal = (ConfigFileApplicationListener) listener;
cfal.setSearchLocations(defaultLocation);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment