Spring Loose Coupling example
The idea of object-oriented design is to divide the whole system into a set of reusable components, however, when the system gets bigger, especially in Java, the biggest object dependency will be tightly coupled so that it is very difficult to manage and modify, and now you can use the spring framework to play the role of an intermediate module, Easy and efficient management of other component dependencies
Example of output generation
For example, suppose your project has a way to output content to CSV or JSON format, you might write code like this
package com.mkyong.output;public interface IOutputGenerator{public void generateOutput();}
, and then the class that implements the interface
PackageCom.Mkyong.Output.Impl;ImportCom.Mkyong.Output.ioutputgenerator;public class Csvoutputgenerator implements Ioutputgenerator{public void Generateoutput () { system.. Println ( "CSV Output Generator" }} /span>
And write a JSON-generated class.
PackageCom.Mkyong.Output.Impl;ImportCom.Mkyong.Output.ioutputgenerator;public class Jsonoutputgenerator implements Ioutputgenerator{public void Generateoutput () { system.. Println "Json Output Generator" }} /span>
There are several ways to invoke the Ioutputgenerator interface, and how we can use spring to avoid over-coupling of objects.
Method 1-Call directly
PackageCom.Mkyong.Common;ImportCom.Mkyong.Output.Ioutputgenerator;ImportCom.Mkyong.Output.Impl.Csvoutputgenerator;Public Class App {public static void main( String[] args ) { ioutputgenerator Output = new csvoutputgenerator(); output. Generateoutput(); }}< /c14>
Problem:
This method, output this object and csvoutputgenerator coupling together, every time to change the output format will have to modify the code, if this type of code throughout the project, then change up on the kneeling
method is called by the helper class
Maybe you'd like to create a helper class. All the output implementations are moved in
PackageCom.Mkyong.Output;ImportCom.Mkyong.Output.Ioutputgenerator;ImportCom.Mkyong.Output.Impl.Csvoutputgenerator;Public Class Outputhelper{ioutputgenerator Outputgeneratorpublic outputhelper () {outputgenerator = newcsvoutputgenerator (); }public void Generateoutput () {outputgenerator.generateoutput (); }} /span>
You can then call this
PackageCom.Mkyong.Common;ImportCom.Mkyong.Output.Outputhelper;Public Class app { public static void Main ( string[] args { outputhelper output = newoutputhelper (); Output.}} /span>
Problem:
It seems more elegant, you just need to manage this helper class to achieve different formats of output requirements change, however, helper or cvsoutputgenerator coupling, every time you want to change the output format, the helper class to do a fine tune.
Method 3-spring
Spring dependency Injection is appropriate to separate the different format generation classes
First, a little fine-tuning the outputhelper, adding a parameter
PackageCom.Mkyong.Output;ImportCom.Mkyong.Output.Ioutputgenerator;Public Class Outputhelper{IoutputgeneratorOutputgeneratorpublic void () {outputgenerator. Generateoutput}public void Setoutputgenerator (ioutputgenerator Outputgeneratorthis. Outputgenerator = Outputgenerator;}} /span>
Then create a spring bean configuration file and declare that all Java objects depend on the
<!--Spring-common.xml--<beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"Xsi:schemalocation="Http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean Id="Outputhelper" Class="Com.mkyong.output.OutputHelper"><property Name="Outputgenerator" Ref="Csvoutputgenerator" /></bean><bean id= "csvoutputgenerator" class=" Com.mkyong.output.impl.CsvOutputGenerator " /><bean id=" Jsonoutputgenerator " class=" Com.mkyong.output.impl.JsonOutputGenerator " /> </beans>
Then call through spring
PackageCom.Mkyong.Common;ImportOrg.Springframework.Context.ApplicationContext;ImportOrg.Springframework.Context.Support.Classpathxmlapplicationcontext;ImportCom.Mkyong.Output.Outputhelper;Public Class App { Public Static voidMain( String[]Args) { ApplicationContextContext= New classpathxmlapplicationcontext (new< Span class= "PLN" > string[] { "spring-common.xml" }); outputhelper output = Span class= "pun" > (outputhelpercontextgetbean (. Generateoutput}} /span>
In the future to change the JSON format, directly change the XML configuration file on the line. Ability to reduce errors
Conclusion
With the spring framework's dependency injection, you can gracefully manage object dependencies with greater flexibility, especially for Java projects.
Spring Loose Coupling example (RPM)