Spring learns the first Hello World program

Source: Internet
Author: User
Tags ip number

Spring is an open source framework, and spring is a lightweight Java development framework that emerged in 2003 by Rod Johnson in his book expert one-on-one development and Some of the concepts and prototypes elaborated in design are derived. It is created to address the complexities of enterprise application development. One of the main advantages of the framework is its layered architecture, which allows the user to choose which component to use, while providing an integrated framework for Java EE application development. Spring uses basic JavaBean to accomplish things that were previously only possible by EJBS. However, the use of spring is not limited to server-side development. From the standpoint of simplicity, testability, and loose coupling, any Java application can benefit from spring. The core of spring is control inversion (IoC) and facet-oriented (AOP). In short, spring is a layered, javase/eefull-stack (one-stop) lightweight, open-source framework.

Spring Module Frame Chart at a glance:

Spring's IOC

First of all, let's talk about the IOC (inversion of control, controlled inversion). This is the core of spring, throughout. The so-called IOC, for the spring framework, is the responsibility of spring to control the object's life cycle and the relationship between objects. What does that mean, for example, how do we find a girlfriend? The common situation is that we go everywhere to see where there is a beautiful body and good mm, and then inquire about their interests, QQ number, telephone number, IP number, IQ number ..., find ways to know them, give them what they want, then hey ... The process is complex and profound, and we have to design and face each link ourselves. The same is true of traditional program development, in an object, if you want to use another object, you have to get it (your own new one, or a query from Jndi), after the use of the object will be destroyed (such as connection, etc.), the object will always and other interfaces or classes together.

So how does the IOC do it? It's kind of like finding a girlfriend through a dating agency, introducing a third party between me and my girlfriend: the Marriage Institute. Matchmaking management of a lot of men and women's information, I can give a list of matchmaking, tell it I want to find a girlfriend, such as like Michelle Reis, figure like Lin Xire, singing like Jay Chou, speed like Carlos, technology like Zidane, and then the matchmaking will be according to our requirements, provide a mm, We just have to go to love her and get married. As simple as it is, if a matchmaking person doesn't meet our requirements, we'll throw an exception. The whole process is no longer controlled by me, but by a similar container-like institution. This is how spring advocates for development, and all classes are registered in the spring container, telling spring what it is, what you need, and then spring will give you what you want when the system runs to the right time, and also give you what you need. All classes are created and destroyed by spring, which means that the object that controls the lifetime of the object is no longer a reference to it, but spring. For a specific object, it was previously controlled by other objects, and now all objects are controlled by spring, so this is called control inversion. If you do not understand, I decided to give up.

A key point of the IOC is to dynamically provide the other objects it needs to an object during the system's operation. This is achieved through DI (Dependency injection, Dependency injection). For example, object A needs to manipulate the database, before we always have to write code in a to get a connection object, with spring we just need to tell spring,a need a connection, as for this connection how to construct, when to construct , a does not need to know. When the system is running, Spring creates a connection at the right time, and then, like an injection, it injects into a, which completes the control of the relationship between the various objects. A relies on connection to function properly, and this connection is injected into a by spring, and the name of the dependency injection comes in. So how is di implemented? An important feature after Java 1.3 is reflection (reflection), which allows the program to dynamically generate objects, execute methods of objects, and change the properties of objects when it is run, and spring is injected through reflection. Refer to Java Doc for information on reflection.
With the understanding of the IOC and Di concepts, everything becomes straightforward and the rest of the work is just piling up wood in the spring frame.

Spring's first Hello World program

Create a new Java Project project with the name spring-0.

Then create a new Lib folder under the SRC directory, assign the following jar packages, and then choose to add the jar packages under the Lib folder to the project build path, Create a new Helloworld.java file, stored in the Com.hello package, and the final project directory structure is as follows:

Helloworld.java content is as follows:

1 package Com.hello; 2  3 public class HelloWorld {4     private String name, 5      6 public     HelloWorld () {7         System.out.println (" HelloWorld constructor ... "); 8     } 9     public     HelloWorld (String name) {One         System.out.println ("HelloWorld constructor with Paramter         this.name = name;13     }14 public     void SetName (String name) {         System.out.println ( "HelloWorld setName ...");         this.name = name;18     }19 public     void Hello () {         System.out.println ("Hello:" + this.name);     }23}

Then create a new Main.java in the Com.hello package to invoke the HelloWorld class, the Main.java content is as follows:

1 package Com.hello; 2  3 public class Main {4 public     static void Main (string[] args) {5         //1. Get HelloWorld instance 6         HelloWorld Hell Oworld = new HelloWorld (); 7         //2. Call instance in method 8         Helloworld.hello (); 9     }10}

Of course, here we do not have any interaction with spring, just the normal method of invoking the class, the following begins to get to the point. First, click the SRC folder, right mouse button new--file, and then named Applicationcontext.xml, which configures the dependency between the called classes.

The contents of the Applicationcontext.xml file are as follows:

1 <beans xmlns= "Http://www.springframework.org/schema/beans" 2     xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "3     xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-2.0.xsd "> 4      5     <!--configuration Bean, property configuration---6     < Bean id= "HelloWorld"  class= "Com.hello.HelloWorld" > 7         <property name= "name" value= "Spring" ></ Property> 8     </bean> 9     <!--Configure bean Properties by construction method-->11     <bean id= "HelloWorld2" class= " Com.hello.HelloWorld ">12         <constructor-arg value=" spring2 "index=" 0 "></constructor-arg>13     </bean>14 </beans>

Finally, modify the Main.java file as follows:

1 package Com.hello; 2  3 import org.springframework.context.ApplicationContext; 4 Import Org.springframework.context.support.ClassPathXmlApplicationContext; 5  6 public class Main {7 public     static void Main (string[] args) {8             //1. Create a Spring IOC container object 9             Applicationc Ontext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml");             2. Get the bean instance             from the IOC container HelloWorld HelloWorld = (HelloWorld) ctx.getbean ("HelloWorld");             3. Invoke the method in the instance             Helloworld.hello ();             HelloWorld helloWorld2 = (HelloWorld) ctx.getbean ("HelloWorld2"); Helloworld2.hello             ();     }19}

The final output results are as follows:

As you can see, we did invoke what we configured in Applicationcontext.xml in Main.java, note that it must be initialized before the spring IOC container reads the bean configuration to create the bean instance, only after the container is instantiated. You can get the bean instance from the IOC container and use it.

Spring learns the first Hello World program

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.