Spring Concepts

At the heart of Spring lie two extremely powerful concepts: Aspect-Oriented Programming (AOP) and Inversion of Control (IoC).

The IoC is a design pattern (also known as Dependency Injection) that provides one object to another object through injection at runtime, thus inverting or shifting the control from the main object to resolve other objects it needs by providing them at runtime. This sounds complicated, but really it is not.

For example, say that you have an application that shows news to the user and, as a news feed, you are using Reuters World News.

This is a pseudo code of a ShowNews bean that is using another bean, ReutersWorldNews, to show and format the news in a method called show_latest_news.

Class ShowNews {   ReutersWorldNews rwn = new ReutersWorldNews();   Public News show_latest_news(){      news = Rwn.getNews();      return news.format();   }}

Say that you have decided to add Bloomberg News as another news source. However, that would require rewriting an entire ShowNews class because it is hard coupled with the ReutersWorldNews bean.

To fix this using IoC, you will need to create an interface and provide a specific news feed class (implementing this interface) for use in the ShowNews bean.

For example:

Interface NewsFeed {   News getNews();}
ReutersWorldNews implements NewsFeed …BloombergNews    implements NewsFeed …

The fixed ShowNews class:

Class ShowNews {   NewsFeed nf;    // now an interface   void setNewsFeed(NewsFeed injected_news_feed){   this.nf = injected_news_feed.}
   Public News show_latest_news(){
      news = nf.getNews();      return news.format();   }}

Now, the class can be injected with any news feed that implements NewsFeed interface and there is no hard coupling. This of course is a very simplified example, but it shows the main idea behind this pattern. You can use setter injection, as well as constructor injection, which is a matter of preference.

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.