Last active
March 14, 2020 16:23
-
-
Save jalispran/088ee3c4da5f74cdcd8914f8ec48914b to your computer and use it in GitHub Desktop.
Gist for Inversion of Control blog post. Find the blog post at https://pranjalgore.com/blog/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//class in application.jar | |
@SpringBootApplication | |
@ComponentScan(basePackages= "com.plugin") //scan classes in package com.plugin in plugin.jar | |
public class Application { | |
public static void main(String[] args) { | |
SpringApplication.run(Application.class, args); | |
} | |
/* | |
* concrete implementation of com.plugin.Contract.java | |
* Spring will inject this bean into PluginApplication.java in plugin.jar | |
*/ | |
@Bean | |
public Contract contract() { | |
return () -> { | |
return "hello from application"; | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//interface in plugin.jar | |
public interface Contract { | |
public String getMessageToPrint(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//class in plugin.jar | |
@RestController | |
public class PluginApplication { | |
/* | |
* Spring will take care of finding the implementation of Contract interface | |
* the developer need not ask for/find the implementation | |
* the control of HOW this implementation is found is taken away from the developer and to the framework | |
* hence, this is inversion of control | |
*/ | |
@Autowired | |
private Contract contract; | |
@GetMapping | |
public String getThis() { | |
return contract.getMessageToPrint(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment