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