SPRINGMVC Learning (what is IOC)

Source: Internet
Author: User

The next time, I will write a blog column about SPRINGMVC, first of all to see how Baidu Encyclopedia on the definition of SPRINGMVC.

         Spring is an open source framework,Springwas a lightweight Java development framework that emerged in the 2003,Rod JohnsonIn his book expert One-on-one development and designelaboratedpart of the concept and prototype derived from. 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, whileJ2ee ApplicationDevelopment provides an integrated framework. Spring uses the basicJavaBeanto accomplish things that were previously only possible by EJBS. However, Spring's use is not limited toServerdevelopment of the terminal. From the standpoint of simplicity, testability, and loose coupling, any Java application can benefit from spring. The core of spring isControl reversal(IoC) and face-tangent (Aop). In simple terms, spring is a layered javase/eeFull-stack (one-stop) LightweightOpen source Framework.

Let's take a look at spring's official picture for us:


is not to see the foggy, what is the IOC, what is the AOP and what is Springmvc, the next series of blog, I will take you to learn spring gradually. Let's begin by learning the spring IOC module.

The IOC is also known as inversion of control, which is plainly the creation of objects, destruction and initialization of a series of operations to the spring container to handle, by spring to control the object's declaration period. For a chestnut: we now write a lot of Java classes, and then write a spring configuration file that gives these classes to the spring container for processing, where initialization and destruction are managed by spring, and if we need to use objects, Just follow the specifications in spring to write the code, you can get the specified object, for what kind of specification should be followed, this is discussed in detail later. This is our IOC (the origin of control reversal). The object is given to spring to control management. So what are the advantages that the IOC gives us? Think about it, now if we need to use a certain class object, only by manual new out, that someone might say, I can not use the Factory mode? Note: Factory mode simply provides us with a simple call to create an object, and the inside of it is also the new one we need, but if we use spring, we just need to configure the spring configuration file with a class that needs to be managed by spring. The specific creation and management of the object is done by spring. Needless to say, let's first look at an example:

We created a new Java project to copy the core jar packages that spring needed.

1.dist/spring.jar

2.lib/jakarta-commons/commons-logging.jar

These two are the core jar files that spring needs, and then we'll create a new class Helloioc.java

Package Com.test.ioc;public class Helloioc {public void Helloioc () {System.out.println ("Hello IOC First");}}

Next, we show you how to manage the Helloioc class with spring. First, I create a new Applicationcontext.xml file: The contents are as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" <span Style= "White-space:pre" ></span>xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" <span style= " White-space:pre "></span>xsi:schemalocation=" Http://www.springframework.org/schema/beans HTTP://WWW.SPR Ingframework.org/schema/beans/spring-beans-2.5.xsd "><span style=" White-space:pre "></span>< Bean id= "HELLOIOC" class= "Com.test.ioc.HelloIoc" ><span style= "White-space:pre" ></span><span Style= "White-space:pre" ></span></bean></beans>
Well, someone might ask, how can I remember so many things, I don't have to worry, I found them in the official documentation. As you can see, the name of the root node here is called Beans, which contains a lot of other beans, where a bean is a concrete class.

Note: The ID in the bean here is the one that uniquely identifies a bean, usually the first letter of the class name lowercase. Class as the name implies is the full class name of the class, when you write, you can hold down the CTRL key and then click, if you can enter, the value of class is correct.

Now that the spring container has been created, let's write a test class, load the spring container, and take the helloioc out of the container.

Package Com.test.ioc;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Helloioctest {public static void Main (string[] args) {ApplicationContext applicationcontext = new Classpathxmlapplicationcontext ("com/test/ioc/ Applicationcontext.xml "); HELLOIOC HELLOIOC = (HELLOIOC) applicationcontext.getbean ("Helloioc"); Helloioc.helloioc ();}}
By running our main method, we will find that the HELLOIOC () method has been executed and found that we did not create it in a way new HELLOIOC, but instead created it for us through the spring container. It is important to note that ApplicationContext is our context, which loads the configuration file and then passes

Applicationcontext.getbean ("Helloioc") to get our entity class, where the parameters in Getbean are the IDs of the beans we configured in the spring configuration file.

Next we add the construction method to the HELLOIOC, as follows:

Package Com.test.ioc;public class Helloioc {public void Helloioc () {System.out.println ("Hello IOC First");} Public Helloioc () {super (); System.out.println ("HELLOIOC has been Init");}}
Then two times get the object of the HELLOIOC, as follows:

ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Com/test/ioc/applicationcontext.xml"); HELLOIOC HELLOIOC = (HELLOIOC) applicationcontext.getbean ("Helloioc"); HELLOIOC helloIoc2 = (HELLOIOC) applicationcontext.getbean ("Helloioc"); System.out.println (HELLOIOC); System.out.println (HELLOIOC2);
At this point, the console prints the result:

HELLOIOC has been init
[Email protected]
[Email protected]

It can be found that although we have two HELLOIOC objects, the two objects are the same, and we find that the constructor has been executed once, which is a good illustration of the singleton pattern that spring defaults to creating objects for us. However, there is a problem here, that is, in the case of Singleton mode, there will be a thread safety problem, for example, I am now adding a string name variable to HELLOIOC, and provide the Get,set method:

