Spring(1)-Spring loosely coupled example
来源:互联网 发布:linux修改普通用户密码 编辑:程序博客网 时间:2024/06/06 04:21
The object-oriented is a good design to break your system into a group of reusable objects. However, when system grows large, especially in Java project, the huge objects dependencies will always tightly coupled and causing the objects very hard to manage or modify. In this scenarios, you can use Spring framework to act as a central module to manage all the object dependencies easily and efficiently.
Output Generator Example
Let see an example, assume your project has a function to output the content to Csv or Json format. Your code may look like following:
File : IOutputGenerator.java – An interface for output generator
package com.mkyong.output; public interface IOutputGenerator{public void generateOutput();}
File : CsvOutputGenerator.java – A Csv output generator to implement the IOutputGenerator interface.
package com.mkyong.output; public interface IOutputGenerator{public void generateOutput();}
File : JsonOutputGenerator.java – A Json output generator to implement the IOutputGenerator interface.
package com.mkyong.output.impl; import com.mkyong.output.IOutputGenerator; public class JsonOutputGenerator implements IOutputGenerator{public void generateOutput(){System.out.println("Json Output Generator");}}
See few ways to call the IOutputGenerator, and how to use Spring to avoid objects to coupled tightly with each other
Normal way, call it directly.
package com.mkyong.common; import com.mkyong.output.IOutputGenerator;import com.mkyong.output.impl.CsvOutputGenerator; public class App { public static void main( String[] args ) { IOutputGenerator output = new CsvOutputGenerator(); output.generateOutput(); }}
Problem
In this way, the problem is the “output” is coupled tightly to CsvOutputGenerator, every change of output generator may involve code change. If this code is scatter over all of your project, every change of the output generator will make you suffer seriously.
Method 2 – Call it with helper class
You may think of create a helper class to move all the output implementation inside.
package com.mkyong.output; import com.mkyong.output.IOutputGenerator;import com.mkyong.output.impl.CsvOutputGenerator; public class OutputHelper{IOutputGenerator outputGenerator; public OutputHelper(){outputGenerator = new CsvOutputGenerator();} public void generateOutput(){outputGenerator.generateOutput();} }
Call it via helper class.
package com.mkyong.common; import com.mkyong.output.OutputHelper; public class App { public static void main( String[] args ) { OutputHelper output = new OutputHelper(); output.generateOutput(); }}
Problem
This look more elegant, and you only need to manage a single helper class, however the helper class is still tightly coupled to CsvOutputGenerator, every change of output generator is still involve minor code change.
Method 3 – Spring
In this scenario, Spring Dependency Injection (DI) is a good choice. The Spring can make your output generator loosely coupled to the output generator.
Minor change in OutputHelper class.
package com.mkyong.output; import com.mkyong.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 the your Java object 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" 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>
Call it via Spring
package com.mkyong.common; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mkyong.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, you just need to change the Spring XML file for a different output generator. When output changed, you need to modify the Spring XML file only, no code changed, means less error.
Conclusion
With Spring framework – Dependency Injection (DI) is a useful feature for the object dependencies management, it is just elegant, highly flexible and easy for maintainability, specially in large Java project.
- Spring loosely coupled example
- Spring loosely coupled example
- Spring(1)-Spring loosely coupled example
- spring example
- Loosely Coupled: The Missing Pieces of Web Services
- Spring Batch + Spring TaskScheduler example
- Spring MVC - DispatcherServlet Example
- Spring MVC Model Example
- Spring Bean Scopes Example
- Spring + JDBC example
- Spring AOP Example
- spring aop excellent example
- Spring 3 JavaConfig example
- Spring bean reference example
- Spring bean scopes example
- Spring 3 JavaConfig example
- Spring ListFactoryBean example
- Spring SetFactoryBean example
- 将求最大的连续组合值转换为背包问题
- 谁获得了最高奖学金
- 设置document.domain实现js跨域
- 比特位操作算法题汇总
- 同一个程序里调用多个BAPI需要注意的情况
- Spring(1)-Spring loosely coupled example
- 最大子数组乘积
- [归纳]PHP手册重读与整理-基本语法与类型
- 那是一种尊敬、一种祝福
- Spring(2)-Spring Dependency Injection (DI)
- 1024Max Sum Plus Plus
- 2440学习(一)—— NFS搭建
- 隧道适配器,本地连接过多的解决办法
- 我深深感觉到每一次呼吸里都充满了思念的韵味