- Install Eclipse
- Install Maven Integration Plugin (check http://www.eclipse.org/m2e/)
- install Spring IDE plugin (check https://marketplace.eclipse.org/content/spring-ide)
- Maven Project contains these important folders:
- src/main/java (for core code)
- src/test/java (for unit test)
- pom.xml for adding dependencies
- when you create new Maven project you can choose one of the archetypes (predefined projects)
- You should define GroupId, ArtificatID, VersionID for your Maven Project.
- create Maven Project
- define Person class
- Add the following dependencies to pom.xml to use spring
- Add beans.xml to the project root, inside beans.xml you will define project beans, add Person bean.
- now we will create beans container and take a bean from it, in order to do that you should write the following:
- as you can see, creating beans continer happens by defining ApplicationContext, spring will open beans.xml and create the beans defined in it.
- you can get a bean by using context.getBean(BeanID)
- you dont need to write Person x = new Person(), as Spring initialize it for you.
- 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
- we used FileSystemApplicationContext to open beans.xml, there are other classes that could be used, we will talk about them later.
- 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: