Skip to content

Instantly share code, notes, and snippets.

@jalispran
Last active March 14, 2020 16:23
Show Gist options
  • Save jalispran/088ee3c4da5f74cdcd8914f8ec48914b to your computer and use it in GitHub Desktop.
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/
//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";
};
}
}
//interface in plugin.jar
public interface Contract {
public String getMessageToPrint();
}
//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