Most people i speak to on the IRC chats have no idea what Spring is, all they know is that it is a Java J2EE programming framework.
I will today try to simplify the situation Spring in essence is mainly a configurable Dependency Injection (DI) framework, this means that you can initialise objects without actually knowing how to,
example:
public Class Service { private ModelObject modelObject; public Service(ModelObject modelObject) { this.modelObject = modelObject; } public void useModelObject() { .... } }
-
Single Responsibility Principle – Every object should have a single responsibility, and that responsibility should be encapsulated by the class
-
Open/Closed Principle – When you get a request for a new feature you should be able to add this without changing any old code by using subclases and new implmentations
-
Liskov Substitution Principle – This means that you should be able to use any subclass in the same place the parent class is used (it honours the method signatures of the parent)
-
Interface Segregation Principle – avoid getting your classes included into other classes, if you break this one it is considered bad design
-
Dependency Inversion Principle – Decouple your software modules, to achieve that you need to isolate dependencies for code reusage
This works by passing in the predefined ModelObject when the class is instantiated, so there is a config file in XML which details how to declare each object so the framework can pass it in. This means you invert the dependency
model so that if the “ModelObject” was actually declared as an Interface it would be able to take ANY class passed in on the configuration that implemented the declared interface.
This leads nicely to the SOLID programming principle where D means Dependency Inversion principle.
SOLID is an excellent way to make sure your new Class is good OO design.
Anyway back to Spring, so we have all our objects declared in the config as well as all the database configuration and the database drivers etc. This means that we have a lot of independent classes which are
sat all on their own and so is easy to test using unit tests. (this is a good thing) This is the main power of Spring, you can drop out and drop in classes and change them by just changing the configuration file.
A project i worked on used Spring to add security so that even if anyone tried to access data they shouldn’t then it still protected them, by having the Domain (Model) objects behind an assembler which checks which information is
allowed to be passed to the Domain Transfer Object (DTO), so if you are admin you can see the person’s email addresses but if not then they don’t even come out of the database so the information is just not there rather than being hidden.
This of course stops nasty hackers from siphoning off your users details and selling them to fraudsters as has happened with Sony, Amazon, SipGate and many other companies around the globe.