Spring: the Eclectic Framework
The only problem that remains is that the use of the news feed class needs to be hard coded in some main logic that uses the ShowNews bean; this is where Spring comes in. Its beans package allows developers to define bean wiring and dependencies in XML and then use some provided bean factory to read this XML, generate the beans, and properly set all injections.
For example, instead of hard coding explicit injection:
ShowNews sn = new ShowNews(); sn.setNewsFeed(new ReutersWorldNew()); sn.show_latest_news();
XML can be created for the Spring purposes.
<beans> <bean id="ReutersWorldNew" class="com.example.ReutersWorldNew"/> <bean id="BloombergNew" class="com.example.BloombergNew"/> <bean id="ShowNews" class="com.example.ShowNews"> <property name="NewsFeed"> <ref bean="ReutersWorldNew"/> </property> </bean> </beans>
The initiation code then would look like this:
BeanFactory factory = new XMLBeanFactory(new FileInputStream("config.xml")); ShowNews sn = (ShowNews) factory.getBean("ShowNews"); sn.show_latest_news();
If you need to change or add the news source, only the XML config file needs to be updated.
Note: In this example, none of the classes (or beans) reference any of the Spring interfaces or classes. Spring is fully transparent here; that is one of a more attractive features of the framework. Also, note that Spring provides many different implementations of the BeanFactory interface (and design pattern), so that reading XML is relatively easy.
The AOP's definition is: "The programming paradigm that attempts to aid programmers in the separation of concerns, or the breaking down of a program into distinct parts that overlap in functionality as little as possible. In particular, AOP focuses on the modularization and encapsulation of cross-cutting concerns".
What this basically means is that business logic should be clearly separated and represented as separate beans whose functionality does not overlap. Moreover, this concept can be extended further than the implementation of only business rules. For example, a bean that shows news should not do any logging, transductions, persistence, or security logic. The AOP aspect of Spring allows precisely that—to non-intrusively add (or weave in) services to the beans in XML, reuse services throughout the application, and separate overlapping functionality.
For example, to add a logging functionality to the News Feed service, something similar to this XML can be used:
<bean id=" ShowNews" class="com.example.ShowNews"> </bean> <bean id="myLogger" class="com.example.MyLogger"> <property name="someProperty"> <value>Custom string property value</value> </property> </bean> <bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"> </bean> <bean id="news" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.example.ShowNews</value> </property> <property name="target"> <ref local="ShowNews"/> </property> <property name="interceptorNames"> <list> <value>myLogger</value> <value>debugInterceptor</value> </list> </property> </bean>
Note: The interceptorNames property takes a list of String: the bean names of the interceptor or aspects in the current factory. Also, note the myLogger bean has a primitive string property set in XML as an example.
Spring provides a lot of functionality to wire beans in XML, such as injecting other beans via setters or constructors, setting primitive bean properties, setting collections of bean properties, and using AOP aspects. It can even auto-wire the beans by type or name. All of these features, in particular AOP, are provided by the Spring to eliminate the need to use EJBs. The transactions, security, and any other business rule can be woven as an aspect by using the Spring container.
Devil's Advocate Discussion
Now, return to the questions of industry support. Who should use Spring and what is involved to implement this technology in an enterprise project? As of this writing, Spring is not supported by any major IDE vendor. There is a stable third-party plug-in for Eclipse, but if you are using any other IDE, you are out of luck. The limited support means that there is no easy (or visual) way to generate configuration XML, which can be quite extensive in a large enterprise application with hundreds of beans and aspects, and thousands of properties. Even complex EJBs can be created visually with any modern IDE, and their code and configuration can be auto generated. To implement Spring in an enterprise project, developers need to learn the "XML language" of Spring configuration, bean wiring, and AOP and then code in both Java and XML. The IDE will not help unless the company adopts Eclipse as a standard, but even after that, a lot of XML will be coded by hand.
Spring's Web module also is configured via XML and with limited support in IDEs creating a Web application based on Spring, MVC can be a much longer and error-prone process. This can be compared to the JavaServer Faces framework, which offers visual application flow creation and auto XML config generation in most of the IDEs that support it.
In this article, I talked about the Spring framework. I have looked at its features, concepts, and usability. Despite the elegance and eclectic structure of the framework, my biggest concern is the limited industry support and heavy reliance on XML, which is a two-edged sword of the framework. On one hand, XML is the glue and makes the framework truly powerful; on the other, such complex XML should not be written by hand. Unfortunately, without proper tool support, there is not way around it at the moment. Having said that, I believe that Spring offers many useful modules and concepts, and investing some time to learn them can be beneficial. It will be interesting to see what will happen to this technology with the arrival of EJB3 and continued evolution of the other frameworks.
- The Spring Application Framework: http://www.springframework.org/
- Inversion of Control: http://en.wikipedia.org/wiki/Inversion_of_Control
- Borland Software Corporation: http://info.borland.com/techpubs/jbuilder/
- Oracle JDeveloper: http://www.oracle.com/technology/products/jdev/index.html
- Spring in Action
Craig Walls and Ryan Breidenbach
2005 | 472 pages
Manning Publications Co.
About the Author
Vlad Kofman is a Senior System Architect. He has implemented enterprise-scale projects for the major Wall Street firms, defense contracts, and the U.S. government. His main interests are object-oriented programming methodologies and the design patterns.
Page 2 of 2