Spring Learning--helloworld

Source: Internet
Author: User

Spring:

    1. Spring is an open source framework.
    2. Spring is for simplifying enterprise application development, and using spring enables simple JavaBean to implement functionality that was previously only available to EJBS.
    3. Spring is an IOC and AOP container framework.

Spring Frame diagram:

Let's start with Spring's first classic project: HelloWorld

HelloWorld for Normal mode:

1  PackageCom.itdjx.spring.beans;2 3 /**4  * @authorWáng Chéng dá5 * @create 2017-02-28 10:386  */7  Public classHelloWorld {8 9     PrivateString name;Ten  One      PublicString GetName () { A         returnname; -     } -  the      Public voidsetName (String name) { -          This. Name =name; -     } -  +      Public voidHello () { -System.out.println ("Hello" + This. GetName ()); +     } A}
 Package Com.itdjx.spring.beans; /**  @author*/Publicclass  Main    {public  staticvoid  main (string[] args) {        new  HelloWorld ();        Helloworld.setname ("ITDX");        Helloworld.hello ();    }}

Console output:

Hello Itdx

HelloWorld in Spring mode:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >5 6     <!--Accessory Bean -7     <BeanID= "HelloWorld"class= "Com.itdjx.spring.beans.HelloWorld">8         < Propertyname= "Name"value= "String"/>9     </Bean>Ten </Beans>
1  PackageCom.itdjx.spring.beans;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 /**7  * @authorWáng Chéng dá8 * @create 2017-02-28 10:399  */Ten  Public classMain { One  A  -      Public Static voidMain (string[] args) { -  the         //Spring Mode -         /** - * 1. Create a Spring IOC container - * 2. Get the Bean instance from the IOC container +          */ -ApplicationContext CTX =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); +HelloWorld HelloWorld = (HelloWorld) ctx.getbean ("HelloWorld"); A Helloworld.hello (); at  -     } -}

Console output:

Hello String


as can be seen from the console output, the value of the property in HelloWorld is already assigned in the configuration file <property name= "name" value= "String"/> , so The value of the Name property in HelloWorld corresponds to the value of value.

If you modify Helloworld in SetName to SetName1, the configuration file <property name= "name" value= "String"/> The name attribute value needs to be changed to Name= "name1".

Here's a more intuitive look at the spring framework's loading process:

1  PackageCom.itdjx.spring.beans;2 3 /**4  * @authorWáng Chéng dá5 * @create 2017-02-28 10:386  */7  Public classHelloWorld {8 9     PrivateString name;Ten  One      PublicString GetName () { A         returnname; -     } -  the      Public voidsetName (String name) { -System.out.println ("HelloWorld setName ():" +name); -          This. Name =name; -     } +  -      Public voidHello () { +System.out.println ("Hello" + This. GetName ()); A     } at  -      PublicHelloWorld () { -System.out.println ("HelloWorld Constructor ..."); -     } -}

1  PackageCom.itdjx.spring.beans;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 /**7  * @authorWáng Chéng dá8 * @create 2017-02-28 10:399  */Ten  Public classMain { One  A  -      Public Static voidMain (string[] args) { -  the         //Spring Mode -         /** - * 1. Create a Spring IOC container - * 2. Get the Bean instance from the IOC container +          */ -ApplicationContext CTX =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); + //HelloWorld HelloWorld = (HelloWorld) ctx.getbean ("HelloWorld"); A //Helloworld.hello (); at  -     } -}

Console output:

HelloWorld Constructor ...
HelloWorld setName (): String

From the above code and console output, we can see that spring has initialized the HelloWorld when it creates the spring IOC container. The parameterless constructor is first loaded to initialize the HelloWorld object, and then the SetName () method is called to assign values to the initialized object.

Configuration file relationship to the class:

Spring Learning--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.