Professional Documents
Culture Documents
Once your project is created successfully, you will have following content in your Project
Explorer:
After Next add user library name as spring-lib and add following minimum required jar files
to this user library :
antlr-runtime-3.0.2
org.springframework.aop-3.2.0.M1
org.springframework.asm-3.2.0.M1
org.springframework.aspects-3.2.0.M1
org.springframework.beans-3.2.0.M1
org.springframework.context.support-3.2.0.M1
org.springframework.context-3.2.0.M1
org.springframework.core-3.2.0.M1
org.springframework.expression-3.2.0.M1
commons-logging-1.1.1
HelloWorld.java
view plainprint?
1. package com.sdnext.dineshonjava.tutorial;
2.
3. public class HelloWorld {
4.
5.
6.
7.
8.
9.
10.
11.
12.
13. }
SpringTestApp.java
view plainprint?
1. package com.sdnext.dineshonjava.tutorial;
2.
3. import org.springframework.context.ApplicationContext;
4. import org.springframework.context.support.ClassPathXmlApplicationContext;
5.
6. public class SpringTestApp
7. {
8.
9.
10.
11.
12.
13.
//this step is used to get required bean using getBean() method of the created con
text
14.
15.
helloWorld.getMessage();
16.
17. }
There are following two important points to note about the main program:
1. First step is to create application context where we used framework API
ClassPathXmlApplicationContext(). This API loads beans configuration file and
eventually based on the provided API, it takes care of creating and initializing all the
objects ie. beans mentioned in the configuration file.
2. Second step is used to get required bean using getBean() method of the created context.
This method uses bean ID to return a generic object which finally can be casted to actual
object. Once you have object, you can use this object to call any class method.
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
3.
4.
5.
6.
7. </beans>
After creation of the bean configuration file(Spring.xml), now we put this file into "src"
directory of the application(i.e. which is present in classpath). Then configuration
file(Spring.xml) is loaded in the ApplicationContext as follows.
view plainprint?
1. ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Now suppose we put configuration file(Spring.xml) into the different directory package other
than "src" like "com.dineshonjava.sdnext.springConfig" then we have to give classpath of
configuration file(Spring.xml) to ApplicationContext as follows:
view plainprint?
1. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/din
eshonjava/sdnext/springConfig/spring.xml");
We can use wildcard for this as follows:
view plainprint?
1. ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:com/di
neshonjava/**/springConfig/spring.xml");
view plainprint?
1. ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:com/*/
**/springConfig/*-spring.xml");
When Spring application gets loaded into the memory, Framework makes use of the above
configuration file to create all the beans defined and assign them a unique ID as defined in tag.
You can use tag to pass the values of different variables used at the time of object creation.
Now run the program you will get following output on console.
Output:
Jun 16, 2012 4:45:17 PM org.springframework.context.support.AbstractApplicationContext
prepareRefresh
INFO: Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@145d068: startup date
[Sat Jun 16 16:45:17 IST 2012]; root of context hierarchy
In the Next chapter we will discuss about the IoC Contianer in its role in the Spring
Framework.