Skip to content

Support getting environment from other events. #7066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
Expand All @@ -49,7 +50,8 @@
* <p>
* Note: access to the Spring {@link Environment} is only possible when the
* {@link #setTriggerEventType(Class) triggerEventType} is set to
* {@link ApplicationEnvironmentPreparedEvent} or {@link ApplicationPreparedEvent}.
* {@link ApplicationEnvironmentPreparedEvent}, {@link ApplicationReadyEvent},
* or {@link ApplicationPreparedEvent}.
*
* @author Jakub Kubrynski
* @author Dave Syer
Expand Down Expand Up @@ -231,6 +233,10 @@ private Environment getEnvironment(SpringApplicationEvent event) {
return ((ApplicationPreparedEvent) event).getApplicationContext()
.getEnvironment();
}
if (event instanceof ApplicationReadyEvent) {
return ((ApplicationReadyEvent) event).getApplicationContext()
.getEnvironment();
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void overridePidFileWithSpring() throws Exception {
}

@Test
public void differentEventTypes() throws Exception {
public void tryEnvironmentPreparedEvent() throws Exception {
File file = this.temporaryFolder.newFile();
SpringApplicationEvent event = createEnvironmentPreparedEvent("spring.pid.file",
file.getAbsolutePath());
Expand All @@ -111,6 +112,19 @@ public void differentEventTypes() throws Exception {
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
}

@Test
public void tryReadyEvent() throws Exception {
File file = this.temporaryFolder.newFile();
SpringApplicationEvent event = createReadyEvent("spring.pid.file",
file.getAbsolutePath());
ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
listener.onApplicationEvent(event);
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isEmpty();
listener.setTriggerEventType(ApplicationReadyEvent.class);
listener.onApplicationEvent(event);
assertThat(FileCopyUtils.copyToString(new FileReader(file))).isNotEmpty();
}

@Test
public void withNoEnvironment() throws Exception {
File file = this.temporaryFolder.newFile();
Expand Down Expand Up @@ -170,6 +184,16 @@ private SpringApplicationEvent createPreparedEvent(String propName,
context);
}

private SpringApplicationEvent createReadyEvent(String propName,
String propValue) {
ConfigurableEnvironment environment = createEnvironment(propName, propValue);
ConfigurableApplicationContext context = mock(
ConfigurableApplicationContext.class);
given(context.getEnvironment()).willReturn(environment);
return new ApplicationReadyEvent(new SpringApplication(), new String[] {},
context);
}

private ConfigurableEnvironment createEnvironment(String propName, String propValue) {
MockPropertySource propertySource = mockPropertySource(propName, propValue);
ConfigurableEnvironment environment = new StandardEnvironment();
Expand Down