Package Com.test.ioc;public class Helloioc {private String name = "";p ublic void Helloioc () {System.out.println ("Hello IOC First ");} Public Helloioc () {super (); System.out.println ("HELLOIOC has been Init");} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
The code for the test class is then modified as follows:

ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Com/test/ioc/applicationcontext.xml"); HELLOIOC HELLOIOC = (HELLOIOC) applicationcontext.getbean ("Helloioc"); HELLOIOC helloIoc2 = (HELLOIOC) applicationcontext.getbean ("Helloioc"); Helloioc.setname ("xiaoming"); Helloioc2.setname ("test"); System.out.println (HELLOIOC); System.out.println (HELLOIOC2); System.out.println (Helloioc.getname ()); System.out.println (Helloioc2.getname ());
The results of this printing are as follows:

HELLOIOC has been init
[Email protected]
[Email protected]
Test
Test


According to the results we can see that now because of the singleton mode, there is a helloIoc2 to change the name variable of HELLOIOC. So how to avoid the occurrence of this problem, in fact, in the configuration file added such a sentence "scope=" prototype "" can cancel the default singleton mode to create the bean. As follows:

<bean id= "Helloioc" class= "Com.test.ioc.HelloIoc" scope= "prototype" ></bean>
The printing results are as follows:

HELLOIOC has been init
HELLOIOC has been init
[Email protected]
[Email protected]
Xiaoming
Test

As you can see, Spring has created two different objects for us.


At this point we have used the spring container to create our objects, and now we are going to demonstrate the initialization and destruction of objects.

In the spring configuration file, in the bean's configuration file, Spring has provided us with a way to set the object's initialization and destruction: Init-method= "" destroy-method= "" can see such a setting on it. Next I create a new class to demonstrate the behavior of initialization and destruction in the spring operands.

Package Com.test.ioc;public class Helloiocinitdes {public helloiocinitdes () {super (); System.out.println ("Helloiocinitdes runs..");} public void init () {System.out.println ("Init runs ..."); public void Destroy () {System.out.println ("Destroy runs ...");}}
Remember that as long as we need to have spring to manage the object, we must configure the Bean in spring's configuration file, where I add the initialization and destruction method settings.

<bean id= "Helloiocinitdes" class= "Com.test.ioc.HelloIocInitDes" init-method= "Init" destroy-method= "destroy" > </bean>
Then create a new Helloiocinitdestest.java to test the initialization and destruction operations. The code is as follows:

ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Com/test/ioc/applicationcontext.xml"); Helloiocinitdes helloiocinitdes = (helloiocinitdes) applicationcontext.getbean ("Helloiocinitdes");
At this point, run thethe Main method of Helloiocinitdestest.java, the result is as follows:

Helloiocinitdes runs.
Init runs ...
You can see that the Init method was executed after the method was constructed, but we found that the Init method was executed, and the Destroy method did not execute, because spring is not ready to destroy our objects, we now simulate closing the spring container, To allow the Destroy method to be executed.

ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Com/test/ioc/applicationcontext.xml"); Helloiocinitdes helloiocinitdes = (helloiocinitdes) applicationcontext.getbean ("Helloiocinitdes"); Classpathxmlapplicationcontext context = (Classpathxmlapplicationcontext) applicationcontext;context.close ();
This is printed as follows:


You can see that the destroy method has been executed at this time.

However, if I add such a word to the bean configuration file in Helloiocinitdes at this point: scope= "Prototype", that is, the object is not created with the default singleton mode, the Destroy-method is not executed. Even though I closed the spring container.

To summarize: The init-method= "Init" method is executed by the spring container after the method is constructed, and we can use him to do some initialization operations, such as shutting down resources in Destroy-method , If the bean is not created by default singleton mode, Destroy-method will not be executed at this time . Then close the resources and other operations, will be done by artificial.

So when did spring help us create the Bean object?? There is a configuration in the bean's configuration file: lazy-init= "", the configuration has two values:

The default for lazy-init= "default" and lazy-init= "true" is false. Here, when I lazy-init= "Default", the Bean object is created when the spring container is loaded into memory, and if lazy-init= "true", the Bean object is created only when the Gebean ("") method is executed.


Notice that all of the beans in the spring container we are currently using are configured with constructors to create the Bean object, and then I'll describe a way to create a bean object using a static factory. First I create a new Helloiocfactory.java inside defines a getinstance method for creating HELLOIOC objects.

Package Com.test.ioc;public class Helloiocfactory {public static HELLOIOC getinstance () {return new HELLOIOC ();}}
Then configure this in the bean:

<bean id= "helloIoc2" class= "Com.test.ioc.HelloIocFactory" factory-method= "getinstance" ></bean>
At this point, if you want to get to the Helloioc object, you just need to pass the Getbean ("") method call as usual.

Today's SPRINGIOC is here, next I will write a spring Dependency injection blog, I hope you like.

SOURCE download




















SPRINGMVC Learning (what is IOC)

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.