Learn spring2--with me Spring 3 (3) – development of the first HelloWorld application using spring

Source: Internet
Author: User
Tags unique id

Http://www.importnew.com/13246.html

  • Home
  • All articles
  • Information
  • Web
  • Architecture
  • Basic technology
  • Books
  • Tutorial
  • I want to contribute
  • More channels»
-nav Bar- Home all posts information web schema Basic technology books Option value= "http://www.importnew.com/cat/tutorial" > tutorial I want to contribute more channels» -IOS -python -android -Web front end Learn from Me Spring 3 (3) – Develop the first HelloWorld app with spring

2014/10/10 | Category: Tutorials | 5 Reviews | Tags: SPRING, tutorials

share to: This article Importnew-Tangxiaojuan without permission, prohibit reprint!

Let's use spring to write the first application.

Completing this chapter requires:

    • Familiarity with the Java language
    • Set up a spring-ready environment
    • Familiarize yourself with the simple Eclipse IDE's operations

If you have not yet set up your environment, refer to the configuration of the spring development environment.

Our first program is to print the "Hello World" statement, which is set by the spring configuration file.

-New Java Project:

The first step is to create a new project with the Eclipse IDE. Click > File > New > Java Project . Then enter the name of the project in the popup dialog, and we'll call HelloWorld it. This will create a new HelloWorld directory under your workspace as the root directory of the project.

Picture a new Java project

Click Finish . You will Project Explorer see the new project in the view. If Project Explorer it is not open, > Window > Show View find it in.

Figure Two Project Explorer view

Add Spring Library

The next step is to add the necessary spring libraries to classpath so that eclipse can find the required class when compiling and running the program.

Right-click in the button Package Explorer > Build Path > Configure Build Path... . Then click Add External JARs... to join the spring library we need. If you have not yet downloaded the spring library, please download the Spring Library first.

The spring libraries we need to join are:

    • org.springframework.aop-3.2.9
    • org.springframework.aspects-3.2.9
    • org.springframework.beans-3.2.9
    • org.springframework.context-3.2.9
    • org.springframework.context.support-3.2.9
    • org.springframework.core-3.2.9
    • org.springframework.expression-3.2.9

In addition, in order to print the information, we also need an Apache Commons Logging API, here to download commons-logging-1.2. At the time of this tutorial, the latest version is commons-logging-1.2. Unzip to any directory after downloading, I extract to ~/commons-logging-1.2 .

Then add the Commons-logging-1.2.jar to Classpath as you would add the Spring library.

Figure three adding the spring library

3–java Source Code

First create a new package "com.importnew". Right-click SRC, and then > New > Package create a new com.importnew package.

Then we need to com.importnew create a new two Java source file under the package HelloWorld.java and MainApp.java .

Helloworld.java:

123456789101112131415161718 package com.importnew;public class HelloWorld {     private String message;    public void setMessage(String message){        this.message  = message;    }    public String getMessage(){        return this.message;    }    public void printMessage(){        System.out.println("Your Message : " + message);    }}

Mainapp.java:

123456789101112131415 package com.importnew;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");        obj.printMessage();    }}
4– configuration file

Next, we need to create a new XML file to configure the bean, the role of which is to focus on one place, to configure and manage all the beans. For the bean concept, see the bean definition.

We also put the XML file src below so that eclipse can read the file under Classpath.

Create a new one Beans.xml , of course this file name is arbitrary, but should be ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml") consistent with the settings in Mainapp.java.

Beans.xml

12345678910111213     <?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">       <bean id="helloWorld" class="com.importnew.HelloWorld">           <property name="message" value="Hello World!"/>       </bean>    </beans>

The bean is set in this Beans.xml file and is contained in <beans> and </beans> . Each bean has a unique ID.

 

This statement assigns a value to the message variable so that it can be printed out Hello World! . To modify the output, you only need to modify the XML file without modifying Mainapp.java and Helloworld.java.

Directory of the entire HelloWorld project

Figure Four HelloWorld Project structure directory

5– Running the program

When you have completed the above steps, we can run the program.

Right-click on the Mainapp.java button > Run As > Java Application . You can also find the Run button on the toolbar.

If everything works, the output is as follows:

1 Your Message : Hello World!
Review the Operation process
    1. The main () statement is run first, and the spring framework uses the ClassPathXmlApplicationContext() first one to create a container.
    2. This container Beans.xml reads the configuration information from and creates the bean (that is, the object) based on the configuration information, with each bean having a unique ID.
    3. It then obtains a reference to the object by locating the bean for that context.getBean() ID.
    4. The method is called through the object's reference printMessage() to print the information.

