SlideShare a Scribd company logo
1 of 19
The Spring Framework:
  A brief introduction to
   Inversion of Control




       James Brundege




                        www.synaptocode.com
What is Spring?

2 Things:

 ◦ An Inversion of Control (IoC) Container

 ◦ Utilities that provide a consistent (and simple!)
  API to many other technologies (JDBC, ORM,
  AOP, Declarative Transactions, etc)
Guiding Principles of Spring
◦ Minimize dependencies on Spring

◦ Minimize dependencies between all layers of an
  application.

◦ All application code should be testable, without an
  application server or other complex environment

◦ Fully factor the APIs (the 90% case should be
  accomplished in one line of code!)
What is Inversion of Control (IoC)?
(besides yet another confusing term for a simple concept)

IoC is all about Object dependencies.

Traditional "Pull" approach:
 ◦ Direct instantiation
 ◦ Asking a Factory for an implementation
 ◦ Looking up a service via JNDI
"Push" approach:
  ◦ Something outside of the Object "pushes" its dependencies
     into it. The Object has no knowledge of how it gets its
                 dependencies, it just assumes they are there.


The "Push" approach is called "Dependency Injection".
The BookDemo Application:
(A simple CRUD app)

                            User Interface




                Domai
                  n
                            Service Layer
                Model




                        Data Access Layer



                                 DB
Pull Example
public class BookDemoServicePullImpl implements BookDemoService {


    public void addPublisherToBook(Book book) {

        BookDemoFactory factory = BookDemoFactory.getFactory();
        BookDemoDao dao = factory.getBookDemoDao();

        String isbn = book.getIsbn();
        if (book.getPublisher() == null && isbn != null) {
            Publisher publisher = dao.findPublisherByIsbn(isbn);
            book.setPublisher(publisher);
        }

    }

}
Push Example
(Dependency Injection)

public class BookDemoServiceImpl implements BookDemoService {

    private BookDemoDao dao;

    public void addPublisherToBook(Book book) {

        String isbn = book.getIsbn();
        if (book.getPublisher() == null && isbn != null) {
            Publisher publisher = dao.findPublisherByIsbn(isbn);
            book.setPublisher(publisher);
        }

    }

    public void setBookDemoDao(BookDemoDao dao) {
        this.dao = dao;
    }

}
BookDemoService Unit Test
Definitions

Dependency Injection is the act of injecting dependencies into an
 Object.

Inversion of Control is the general style of using Dependency
  Injection to wire together application layers.

Hence Spring is an Inversion of Control container. That is, it is a
 container that handles Dependency Injection for you.
Why is Dependency Injection better?
2 reasons:
  ◦ Loose Coupling
  ◦ Testability
Loose Coupling is improved because you don't hard-code
 dependencies between layers and modules. Instead you
 configure them outside of the code. This makes it easy to swap
 in a new implementation of a service, or break off a module and
 reuse it elsewhere.

Testability is improved because your Objects don't know or care
 what environment they're in as long as someone injects their
 dependencies. Hence you can deploy Objects into a test
 environment and inject Mock Objects for their dependencies
 with ease.
How Spring does Inversion of Control

◦ Write a configuration file in which you name
  concrete "beans" for the interfaces between your
  layers.

◦ "Wire" the application together by stating which
  beans are dependent on each other.

◦ Instantiate a Spring object called an
  ApplicationContext. This is a type of bean factory
  that will instantiate all your other beans and handle
  dependency injection.
Example Spring applicationContext.xml
<beans>

    <bean id="bookDemoDao"
            class="com.bookdemo.dao.hibernate.BookDemoDaoHibernateImpl">

          <property name="sessionFactory">
              <ref local="sessionFactory"/>
          </property>

    </bean>

    <bean id="bookDemoService"
            class="com.bookdemo.service.impl.BookDemoServiceImpl">

          <property name="bookDemoDao">
              <ref bean="bookDemoDao"/>
          </property>

    </bean>

</bean>
Bootstapping the IoC container
To start an app using IoC:
 ◦ Create an ApplicationContext object and tell it
      where applicationContext.xml is.
ApplicationContext appContext =
  new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");




This just has to be done once on startup, and can be done
 in the main() method or whatever code bootstraps the
 application.
Bootstapping the IoC container
The appContext holds a single copy of each bean
 declared in applicationContext.xml, so you could ask
 the Context for any bean by name:
MyService service = (MyService)appContext.getBean("myService");


