Spring Tutorial (iv) Hello World instance

Source: Internet
Author: User
Tags unique id tutorialspoint

Hello World Instance

Let's start the actual programming using the Spring framework. Before you start writing the first example using the spring framework, you must make sure that the spring environment is set up correctly, as described in the spring--Environment Setup tutorial. Let's say you have some knowledge about the work of Eclipse IDE.

So let's continue to write a simple Spring application that will output "Hello world!" based on the information configured in the Spring Beans configuration file "or other information.

1th step: Create a Java project

The first step is to use the Eclipse IDE to create a simple Java project. Follow the options File, New, Project, and finally select the Java Project Wizard from the list of wizards. Now, use the wizard window to name your project hellospring, as follows:

Once your project has been successfully created, you will see the following in Project Explorer:

2nd Step: Add the required libraries

The second step allows us to add the Spring framework and common log API libraries to our project. To do this, right-click on your project name hellospring and follow the options available below on the shortcut menu: Build path, Configure build path, displays the Java Build Paths window as follows:

Now, using the available Add External JARs button in the Libraries tab, add the core JAR files from the Spring framework and the Common Log installation directory:

    • commons-logging-1.1.1

    • Spring-aop-4.1.6.release

    • Spring-aspects-4.1.6.release

    • Spring-beans-4.1.6.release

    • Spring-context-4.1.6.release

    • Spring-context-support-4.1.6.release

    • Spring-core-4.1.6.release

    • Spring-expression-4.1.6.release

    • Spring-instrument-4.1.6.release

    • Spring-instrument-tomcat-4.1.6.release

    • Spring-jdbc-4.1.6.release

    • Spring-jms-4.1.6.release

    • Spring-messaging-4.1.6.release

    • Spring-orm-4.1.6.release

    • Spring-oxm-4.1.6.release

    • Spring-test-4.1.6.release

    • Spring-tx-4.1.6.release

    • Spring-web-4.1.6.release

    • Spring-webmvc-4.1.6.release

    • Spring-webmvc-portlet-4.1.6.release

    • Spring-websocket-4.1.6.release
3rd Step: Create a source file

Now let's create the actual source file under the Hellospring project. First, we need to create a package called Com.tutorialspoint. To do this, right-click on the SRC in the package explore area and follow the options: New---package.

Next, we create the Helloworld.java and Mainapp.java files under package com.tutorialspoint.

Here is the contents of the Helloworld.java file:

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

The following is the contents of the second file, Mainapp.java:

package com.tutorialspoint;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.getMessage(); }}

There are two key points to note about the main procedure:

    • The first step is to use the framework API Classpathxmlapplicationcontext () to create the context of the application. This API loads the beans configuration file and ultimately based on the provided API, which handles the creation and initialization of all objects, the beans mentioned in the configuration file.

    • The second step is to use the Getbean () method of the created context to get the bean you want. This method uses the bean's ID to return a generic object that can eventually be converted to the actual object. Once you have an object, you can use this object to invoke methods of any class.
4th step: Create a Bean configuration file

You need to create a Bean's configuration file, which is an XML file, and as an adhesive for the glue bean, the class. This file needs to be created in the SRC directory, as shown in:

Usually the developer saves the file with the name Beans.xml file, but you can choose any name you like individually. You must make sure that this file is available in CLASSPATH, and use the same name in the main application, while creating the context of the application in the Mainapp.java file.

Beans.xml is used to assign unique IDs to different beans and to control the creation of objects of different values without affecting any of Spring's source files. For example, using the following file, you can pass any value for the "message" variable, so you can output different values of the information without affecting the Helloworld.java and Mainapp.java files. Let's take a look at how it works:

<?xml version= "1.0" encoding= "UTF-8"?><Beansxmlns="Http://www.springframework.org/schema/beans"xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" Span class= "Hljs-attribute" >xsi:schemalocation= http://www.springframework.org/schema/ Beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <bean id= "HelloWorld" class=" Com.tutorialspoint.HelloWorld "> << Span class= "Hljs-title" >property name= "message" value= "Hello world!" /> </bean></ BEANS>             

When the Spring application is loaded into memory, the framework leverages the above configuration file to create all the defined beans, and assigns them a unique ID according to the label's definition. You can use tags to pass values that use different variables when you create an object.

5th step: Run the program

Once you have finished creating the source code and the bean's configuration file, be ready to compile and run your program next. To do this, keep the Mainapp.java file tag valid and use the available Run options in the Eclipse IDE, or compile and run your application Mainapp using Ctrl + F11. If everything is working correctly in your application, the following information will be printed in the Eclipse IDE console:

Your Message : Hello World!

Congratulations, you've succeeded in creating your first Spring application. You can see the flexibility of the Spring application above by changing the value of the "message" property and keeping the two source files intact. Next, let's start doing something more interesting in the next few chapters.

Original source: http://wiki.jikexueyuan.com/project/spring/hello-world-example.html

Spring Tutorial (iv) Hello World instance

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.