Well, this is your first spring app. You've learned to create a new Java project with Eclipse, import the spring and commons-logging libraries, write Java source code and XML configuration files, and run successfully. If you want to change the output, you only need to modify the value values in the XML file without changing the Java source file.

In the following chapters, we will experience spring's more powerful features on this basis.

About Tangxiaojuan

A Cheng on the road

View more articles in Tangxiaojuan >>

+


Related articles
    • Study with Me Spring 3 (2) – Development environment configuration
    • Learn Spring with me 3 (1) – Why learn Spring
    • Spring MVC + Hibernate + Maven:crud Operation example
    • Understanding Spring MVC Model Attribute and Session Attribute
    • Spring Interview Quiz
    • Spring Interview Quiz Top 25
    • How to implement an XA, non-XA, spring Distributed transaction
    • Spring MVC Introductory Example explained
    • Spring's IOC principle
    • How to use spring to develop and monitor the thread pool service
Post a comment

5 reviews
  1. Flylee2014/10/10 11:40

    Support! Looking forward to the next tutorial

    1 0

    Reply
  2. neven72014/10/15 12:05

    Beans.xml articles are not in full

    0 0

    Reply
    • Tangxiaojuan 2014/10/31 2:20

      Thank you for your correction. Updated

      1 0

      Reply
  3. Mugbya2014/10/22 5:41

    Very good site, and Cheng's translation and original Nice

    0 0

    Reply
  4. z12157750542015/04/16 12:59

    Good article about getting started, like a

    0 0

    Reply
Comments from Weibo «java EE7 and Maven Engineering Primer (2) Getting started with Java EE7 and Maven Engineering (3)»

    • Hot Articles of the month
    • Popular Articles of the year
    • Popular tags

0 generic type? Super T and? The difference between extends T

1 Translation: Understanding weak references in Java

2Java servlet Working principle Quiz

3 How to deal with Interruptedexception

Introduction to 4Java Programming (2.4): text input and output

How does 5Java main be executed?

6 Exploring Java Strings

7 using Relproxy to improve Java development efficiency

Getting Started with 8Java programming (2.3): Classes, objects, and subroutines

Analysis of i=i++ problem in 9java

Latest comments
  • Re: Quickly and efficiently learn Java programming online Resources Top 20 This is written by three O of India, except that a few sites can be, most of them rubbish. Ryan
  • How does the Re:java I/O bottom work? MarkOniong
  • Re:netty Tutorial-part3-channelevent-Really? Ask a question. Ctx.getchannel (). Write (response). Addlistene ... Zhang Guolao Sheng
  • Re: Talking about Java string string constant stringbuffer string variable (thread safe) string ... Oniong
  • Re:java realization of single case difficult to understand the reflection code that bypasses the enumeration singleton protection ... Constructor con = fooenumsing ... Oniong
  • Re: in generics? Super T and? Extends t differs from public T $ (int id) {return (t) super.find ... Kenny
  • Re:java Programming Primer (2.4): text input and output are there 2.6 links to the section? Rocky
  • Re:java 9 features AI should be tested at the end, and if not, fall back to the normal method. Most of the situation is correct. we are

About Importnew

Importnew focuses on Java technology sharing. Officially launched on November 11, 2012 11:11. Yes, it's a very special occasion:)

Importnew consists of two Java keyword Import and new, meaning: Java Developers learn new knowledge of the site. Import can be thought of as learning and absorbing, and new can be thought of as knowledge, technology circles and new friends ...

Recommended attention

Groups – good topics, inspirational responses, trusted circles
Headline – wrote the article? See Dry goods? Go to the headlines!
Blind Date – A marriage platform for it single men and women
Resources – Excellent tool resource navigation
Translation – Active & professional translation Team
Blog – Featured blog posts at home and abroad
Front –javascript, HTML5, CSS
Android – Focus on Android technology sharing
ios– focus on iOS technology sharing
java– Focus on Java technology sharing
python– focus on Python technology sharing

Contact Us

Email:[email protected]
Sina Weibo: @ImportNew
Number: Importnew

Feedback suggestions: [Email protected]
Advertising and business cooperation qq:2302462408

©2015 importnew

Learn spring2--come with Me Spring 3 (3) – Develop the first HelloWorld app using spring

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.