But don't! That is a "Pull" technique that treats the
 ApplicationContext like a Factory.

Instead, make sure that applicationContext.xml connects
 every bean to every other bean that needs it. None of
 the beans thus have a dependency on spring.jar
Bootstapping the IoC container
For web applications the situation is simpler:

Web applications are bootstrapped by the web
 container based on the web.xml file. Hence creating
 an ApplicationContext on startup is as simple as a
 single declaration in web.xml:
<listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
</listener>
A Spring app with no dependencies
on Spring?

When bootstrapping spring from web.xml, there are
 only two pieces:
 ◦ The applicationContext.xml file
 ◦ A single tag within web.xml

Not a single line of Java code!

Therefore, not one of your custom classes has a
 dependency on spring.jar
The BookDemo App
What else has Spring got?

Spring provides either implementations or fully-factored API
 wrappers over these technologies:

 ◦ JDBC and DAOs
 ◦ ORM: Hibernate, iBatis, TopLink and others
 ◦ Declarative Transaction Support (without a full J2EE
    app server)
 ◦ Aspect-Oriented Programming
 ◦ Remote calls and Web Services (Axis)
 ◦ EJBs
Resources
Spring Website:
http://www.springframework.org/

Download Spring with sample applications:
http://prdownloads.sourceforge.net/springframework/spring-framework-1.2.8-with-dependencies.zip?downlo


Rod Johnson's book on Spring:
http://www.powells.com/biblio?PID=719&cgi=product&isbn=0764574833

Wikipedia:
http://en.wikipedia.org/wiki/Spring_Framework_%28Java%29

EasyMock:
http://www.easymock.org/

Echo2 (Rich Web Inteface framework)
http://www.nextapp.com/platform/echo2/echo/

More Related Content

What's hot

Automation Anywhere Case study
Automation Anywhere Case studyAutomation Anywhere Case study
Automation Anywhere Case studyShekar S
 
JSON Rules Language
JSON Rules LanguageJSON Rules Language
JSON Rules Languagegiurca
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY GOKUL SREE
 

What's hot (9)

KnockoutJS and MVVM
KnockoutJS and MVVMKnockoutJS and MVVM
KnockoutJS and MVVM
 
"this" in JavaScript
"this" in JavaScript"this" in JavaScript
"this" in JavaScript
 
Java
JavaJava
Java
 
Automation Anywhere Case study
Automation Anywhere Case studyAutomation Anywhere Case study
Automation Anywhere Case study
 
JSON Rules Language
JSON Rules LanguageJSON Rules Language
JSON Rules Language
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
 
Tomcat + other things
Tomcat + other thingsTomcat + other things
Tomcat + other things
 
Web Api vs MVC
Web Api vs MVCWeb Api vs MVC
Web Api vs MVC
 
React.js
React.jsReact.js
React.js
 

Viewers also liked

Viewers also liked (12)

Final macro eco
Final macro ecoFinal macro eco
Final macro eco
 
Denver Startup Weekend June 2011 - 401(k) foster pitch
Denver Startup Weekend June 2011 - 401(k) foster pitchDenver Startup Weekend June 2011 - 401(k) foster pitch
Denver Startup Weekend June 2011 - 401(k) foster pitch
 
Mnc
MncMnc
Mnc
 
งานนำเสนอ1 บัญชี
งานนำเสนอ1 บัญชีงานนำเสนอ1 บัญชี
งานนำเสนอ1 บัญชี
 
Overtrading on credit final
Overtrading on credit finalOvertrading on credit final
Overtrading on credit final
 
Bge
BgeBge
Bge
 
Negotitation skills
Negotitation skillsNegotitation skills
Negotitation skills
 
Excel
ExcelExcel
Excel
 
Rationalization of subsidies
Rationalization of subsidiesRationalization of subsidies
Rationalization of subsidies
 
The Best Logo Design Trends of 2015
The Best Logo Design Trends of 2015The Best Logo Design Trends of 2015
The Best Logo Design Trends of 2015
 
Pantene
PantenePantene
Pantene
 
unilever organizational change
unilever organizational change unilever organizational change
unilever organizational change
 

Similar to Spring io c

Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAOAnushaNaidu
 
Skillwise-Spring framework 1
Skillwise-Spring framework 1Skillwise-Spring framework 1
Skillwise-Spring framework 1Skillwise Group
 
