Spring:
- Spring is an open source framework.
- Spring is for simplifying enterprise application development, and using spring enables simple JavaBean to implement functionality that was previously only available to EJBS.
- 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:
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:
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