Spring learning: Create a project Hello Spring instance code, hellospring

Source: Internet
Author: User

Spring learning: Create a project Hello Spring instance code, hellospring

This article focuses on creating the Hello spring instance code for the Spring learning project. The details are as follows.

1. Create an eclipse project and introduce the jar package

1. Create a java project HelloSpring in eclipse

2. Create the lib directory and add 5 jar packages required by spring.

3. Right-click five files and choose Build Path> add to build path.

2. Compile spring's hello spring code

1. Create the io. spring. beans package and write HelloWorld. java

Package io. spring. beans;/*** @ author fat character alex e-mail: zanbin168@qq.com * @ version 1.0 */public class HelloWorld {private String name; public void setName (String name) {this. name = name;} public void hello () {System. out. println ("hello" + name );}}

2. Right-click src and choose create spring bean configuration file applicationContext. 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.xsd"> <! -- Configure bean --> <bean id = "helloWorld" class = "io. spring. beans. helloWorld "> <property name =" name "value =" "> </property> </bean> </beans>

3. Compile Main. java

Package io. spring. beans; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext;/*** @ author fat character alex e-mail: zanbin168@qq.com * @ version 1.0 */public class Main {public static void main (String [] args) {// 1. Create the Spring IOC container object ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext. xml "); // 2. Obtain the Bean instance HelloWorld helloWorld = (HelloWorld) ctx from the IOC container. getBean ("helloWorld"); // 3. Call the hello method helloWorld. hello ();}}

Output result

When the red spring log is printed on the console, the spring application is successful.

Summary

The above is all about the code for creating the Hello spring instance project in Spring learning. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

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.