File tree Expand file tree Collapse file tree 5 files changed +54
-4
lines changed
spring-weather-app/src/main/java/cloud/nativ/flamewars Expand file tree Collapse file tree 5 files changed +54
-4
lines changed Original file line number Diff line number Diff line change 99import java .util .concurrent .TimeUnit ;
1010import java .util .logging .Level ;
1111import java .util .logging .Logger ;
12+ import java .util .stream .IntStream ;
1213
1314public 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 }
Original file line number Diff line number Diff line change 1+ package cloud .nativ .flamewars ;
2+
3+ public interface WeatherProvider {
4+ String getWeather ();
5+ }
Original file line number Diff line number Diff line change 11package cloud .nativ .flamewars ;
22
3+ import org .springframework .beans .BeansException ;
4+ import org .springframework .context .ApplicationContext ;
5+ import org .springframework .context .ApplicationContextAware ;
36import 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments