1. Steps for development (general overview)
(1). Prepare Jar Package
(2). Development HelloWorld program
(3). in the Applicationcontext.xml Complete the configuration ( xsd-config.html )
(4). start the container, get the bean from the container, call the method in the bean
2. Steps
(1). jar Package to import
Spring-core-3.2.0.release.jar
Spring-beans-3.2.0.release.jar
Spring-context-3.2.0.release.jar
Spring-expression-3.2.0.release.jar
Com.springsource.org.apache.commons.logging-1.1.1.jar
(2). General Pojo Class
The role of the package cn.wwh.www.spring.aaa.helloworld;/** * Class: * * * @author Skiff * @version 1.0 *@ created: 2014-9-21 pm 03:22:26 */pu Blic class HelloWorld {public void Show () {System.out.println ("Hello world! This is my first spring! ");}
(3) Configuring the Helloworld.xml file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "><!--Id:bean Unique identifier class: The fully qualified name of the class: cannot be an abstract class, cannot be an interface. Object obj=class.forname ("Cn.wwh.www.spring.aaa.helloworld.HelloWorld"). newinstance ();--><bean id= " HelloWorld "class=" Cn.wwh.www.spring.aaa.helloworld.HelloWorld "></bean></beans>
Referencing in the Applicationcontex.xml file
Helloworld.xml file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "><!--introduce additional profiles: default from the current path to find the file. You can bring the path prefix: Classpath: Means to find a file from the Classpath path (recommended) file: Indicates that you want to find the files from disk <import resource= "file:d:\users\administrator\workspaces\ MyEclipse 8.5\springexercise1\src\cn\wwh\www\spring\aaa\helloworld\helloworld.xml "/> Note: Only the resource interface can recognize and parse this prefix:/springexercise1/src/cn/wwh/www/spring/aaa/helloworld/helloworld.xml-<import Resource= "Classpath:cn/wwh/www/spring/aaa/helloworld/helloworld.xml"/> </beans>
(4). Test Case
Package Cn.wwh.www.spring.aaa.helloworld;import Org.junit.test;import Org.springframework.beans.factory.beanfactory;import org.springframework.beans.factory.xml.XmlBeanFactory; Import Org.springframework.core.io.classpathresource;import org.springframework.core.io.resource;/** * The role of class: * * * @author Skiff * @version 1.0 *@ created: 2014-9-21 pm 03:25:12 */public class Testhelloworld {//1. Method calls in a traditional way @testpubl IC void Testold () {//Create an object HelloWorld hello = new HelloWorld (); Hello.show ();} Call @testpublic void Testioc () {//1) using the Spring Framework's control inversion. Load Applicationcontext.xml file: Find classpath path path from resource resource = New Classpathresource ("Applicationcontext.xml");//2. Create and get the spring container beanfactory bean = new Xmlbeanfactory (Resource);// 3. Get the specified bean, (especially note this, the parameter in Getbean is the ID in the bean tag in the hellowrld.xml file) HelloWorld hello = (HelloWorld) Bean.getbean (" HelloWorld ");//4. Operation Beanhello.show ();}}
Effect Test Chart:
3. Note: 1. I put the resources folder in the project directory, and the same directory as SRC, the Applicationcontext.xml file is placed in the folder
2. Introduce additional configuration files:
The default is to find the file from the current path.
You can bring the path prefix:
Classpath: Means to find the file from the Classpath path (recommended)
File: means to find files from disk
<import resource= "File:e:\users\administrator\workspaces\myeclipse 8.5
\springexercise1\src\cn\wwh\www\spring\aaa\helloworld\helloworld.xml "/>
It is important to note that this prefix can only be identified and parsed by the resource interface:/springexercise1/src/cn/wwh/www/spring/aaa/helloworld/helloworld.xml
3. Under the <bean> tab in the Helloworld.xml file:
The bean Element ID and name:
It is best to use the ID: special case before using name.
The id attribute of the bean in spring development, following the XML syntax ID constraint:
The name of the ID to satisfy the XML pair ID attribute naming specification must start with a letter, you can use letters, numbers, hyphens, underscores, words, colons
Using the Name property, you can use many special characters, early integration in Struts1 and spring (now SPRINGMVC)
<bean name= "/login" class= ".... Loginaction "/> Name contains/, the use of ID will be error
4.HelloWorld program from the Spring angle: Why configure, Spring will go to create/Manage Bean object.
(1). Load the configuration file with the resource object;
(2). Parse the configuration file and get the bean;
(3). Parse Bean,id as the bean's name, Class is used to reflect the bean instance (Class.forName (ClassName));
Note that in this configuration, all beans are guaranteed to have a parameterless constructor
(4). Returns an object instance from the container when the Getbean is called
The conclusion is that the code is transferred from the Java file to the XML.
Three methods of the 5.getBean method:
(1). Get the Bean by type:
World=factory.getbean (Helloworld.class);
Requires only one instance of this type to be configured in spring;
If the class of this type is configured with multiple <bean/>, an error will be made:--one type can find multiple objects
(2). Fetch the Bean according to the Bean's name:
World= (HelloWorld) Factory.getbean ("Hello2");
It's not safe to take beans by name;
Abnormal:
Org.springframework.beans.factory.NoSuchBeanDefinitionException:No Bean named ' HelloWorld ' is defined:
The ID of a <bean/> element is not found in the container or name is HelloWorld.
(3). By name and type: (recommended)
World=factory.getbean ("Hello", helloworld.class);
Abnormal:
Org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type [Cn.itcast.cd.day1._1hello. HelloWorld] is defined:
Not a unique bean type error:
Spring's first program