Wednesday, January 1, 2014

Spring Course: V1 to V6 Getting Started

Setting up your system
  1. Install Eclipse
  2. Install Maven Integration Plugin (check http://www.eclipse.org/m2e/)
  3. install Spring IDE plugin (check https://marketplace.eclipse.org/content/spring-ide)
Introducing Maven

  1. Maven Project contains these important folders:
    1. src/main/java (for core code)
    2. src/test/java (for unit test)
  2. pom.xml for adding dependencies
  3. when you create new Maven project you can choose one of the archetypes (predefined projects)
  4. You should define GroupId, ArtificatID, VersionID for your Maven Project.
Spring HelloWorld
  1. create Maven Project
  2. define Person class
  3. Add the following dependencies to pom.xml to use spring
    1. Spring-core
    2. Spring-beans
    3. Spring-context

  4. Add beans.xml to the project root, inside beans.xml you will define project beans, add Person bean.

  5. now we will create beans container and take a bean from it, in order to do that you should write the following:
  6. as you can see, creating beans continer happens by defining ApplicationContext, spring will open beans.xml and create the beans defined in it.
  7. you can get a bean by using context.getBean(BeanID)
  8. you dont need to write Person x = new Person(), as Spring initialize it for you.
  9. you should close the context at the end to avoid memeory leak, as ApplicationContext doesnt have a close() method, you should cast the context to close it
  10. we used FileSystemApplicationContext to open beans.xml, there are other classes that could be used, we will talk about them later.
  11. you can use BeanFactory instead of ApplicationContext.
What is a context 
a context is an interface to the container, you take beans from the context 

Class Path Context:
  1. instead of using FileSystemXmlApplicationContext to open the beans, you can use ClassPathXmlApplicationContext, which can open beans.xml defined in the classpath.
  2. for example we will move beans.xml to a Package, then we open it as follows:

No comments:

Post a Comment