Beginner Java SSH's Spring second article

Source: Internet
Author: User

The last one we successfully set up the spring development environment, next to see what is the magic of spring (I am also a novice, there is not very good place to point out that the great God to make it easy to change, thank you).

Before I heard that spring is in the application of the XML file more, but how to use it is always foggy, oneself in the beginning when learning, because not directly new Web project, Not automatically generated applicationcontext.xml, that will not know how important this configuration file, so after writing the following piece of code, the runtime, always error, but also silly to think that the file will be automatically generated, now think really ridiculous. However, if you use Myeclipes to automatically import the Spring rack package, it will be automatically generated. Now remind everyone, for Applicationcontext.xml must first confirm its existence, then carry on the next code development. (For this problem, the gods don't have to look at it)

Applicationcontext.xml code I have given in the previous blog, I am stored in the SRC directory, here I give the link: http://www.cnblogs.com/speedwade/p/3968101.html

Next I wrote a test of the applet, to see if spring has succeeded in matching, the path I placed in the SRC directory under the Com.sep.basic package, create a new class class, named Springtest. Because I have been built, so I will prompt the file already exists, the first time the new one will not have this prompt appears,

Then the code is

1  PackageCom.sep.basic;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6  Public classSpringtest {7      Public Static voidMain (String args[])8     {9         //Create ApplicationContextTenApplicationContext CTX =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); One         //Output Spring Container A System.out.println (CTX); -     } -}

After you right-clicked run As/java application on the newly created file, a code similar to the following appears in the console:

1 org[email protected]b749757:startup Date [Mon Sep 17:21:18 CST]; Root of context hierarchy

Let's explain that in Springtest.java, we just created an instance of ApplicationContext, and he is the spring container, and we can successfully access the beans in the container only if we get the container. The output is exactly the ApplicationContext instance we created. Most of the functionality in spring is done through this container. So visible it's powerful.

Let's start by creating a simple bean to see how spring works.

Start by creating a simple Java class file named Firstbeantest with the path Com.sep.basic.service package. The code is:

1  PackageCom.sep.basic.service;2 3  Public classFirstbeantest {4     PrivateString name;5     //Setter method for the Name property6      Public voidsetName (String name) {7          This. Name =name;8     }9     //the Info method of the test classTen      Public voidInfo () One     { ASystem.out.println ("Firstbean ' s Name is" +name); -     } -}

The above gives a member variable with his set method.

Then add the following code in the Applicationcontext.xml:

1 <!--to deploy the Firstbeantest class to a bean in spring -2     <BeanID= "Firstbeantest"class= "Com.sep.basic.service.FirstBeanTest">3         < Propertyname= "Name"value= "Success"></ Property>4     </Bean>

Next go back to the Springtest file and add the following code:

1 firstbeantest FBT = Ctx.getbean ("Firstbeantest", Firstbeantest.  Class); 2 3 FBT. Info ();

OK, now run, you can see the output statement: Firstbean ' s Name is success

Isn't it amazing? We didn't manually create an instance of Firstbeantest, why would we be able to assign a value to it? This is the responsibility of spring, we only need to use the spring container to get the instance object, and can be assigned, which is the credit of spring, spring will be based on the configuration information, the corresponding instance, and set the corresponding property values.

This phenomenon is what we often hear about inversion of control (IOC).

  

Beginner Java SSH's Spring second article

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.