The first hello world program for spring learning

Spring is an open source framework. Spring is a lightweight java development framework rising in 2003. It is derived from some concepts and prototypes described by rod Johnson in his book expert one on one J2EE development and design. It is created to solve the complexity of enterprise application development. One of the main advantages of the framework is its layered architecture, which allows users to choose which component to use, and provides an integrated framework for J2EE application development. Spring uses basic JavaBeans to do things that previously could only be done by EJBs. However, the use of spring is not limited to server-side development. From the perspective of simplicity, testability and loose coupling, any Java application can benefit from spring. The core of spring is inversion of control (IOC) and aspect oriented (AOP). In short, spring is a layered Java Se / EE full stack (one-stop) lightweight open source framework.

Overview of spring module framework diagram:

IOC of spring

First, let's talk about IOC (inversion of control). This is the core of spring and runs through it. The so-called IOC, for the spring framework, is that spring is responsible for controlling the life cycle of objects and the relationship between objects. What does this mean? For a simple example, how do we find girlfriends? The common situation is that we go everywhere to see where there are beautiful and beautiful women OK mm, then ask about their hobbies, QQ number, phone number, IP number, IQ number... Find a way to get to know them, give them what they want, and then hey... This process is complex and profound. We must design and face each link ourselves. The same is true for traditional program development. If you want to use another object in an object, you must get it (new one yourself, or query one from JNDI). After using it, you have to destroy the object (such as connection). The object will always be coupled with other interfaces or classes.

So how does IOC do it? It's a bit like looking for a girlfriend through a marriage agency. A third party was introduced between my girlfriend and me: a marriage agency. The matchmaking agency manages a lot of information about men and women. I can put forward a list to the matchmaking agency and tell it what kind of girlfriend I want, such as looking like Li Jiaxin, building like Lin Xilei, singing like Jay Chou, speed like Carlos and technology like Zidane. Then the matchmaking agency will provide a mm according to our requirements. We just need to fall in love with her Just get married. It's simple and clear. If the person referred to us doesn't meet the requirements, we'll throw an exception. The whole process is no longer controlled by myself, but by a container like organization such as marriage agency. This is the development method advocated by spring. All classes will be registered in the spring container to tell spring what this is and what you need. Then spring will take the initiative to give you what you want when the system runs to the appropriate time, and give you other things you need at the same time. The creation and destruction of all classes are controlled by spring, which means that the object life cycle is no longer controlled by the object that references it, but by spring. For a specific object, it used to control other objects, but now all objects are controlled by spring, so this is called control inversion. If you don't understand, I decide to give up.

A key point of IOC is to dynamically provide an object with other objects it needs during system operation. This is through di (dependency injection). For example, object a needs to operate the database. In the past, we always wrote our own code in a to obtain a connection object. With spring, we just need to tell spring that a connection is needed in A. as for how and when to construct this connection, a does not need to know. When the system is running, spring will Create a connection at an appropriate time, and then inject it into a like an injection, so as to complete the control of the relationship between various objects. A needs to rely on a connection to run normally, and this connection is injected into a by spring. That's the name of dependency injection. So how is di implemented? An important feature after Java 1.3 is reflection (reflection), which allows programs to dynamically generate objects, execute object methods and change object properties when running. Spring implements injection through reflection. For information about reflection, please refer to Java doc. After understanding the concepts of IOC and Di, everything will become simple and clear. The rest is just building blocks in the spring framework.

Spring's first hello world program

Create a new Java project named spring-0.

Then create a new lib folder under the SRC directory, assign the following jar packages, and then select to add all the jar packages under the Lib folder to the project build path to create a new HelloWorld Java file, stored in com In the Hello package, the final project directory structure is as follows:

HelloWorld. The Java content is as follows:

Then on COM Create a new main in the Hello package Java to call the HelloWorld class, main The Java content is as follows:

Of course, we haven't had any interaction with spring here, just ordinary calling class methods. Let's get to the point. First, click the SRC folder, right-click new - > file, and then name it ApplicationContext XML, which configures the dependencies between the called classes.

applicationContext. The contents of the XML file are as follows:

Finally, modify main Java file, as follows:

The final output results are as follows:

As you can see, we are in main Java does call our ApplicationContext Note: before the spring IOC container reads the bean configuration and creates a bean instance, it must be initialized. Only after the container is instantiated can the bean instance be obtained from the IOC container and used.

reference resources

1. Spring (an open source framework created by rod Johnson)

2. Spring IOC principle (after reading, you can write a spring by yourself)

3. How can novice Java learn the three frameworks of spring, struts and Hibernate?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>