Spring Development steps:
The Spring jar package describes:
Under 3.0, the source code has all the packages related to spring (including spring core packages and dependency packages)
In more than 3.0 versions, the source only spring core package does not depend on the package dependency package need to download
1) source code, JAR package: Spring-framwork-3.2.5.release
Commons-logging-1.1.3.jar Log
Spring-beans-3.2.5.release.jar Bean Node
Spring-context-3.2.5.release.jar Spring Context Node
Spring-core-3.2.5.release.jar Spring Core Features
Spring-expression-3.2.5.release.jar Spring expression-related tables
These are the 5 jar packages that must be introduced and can be managed by the user in the project!
2) Core configuration file: Applicationcontext.xml
Spring configuration file: Applicationcontext.xml/bean.xml
Constraint reference:
Spring-framework-3.2.5.release\docs\spring-framework-reference\htmlsingle\index.html
< 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" Xmlns:context="Http://www.springframework.org/schema/context" xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " > </ Beans > |
Introductory Case:
User class
<span style= "Font-family:courier New;" >package Cn.itcast.hello;public class User {public user () {System.out.println ("User object created! ");} private int id;private String name;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}} </span><strong></strong>
Bean.xml file
<<span style= "Font-family:courier New;" >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.xsd "> <!-- By default, Spring creates an object as a singleton singleton prototype: Multiple cases--- <bean id= "user" class= " Cn.itcast.hello.User "></bean><bean id=" User "lazy-init=" true " class=" Cn.itcast.hello.User "scope = "Singleton" init-method= "Init_user" destroy-method= "Destory_user" ></bean></beans></span>
Test class App
Here are two ways to create an IOC container, and the second one is recommended.
<span style= "Font-family:courier New;" >package Cn.itcast.hello;import Org.junit.test;import Org.springframework.beans.factory.beanfactory;import Org.springframework.beans.factory.xml.xmlbeanfactory;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.core.io.classpathresource;import Org.springframework.core.io.resource;public class App {@ testpublic void Test () {//1. Parse bean.xml file, Resource resource=new classpathresource ("Bean.xml");//2. Create Object container, Bean factory IOC container = Factory class + configuration file beanfactory factory=new xmlbeanfactory (Resource); User user= (user) Factory.getbean ("User"); System.out.println (User.getname ());} @Testpublic void Test2 () {//This allows direct access to the IOC container ApplicationContext ac=new classpathxmlapplicationcontext ("Bean.xml"); User user= (user) Ac.getbean ("User"); System.out.println (User.getid ());}} </span>
Operation Result:The user object was created not by new but by using the spring container!
Detail Questions:
We change the configuration user object in the Bean.xml configuration file to:
<bean id= "User" lazy-init= "true" class= "Cn.itcast.hello.User" scope= "singleton" init-method= "Init_user" Destroy-method= "Destory_user" ></bean>
Add a non-tragic constructor and an initialization method to the user class as well as an object destruction method
<span style= "Font-family:courier New;" >public User () {System.out.println ("User object created! ");} public void Init_user () {System.out.println ("User object initialized! ");} public void Destory_user () {System.out.println ("User object destroyed!") ");} </span>
Test class App2
<span style= "Font-family:courier New;" >package Cn.itcast.hello;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class App2 {@Testpublic void Test () { Classpathxmlapplicationcontext ac=new classpathxmlapplicationcontext ("Bean.xml"); SYSTEM.OUT.PRINTLN ("IOC container created!") "); User user1= (user) Ac.getbean ("User"); User user2= (user) Ac.getbean ("User"); System.out.println (user1); System.out.println (user2);///Object destruction method to display the method that is not destroyed in the calling interface needs to call the Destroy () method using the Implementation class Ac.destroy ();}} </span>
test scope= "Singleton" in a single case bean.xml:
Test scope= "prototype" in multiple cases bean.xml
Summarize:
* 1) Object creation: Single Case / Multiple Cases
* scope= "singleton", default value, that The default is singleton "service/DAO/ tool class"
* scope= "prototype", multiple cases; "Action Object"
*
* 2) when to create ?
* scope= "prototype" The object is created when the object is used.
* scope= "singleton" in the Start ( before container initialization ) , you have created a Bean , and the entire application has only one.
* 3) whether to defer creation
* Lazy - Init = "false" default is false, Create objects without delay, that is, at startup time
* Lazy - Init = "true" deferred initialization, objects are created when objects are used
* (only valid for single cases)
* 4) after the object is created, initialize the / destroyed
* Init - method= "Init_user" the corresponding object's Init_user method that executes after the object creates love "
* Destroy-method= "Destroy_user""In the Call container object's
Destriymethod when executed,(Containerwith the implementation class)"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring Starter Case