tags:

views:

174

answers:

3

My question is : from basic which are the necessary jars that should required in Spring and how could we configure Spring project ?

+5  A: 

Go to Spring home page and download Spring (Here, i am using 2.5.x version)

After installing, put the following jar in your classpath

<SPRING_HOME>/dist/spring.jar

Here goes a single bean

package br.com.introducing.Hello;

public class Hello {

    private String message;

    // getter's and setter's

}

...

Write a single xml to configure your beans as follows

// app.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;
    <bean id="hello" class="br.com.introducing.Hello">
        <property name="message" value="What do you want ?"/>
    </bean>
</beans>

Put your app.xml in root classpath

And your psvm

public static void main(String [] args) {
    ApplicationContext appContext = new ClassPathXmlApplicationContext("app.xml");

    Hello hello = (Hello) appContext.getBean("hello");

    hello.getMessage(); // outputs What do you want ?
}

UPDATE

What is the role of the applicationContext.xml

When using getBean method, it behaves like a Factory pattern. Something like

public class ApplicationContext {

    Map wiredBeans = new HashMap();

    public static Object getBean(String beanName) {
        return wiredBeans.get(beanName);
    }

}

As said by Spring in Action book

It is a general-purpose factory, creating and dipensing many types of bean.

But, There is more

  • Allows you load files
  • You can publish events
  • It supports i18n (i18n stands for internationalization)

Suppose here goes messages.properties (root of the classpath)

// messages.properties

messsageCode=What do you want ?

To enable i18n, You must define a bean called messageSource to get advantage of our resource, as follows

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
    </bean>
</beans>

Now, you can use it

appContext.getMessage("messsageCode", null, null); // outputs What do you want ?

Usually, we do not need to define all of our beans in xml file. You can use annotations (Additional settings needed to enable component scanning) instead of xml, Something like

package br.com.introducing.Hello;

@Component
public class Hello {

    private String message;

    // getter's and setter's

}

Component annotation says:

Spring, i am a general-purpose bean which can be retrieved through application context

A good resource about Spring is either Spring in Action book or Spring documentation

Advice: read carefully

Arthur Ronald F D Garcia
what is the role of the applicationContext.xml in the spring?
saloni
@saloni Added to original answer
Arthur Ronald F D Garcia
A: 

You can also use Maven to create and manage projects. You can get an idea about Maven and how to start from here

A directory structure will be created by Maven and there will be a pom.xml inside your project directory. You can mention all dependencies in this file. Eg: for using spring, you can mention the dependency as follows,

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.5.3</version>
</dependency>

If you are using Eclipse as IDE, you need to execute the following command,

mvn eclipse:eclipse

This will create a .project file. You can now import the project into Eclipse IDE and start coding your application.

For beginners, Spring reference documentation and books like Spring in Action and Spring Recipes are very useful

A: 

You can have a look at the article on understanding the webapplicationcontexts and other xml config files in spring

Think this can help you in getting the configurations related to basic spring MVC with ease

javaprogrammer