The Java Framework Spring (ii)

Source: Internet
Author: User

The previous blog describes some of the basic concepts of spring, let's create the first spring program.

The steps are as follows:

1) Guide Package

2) configuration file

Cases with no hint

MyEclipse->file and Editors-xml->xml catlog, click Add, locate the file above spring-beans-3.1.xsd, then Key type schemaloaction, ke Add spring-beans-3.1.xsd after Y.

//Settings for Beans.xml<?xml version= "1.0" encoding= "UTF-8"?><Beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" >//Here own according to need to set,give Userinf this class to spring management<bean name= "Userinfo_name"class= "Cat.beans.UserInfo" > <property name= "id" value= "1"/> <property name= "userName" value= "zhaomingming"/>            <property name= "password" value= "123"/> <property name= "Note" value= "This is the note"/> </bean> </beans>
 Public classTest { Public Static voidMain (string[] args) {//Initialize ContainerClasspathxmlapplicationcontext ctx=NewClasspathxmlapplicationcontext ("Beans.xml"); UserInfo User= (UserInfo) ctx.getbean ("Userinfo_name");//two equivalent methods are explained below                        /*This can be the case if there is only one userinfo_name in the Beans.xml.    * More, you cannot use this, so the introduction can be used with the following method *userinfo User=ctx.getbean (Userinfo.class); */                        //a method with parameters//UserInfo User=ctx.getbean ("Userinfo_name", userinfo.class);System.out.println (user); /*Attach: Initialize the container, you can also write *applicationcontext ctx= new Classpathxmlapplicationcontext ("Beans.xml"); * Because ApplicationContext is the parent class of Classpathxmlapplicationcontext*/            }}

Spring (ii) of the Java framework

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.