Spring (1)
Spring (1)Spring (1)
Spring (1)Aneega
 
Lecture android best practices
Lecture   android best practicesLecture   android best practices
Lecture android best practiceseleksdev
 
Integration Testing With ScalaTest and MongoDB
Integration Testing With ScalaTest and MongoDBIntegration Testing With ScalaTest and MongoDB
Integration Testing With ScalaTest and MongoDBMichal Bigos
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignJames Phillips
 
Spring framework
Spring frameworkSpring framework
Spring frameworkAjit Koti
 
Effective JavaFX architecture with FxObjects
Effective JavaFX architecture with FxObjectsEffective JavaFX architecture with FxObjects
Effective JavaFX architecture with FxObjectsSrikanth Shenoy
 
Spring from a to Z
Spring from  a to ZSpring from  a to Z
Spring from a to Zsang nguyen
 
Gnizr Architecture (for developers)
Gnizr Architecture (for developers)Gnizr Architecture (for developers)
Gnizr Architecture (for developers)hchen1
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOCCarl Lu
 
Spring training
Spring trainingSpring training
Spring trainingTechFerry
 
Dependency Injection Lightning Talk
Dependency Injection Lightning TalkDependency Injection Lightning Talk
Dependency Injection Lightning TalkJon Bachelor
 
2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdfDeoDuaNaoHet
 
Ef Poco And Unit Testing
Ef Poco And Unit TestingEf Poco And Unit Testing
Ef Poco And Unit TestingJames Phillips
 
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...Amit Singh
 
Toms introtospring mvc
Toms introtospring mvcToms introtospring mvc
Toms introtospring mvcGuo Albert
 

Similar to Spring io c (20)

Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAO
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Skillwise-Spring framework 1
Skillwise-Spring framework 1Skillwise-Spring framework 1
Skillwise-Spring framework 1
 
Spring (1)
Spring (1)Spring (1)
Spring (1)
 
Lecture android best practices
Lecture   android best practicesLecture   android best practices
Lecture android best practices
 
Spring 2
Spring 2Spring 2
Spring 2
 
Integration Testing With ScalaTest and MongoDB
Integration Testing With ScalaTest and MongoDBIntegration Testing With ScalaTest and MongoDB
Integration Testing With ScalaTest and MongoDB
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class Design
 
Spring session
Spring sessionSpring session
Spring session
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Effective JavaFX architecture with FxObjects
Effective JavaFX architecture with FxObjectsEffective JavaFX architecture with FxObjects
Effective JavaFX architecture with FxObjects
 
Spring from a to Z
Spring from  a to ZSpring from  a to Z
Spring from a to Z
 
Gnizr Architecture (for developers)
Gnizr Architecture (for developers)Gnizr Architecture (for developers)
Gnizr Architecture (for developers)
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
 
Spring training
Spring trainingSpring training
Spring training
 
Dependency Injection Lightning Talk
Dependency Injection Lightning TalkDependency Injection Lightning Talk
Dependency Injection Lightning Talk
 
2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf
 
Ef Poco And Unit Testing
Ef Poco And Unit TestingEf Poco And Unit Testing
Ef Poco And Unit Testing
 
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
 
Toms introtospring mvc
Toms introtospring mvcToms introtospring mvc
Toms introtospring mvc
 

