Skip to content

Commit 2a22ced

Browse files
committed
Added named based WeatherProvider lookup.
1 parent 03d1633 commit 2a22ced

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

spring-weather-app/src/main/java/cloud/nativ/flamewars/WeatherApp.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.concurrent.TimeUnit;
1010
import java.util.logging.Level;
1111
import java.util.logging.Logger;
12+
import java.util.stream.IntStream;
1213

1314
public class WeatherApp {
1415

@@ -18,8 +19,17 @@ public static void main(String[] args) {
1819
WeatherApp app = new WeatherApp();
1920
app.start();
2021

21-
Weather rosenheim = app.getWeather("Rosenheim");
22-
LOGGER.info(rosenheim.toString());
22+
// who needs for loops, right?!
23+
IntStream.range(0, 3).forEach(i -> {
24+
Weather rosenheim = app.getWeather("Rosenheim");
25+
LOGGER.info(rosenheim.toString());
26+
});
27+
28+
// oops, I did it again. ;)
29+
IntStream.range(0, 3).forEach(i -> {
30+
Weather london = app.getWeather("London");
31+
LOGGER.info(london.toString());
32+
});
2333

2434
app.stop();
2535
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cloud.nativ.flamewars;
2+
3+
public interface WeatherProvider {
4+
String getWeather();
5+
}
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package cloud.nativ.flamewars;
22

3+
import org.springframework.beans.BeansException;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.ApplicationContextAware;
36
import org.springframework.stereotype.Component;
47

58
@Component
6-
public class WeatherRepository {
9+
public class WeatherRepository implements ApplicationContextAware {
10+
11+
private ApplicationContext applicationContext;
712

813
public Weather findWeatherByCity(String city) {
9-
return new Weather(city, "Sunshine");
14+
WeatherProvider provider = applicationContext.getBean(city, WeatherProvider.class);
15+
return new Weather(city, provider.getWeather());
16+
}
17+
18+
@Override
19+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
20+
this.applicationContext = applicationContext;
1021
}
1122
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cloud.nativ.flamewars.provider;
2+
3+
import cloud.nativ.flamewars.WeatherProvider;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component("London")
7+
public class LondonWeatherProvider implements WeatherProvider {
8+
@Override
9+
public String getWeather() {
10+
return "Rainy";
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cloud.nativ.flamewars.provider;
2+
3+
import cloud.nativ.flamewars.WeatherProvider;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component("Rosenheim")
7+
public class RosenheimWeatherProvider implements WeatherProvider {
8+
@Override
9+
public String getWeather() {
10+
return "Sunshine";
11+
}
12+
}

0 commit comments

Comments
 (0)