Reference
[1] 題目來源:https://www.learnmode.net/upload/flip/book/12/129df32e678ac3af/d0469f5495a9.pdf
package com.example.service; public interface WeatherService { String getWeatherForecast(); }
package com.example.service; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; @Service @Profile({"sunny", "default"}) public class SunnyDayService implements WeatherService { @Override public String getWeatherForecast() { return "Today is sunny day"; } }
package com.example.service; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; @Service @Profile({"raining"}) public class RainyDayService implements WeatherService { @Override public String getWeatherForecast() { return "Today is raining day"; } }
spring: profiles: active: raining
package com.example.test; import com.example.service.WeatherService; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.example") @Slf4j public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Bean CommandLineRunner testWeatherService(WeatherService weatherService) { return args -> { String output = weatherService.getWeatherForecast(); log.debug("weather forecast = {}", output); }; } }
2018-12-20 09:53:30.255 DEBUG 6920 --- [ main] com.example.test.TestApplication : weather forecast = Today is raining day