SpringLoader - using Spring with Sponge

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.

1 Like

You can absolutely remove this now! Spring is awful and god knows why anyone goes to it still when there are much better alternatives out there.

I had to use Spring Boot as part of my university degree and comparing it with the likes of Laravel and other frameworks, it’s awful. I hope no one ever starts using Spring with Sponge in any way ever, I feel like it’d make someone lose brain cells using both with one another.

(Completely opinionated post, but oh well :stuck_out_tongue:)

You are seriously comparing Java Frameworks to PHP Frameworks? Shakes head. Spring is widely used in Java and most job postings for Java I see advertised want some form of experience in it.

@sam543381 I’ve not used Spring, and I’m looking forward to trying it out and learning it with your plugin and Sponge, thanks.

1 Like

I’m not comparing the languages… smh

I’m comparing the principles used in each framework. Laravel adopts a nicer approach. Spring has terrible error handling, route handling etc…

I agree many job apps require Spring, but that’s generally because Spring is chosen by some business executive that thinks spring is a lean mean enterprise machine when in reality it sucks.

https://www.quora.com/Which-is-better-Play-Framework-or-Spring-MVC-How-should-I-decide-what-to-use

Here I hope that helps prove my point, not to mention Ore is built on top of Play, not Spring.

Alright, let’s explain !

First of all, Laravel is a PHP framework whose goal is to make web applications.
Spring is … an ecosystem consisting of multiple projects which includes : Spring Framework which itself includes Spring MVC.

Spring MVC and Laravel are-they really built to create the same type of applications ?

Absolutely not :

  • Laravel is clearly made for building browser-oriented web applications : it is shipped with vuejs, webpack and uses NPM. Just take a look at laracasts and you’ll see how frontend is part of the framework.
  • Spring MVC is shipped with no view template engine and it is up to you to use one (see starters).

I would personally primarly use Laravel to build showcase websites and Spring MVC to build REST or SOAP services with more complex needs (microservice architectures with Spring DataFlows, Spring Batch for batch operations, …). Using Spring in your webapps also enable the use of a set of awesome Java APIs : DeepLearning4j or Tensorflow for deep learning or Spark for distributed computing.

Let’s be honest

Why would you ship an entire website into a Sponge plugin ? That’s pointless : the website would not even be avaible when the server would be down !

What you’re missing

Spring is not just about building web applications but has the potential to make a lot more, which is especially useful with Sponge : I personally don’t see a lot of applications where a plugin would need to expose a web interface or API.

@ryantheleach I would be happy to help you use Spring, you should start by taking a look at the Spring Framework then Spring Boot references :smiley:

As you may noticed I’m not native, sorry for grammar mistakes ^^