Java Spring Loose Coupling
The object-oriented concept is a good design to break the system into a group of reusable objects. However, when the system gets bigger, especially in Java projects, huge object dependencies will always be tightly coupled to make objects difficult to manage or modify. In this case, all object dependencies can be easily and efficiently managed using the Spring framework as a core module.
Example of output generator
Let's look at an example, suppose your project has a function output in CSV or JSON format. Your code might look like the following example:
File : IOutputGenerator.java Output Generator interface package com.yiibai.output;public interface IOutputGenerator{ public void generateOutput();}File : CsvOutputGenerator.java A CSV output generator is used to implement the IOutputGenerator interface. package com.yiibai.output.impl;import com.yiibai.output.IOutputGenerator;public class CsvOutputGenerator implements IOutputGenerator{ public void generateOutput(){ System.out.println("Csv Output Generator"); }}File: JsonOutputGenerator.java A JSON output generator is used to implement the IOutputGenerator interface. package com.yiibai.output.impl;import com.yiibai.output.IOutputGenerator;public class JsonOutputGenerator implements IOutputGenerator{ public void generateOutput(){ System.out.println("Json Output Generator"); }} There are several ways to call IOutputGenerator, and how to use Spring to avoid objects being closely linked to each other.
1. Method 1 is called directly
The normal way is to call it directly.
package com.yiibai.common;import com.yiibai.output.IOutputGenerator;import com.yiibai.output.impl.CsvOutputGenerator;/* http://www.manongjc.com/article/1602.html */public class App { public static void main( String[] args ) { IOutputGenerator output = new CsvOutputGenerator(); output.generateOutput(); }}There are problems
In this way, the problem is that "output" is tightly coupled to CsvOutputGenerator, and every change generated by the output may involve code changes. If this code is scattered throughout your project, every change generated by the output will cause you to suffer.
Method 2 Call it with a helper class
Maybe you want to create a helper class to implement all output inside the class.
package com.yiibai.output;import com.yiibai.output.IOutputGenerator;import com.yiibai.output.impl.CsvOutputGenerator;public class OutputHelper{ IOutputGenerator outputGenerator; public OutputHelper(){ outputGenerator = new CsvOutputGenerator(); } public void generateOutput(){ outputGenerator.generateOutput(); } } Call it through the helper class
package com.yiibai.common;import com.yiibai.output.OutputHelper;public class App { public static void main( String[] args ) { OutputHelper output = new OutputHelper(); output.generateOutput(); }}There are problems
This looks more elegant than before, just managing a helper class, but the helper class is still tightly coupled to the CsvOutputGenerator, and every change generated by the output still involves small code changes.
Method 3 Spring
In this case, Spring Dependency Injection (DI) is a good choice. Spring allows output generation to be loosely coupled to the output generator.
The OutputHelper class has smaller modifications.
package com.yiibai.output;import com.yiibai.output.IOutputGenerator;public class OutputHelper{ IOutputGenerator outputGenerator; public void generateOutput(){ outputGenerator.generateOutput(); } public void setOutputGenerator(IOutputGenerator outputGenerator){ this.outputGenerator = outputGenerator; }} Create a Spring bean configuration file and declare all Java objects dependencies here.
<!-- 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"> <property name="outputGenerator" ref="CsvOutputGenerator" /> </bean> <bean id="CsvOutputGenerator" /> <bean id="JsonOutputGenerator" /> </beans>
Call it through Spring
package com.yiibai.common;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiibai.output.OutputHelper;public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml"}); OutputHelper output = (OutputHelper)context.getBean("OutputHelper"); output.generateOutput(); }} Now, just change the Spring XML file using a different output generator. Only modify Spring XML files without uncensored modifications means fewer errors.
Conclusion: With Spring Framework - This dependency injection (DI) is useful for object dependency management, making it more elegant, highly flexible and easy to maintain in the development management of large Java projects.
Thank you for reading, I hope it can help you. Thank you for your support for this site!