Spring io c

  • 1. The Spring Framework: A brief introduction to Inversion of Control James Brundege www.synaptocode.com
  • 2. What is Spring? 2 Things: ◦ An Inversion of Control (IoC) Container ◦ Utilities that provide a consistent (and simple!) API to many other technologies (JDBC, ORM, AOP, Declarative Transactions, etc)
  • 3. Guiding Principles of Spring ◦ Minimize dependencies on Spring ◦ Minimize dependencies between all layers of an application. ◦ All application code should be testable, without an application server or other complex environment ◦ Fully factor the APIs (the 90% case should be accomplished in one line of code!)
  • 4. What is Inversion of Control (IoC)? (besides yet another confusing term for a simple concept) IoC is all about Object dependencies. Traditional "Pull" approach: ◦ Direct instantiation ◦ Asking a Factory for an implementation ◦ Looking up a service via JNDI "Push" approach: ◦ Something outside of the Object "pushes" its dependencies into it. The Object has no knowledge of how it gets its dependencies, it just assumes they are there. The "Push" approach is called "Dependency Injection".
  • 5. The BookDemo Application: (A simple CRUD app) User Interface Domai n Service Layer Model Data Access Layer DB
  • 6. Pull Example public class BookDemoServicePullImpl implements BookDemoService { public void addPublisherToBook(Book book) { BookDemoFactory factory = BookDemoFactory.getFactory(); BookDemoDao dao = factory.getBookDemoDao(); String isbn = book.getIsbn(); if (book.getPublisher() == null && isbn != null) { Publisher publisher = dao.findPublisherByIsbn(isbn); book.setPublisher(publisher); } } }
  • 7. Push Example (Dependency Injection) public class BookDemoServiceImpl implements BookDemoService { private BookDemoDao dao; public void addPublisherToBook(Book book) { String isbn = book.getIsbn(); if (book.getPublisher() == null && isbn != null) { Publisher publisher = dao.findPublisherByIsbn(isbn); book.setPublisher(publisher); } } public void setBookDemoDao(BookDemoDao dao) { this.dao = dao; } }
  • 9. Definitions Dependency Injection is the act of injecting dependencies into an Object. Inversion of Control is the general style of using Dependency Injection to wire together application layers. Hence Spring is an Inversion of Control container. That is, it is a container that handles Dependency Injection for you.
  • 10. Why is Dependency Injection better? 2 reasons: ◦ Loose Coupling ◦ Testability Loose Coupling is improved because you don't hard-code dependencies between layers and modules. Instead you configure them outside of the code. This makes it easy to swap in a new implementation of a service, or break off a module and reuse it elsewhere. Testability is improved because your Objects don't know or care what environment they're in as long as someone injects their dependencies. Hence you can deploy Objects into a test environment and inject Mock Objects for their dependencies with ease.
  • 11. How Spring does Inversion of Control ◦ Write a configuration file in which you name concrete "beans" for the interfaces between your layers. ◦ "Wire" the application together by stating which beans are dependent on each other. ◦ Instantiate a Spring object called an ApplicationContext. This is a type of bean factory that will instantiate all your other beans and handle dependency injection.
  • 12. Example Spring applicationContext.xml <beans> <bean id="bookDemoDao" class="com.bookdemo.dao.hibernate.BookDemoDaoHibernateImpl"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="bookDemoService" class="com.bookdemo.service.impl.BookDemoServiceImpl"> <property name="bookDemoDao"> <ref bean="bookDemoDao"/> </property> </bean> </bean>
  • 13. Bootstapping the IoC container To start an app using IoC: ◦ Create an ApplicationContext object and tell it where applicationContext.xml is. ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml"); This just has to be done once on startup, and can be done in the main() method or whatever code bootstraps the application.
  • 14. Bootstapping the IoC container The appContext holds a single copy of each bean declared in applicationContext.xml, so you could ask the Context for any bean by name: MyService service = (MyService)appContext.getBean("myService"); But don't! That is a "Pull" technique that treats the ApplicationContext like a Factory. Instead, make sure that applicationContext.xml connects every bean to every other bean that needs it. None of the beans thus have a dependency on spring.jar
  • 15. Bootstapping the IoC container For web applications the situation is simpler: Web applications are bootstrapped by the web container based on the web.xml file. Hence creating an ApplicationContext on startup is as simple as a single declaration in web.xml: <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
  • 16. A Spring app with no dependencies on Spring? When bootstrapping spring from web.xml, there are only two pieces: ◦ The applicationContext.xml file ◦ A single tag within web.xml Not a single line of Java code! Therefore, not one of your custom classes has a dependency on spring.jar
  • 18. What else has Spring got? Spring provides either implementations or fully-factored API wrappers over these technologies: ◦ JDBC and DAOs ◦ ORM: Hibernate, iBatis, TopLink and others ◦ Declarative Transaction Support (without a full J2EE app server) ◦ Aspect-Oriented Programming ◦ Remote calls and Web Services (Axis) ◦ EJBs
  • 19. Resources Spring Website: http://www.springframework.org/ Download Spring with sample applications: http://prdownloads.sourceforge.net/springframework/spring-framework-1.2.8-with-dependencies.zip?downlo Rod Johnson's book on Spring: http://www.powells.com/biblio?PID=719&cgi=product&isbn=0764574833 Wikipedia: http://en.wikipedia.org/wiki/Spring_Framework_%28Java%29 EasyMock: http://www.easymock.org/ Echo2 (Rich Web Inteface framework) http://www.nextapp.com/platform/echo2/echo/