Spring configuration and a spring HelloWorld

Source: Internet
Author: User

Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka

This article will be the presenter of Spring's configuration under Eclipse and run the first HelloWorld with spring.

First, download the required documents

Here we have configured the Java operating environment and installed eclipse.

Download Spring

: http://projects.spring.io/spring-framework/

Download commons-logging

: http://commons.apache.org/proper/commons-logging/download_logging.cgi

Download them and unzip them where you want them to be, and remember to check whether they are 32-bit or 64-bit before downloading.

Second, configure Spring

1, a new project, called Springhelloworld.

2, add spring3.x package, online There are many different methods. I'm only talking about one here.

In Window->preferences->java->build path->user libraries->new add a library of user packages, here's why the spring package is much more, we do this, Once configured, each project will have to be added directly to the library at a later time.


Named Spring3.2, click OK


Click Add External JARS. In the pop-up window, select the location of the Spring Libs package (see your decompression location) and add the jar you used.


After adding success

To add to the project:

Select New Project-"Properties->java Build Path->add Library

Select User Library in the pop-out window

Then you will jump out of a window, then you can choose our previous configuration of the user library package Spring3.2, the ditch hit.

Add success

And then you can see the added Spring3.2 in the project.

Third, add commons-logging

Select Project-"Properties->java Build Path->add Library

Then choose the package that commons-logging is on.

Added succeeded.

Iv. start of Spring programming

Well, when the above configuration is done, we can start the first HelloWorld.

1. First create a new Helloworld.java under the current package

Package com.test;/** * Spring First HelloWorld * @author Lin Bingwen (email [email protected] Blog: Http://blog.csdn.net/evankaka) * @time 20 15.4.1 */public class HelloWorld {private string Info;public string GetInfo () {return info;} public void SetInfo (String info) {this.info = info;}}

2, write the configuration file Applicationcontext.xml

Under the current project

This is the addition of successful

Then change the Applicationcontext.xml content to read 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"    xmlns:p= "http://www.springframework.org/schema/ P "    xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans-3.0.xsd ">      <!--configuration of beans that need to be managed by spring (created, placed in the spring IOC container)--    <bean id=" Hello "class=" Com.test.HelloWorld ">        <!--Configure the attributes that the bean needs to inject (which is injected through the property set method)--        <property name=" Info "value=" Happy New year! " />    </bean></beans>
3. Start of Reverse control

Add the following in Main.java:

/** * Spring First HelloWorld * @author Lin Bingwen (email [email protected] Blog: Http://blog.csdn.net/evankaka) * @time 2015.4.1 */package C Om.test;import Org.springframework.beans.factory.beanfactory;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Main {private String who = null; public static void Main (string[] args) {//Get spring's applicationcontext configuration file, inject into IOC container//(Map:key:String, bean tag id attribute value = = >value:object, an instance of the class that the Bean tag class attribute refers to) beanfactory factory = new Classpathxmlapplicationcontext (" Applicationcontext.xml "); HelloWorld hw1 = (HelloWorld) factory.getbean ("Hello");//map.get ("Hello") System.out.println (Hw1.getinfo ()); System.out.println (HW1);}}

Then select the project right-click:

The next step is to output the results:


Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka

Spring configuration and a spring HelloWorld

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.