As you may already know, Spring is an awesome set of tools developed by Pivotal intended to help developers (not necessarily Sponge ones) build modern, robust, JVM-based applications.
1- What do you mean by … “set of tools”
If you took a look at the Spring website, you might noticed that Spring is composed of multiple projects.
All Spring projects are based on the Spring Framework that provides “core support for dependency injection, transaction management, web applications, data access, messaging, testing and more”.
On top of this framework were built a bunch of projects, here is an exhaustive list you are likely to use in Sponge plugins :
- Spring Boot : Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.
- Spring Data : Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store.
- Spring Social : Connect your Spring application with Software-as-a-Service (SaaS) API providers such as Facebook, Twitter, and LinkedIn.
- … see the project list on the official website for a complete list.
2 - Alright, then what is SpringLoader useful for ?
Integrating Spring with Sponge is … not quite easy and can result in the creation of multiple SpringBoot applications in a single JVM.
In order to simplify the initialization of spring apps and remove boilerplate code, SpringLoader provides a consistent way to load your plugin using “addons” :
/**
* Just an example plugin using SpringLoader
*
* @see fr.sam543381.springloader.ExampleModule
*/
@org.spongepowered.api.plugin.Plugin(id = "fr-sam543381-springloader", name = "SpringLoader", authors = "Sam54", description = "Using Spring with Sponge")
public class ExamplePlugin {
@Inject
private Logger logger;
private Loader loader = Loader.getLoader();
@Listener
public void onStarting(GameAboutToStartServerEvent event) {
loader.withLogger(logger);
loader.registerModule(ExampleModule.class);
}
@Listener
public void onStarted(GameStartingServerEvent event) {
loader.init();
}
}
And the addon class, as a bean were you can use Spring’s concepts :
@Component
public class ExampleModule implements Addon {
@Override
public String getName() {
return "example";
}
@Autowired
private Logger logger;
@Autowired
private Loader loader;
@Override
public void ready() {
logger.info(
"SpringLoader is correctly running. Note that as long as it is in the classpath you can remove it from the mods folder.");
logger.debug("Addons registered by SpringLoader :");
for (Class<? extends Addon> cls : loader.getAddons())
logger.debug("- " + cls.getSimpleName() + " (" + cls.getName() + ")");
}
}
Edit : addons should be Spring components (annotated with @Component
, @Service
, @Repository
, or @Controller
) otherwise ready
is not going to be called.
Note that most of Sponge’s singletons can be autowired, see these beans for more informations.