public interface OSService {
	
	void os();

}


@ConditionalOnProperty(prefix = "custom.os", name = "name", havingValue = "linux")
@Service("osService")
public class LinuxService implements OSService {
	
	@Override
	public void os() {
		System.out.println("Now OS is Linux!");
	}

}


@ConditionalOnProperty(prefix = "custom.os", name = "name", havingValue = "windows")
@Service("osService")
public class WindowsService implements OSService {

	@Override
	public void os() {
		System.out.println("Now OS is Windows!");
	}

}


@RequestMapping(path = "test")
@RestController
public class TempRestController {

	@Autowired
	OSService osService;

	@PostMapping(path = "condition")
	public void condition() {
		osService.os();
	}
}

配置文件 application.yml:
 

custom:
   os:
      name: linux

 

Logo

更多推荐