JavaWeb Spring開辟入門深刻進修。本站提示廣大學習愛好者:(JavaWeb Spring開辟入門深刻進修)文章只能為提供參考,不一定能成為您想要的結果。以下是JavaWeb Spring開辟入門深刻進修正文
1 Spring根本特點
Spring是一個異常活潑的開源框架;它是一個基於Core來構架多層JavaEE體系的框架,它的重要目地是簡化企業開辟.
Spring以一種非侵入式的方法來治理你的代碼,Spring倡導”起碼侵入”,這也就意味著你可以恰當的時刻裝置或卸載Spring,Spring讓java亮了。 (開放–閉合道理),這裡是閉准繩。
2 開辟spring所須要的對象
(這裡先將spring2.5 ,前面3.0)
2.1 Spring的jar包
到http://www.springsource.org/download下載spring,然落後行解緊縮,在解壓目次中找到上面jar文件,拷貝到類途徑下
—spring的焦點類庫 在spring文檔的dist下 dist\spring.jar
—引入的第三方類庫 都spring文檔的lib下,lib\jakarta-commons\commons-logging.jar
—假如應用了切面編程(AOP),還須要以下jar文件 lib/aspectj/aspectjweaver.jar和aspectjrt.jarlib/cglib/cglib-nodep-2.1_3.jar
—假如應用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,還須要以下jar文件lib\j2ee\common-annotations.jar
注:JSR(Java 標准要求)是指向JCP(Java Community Process)提出新增一個尺度化技巧標准的正式要求。任何人都可以提交JSR(Java 標准要求),以向Java平台增加新的API和辦事。JSR已成為Java界的一個主要尺度
2.2 Spring設置裝備擺設文件
默許情形下是applicationContext.xml文件。可以樹立許多xml文件,工程中普通都是如許設置裝備擺設的。(src目次下建)
3 Spring根本功效詳解
3.1 SpringIOC
Spring的掌握反轉:把對象的創立、初始化、燒毀等任務交給spring容器來做。由spring容器掌握對象的性命周期。
步調:
•A. 啟動spring容器
1、 在類途徑下尋覓設置裝備擺設文件來實例化容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
可以在全部類途徑中尋覓xml文件
* 經由過程這類方法加載。須要將spring的設置裝備擺設文件放到以後項目標classpath途徑下
* classpath途徑指的是以後項目標src目次,該目次是java源文件的寄存地位。
2、 在文件體系途徑下尋覓設置裝備擺設文件來實例化容器
Spring的設置裝備擺設文件可以指定多個,可以經由過程String數組傳入。
注:常常用第一種辦法啟動容器
•B. 從spring容器中提取對象
spring 容器構造:
3.2 別號
<beans> <alias name="person" alias="p"/>//alias這裡是別號,可以經由過程p,獲得person這個bean. <bean name="person" class="cn.itcast.aliasspring.Person"/> </beans>
經由過程如許的設置裝備擺設,可以到達在一個處所定名,在多個處所應用分歧的名字的後果。
3.3 Spring容器外部對象
1 創立對象的方法
1.1 無參結構函數
<bean id="helloWorld" class="com.itheima10.spring.createobject.HelloWorld"></bean>
1.2 靜態工場辦法
<bean id="helloWorld2" class="com.itheima10.spring.createobject.method.HelloWorldFactory"
factory-method="getInstance"></bean>
1.3 實例工場辦法
<bean id="helloWorldFactory" class="com.itheima10.spring.createobject.method.HelloWorldFactory2"></bean>
<bean id="helloWorld3" factory-bean="helloWorldFactory" factory-method="getInstance"></bean>
記住概念便可,用的最多的是第一種辦法,然則和其余對象集成時,用的是實例工場形式。
實例:
設置裝備擺設applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 把一個類放入到spring容器中,該類就稱為一個bean --> <!-- bean描寫了一個類 id 獨一的標示 class 類的全名 --> <bean id="helloWorld" class="com.itheima10.spring.createobject.HelloWorld"></bean> <!-----------------------別號--------------------------------------> <!-- name的屬性和bean的id婚配 alias 別號 --> <alias name="helloWorld" alias="狗蛋"/> <alias name="helloWorld" alias="習近平"/> <!-----------------------靜態工場辦法--------------------------------------> <!-- 把helloWorldFactory放入到spring容器中 factory-method 工場辦法 --> <bean id="helloWorld2" factory-method="getInstance" class="com.itheima10.spring.createobject.method.HelloWorldFactory"></bean> <!-----------------------實例工場辦法--------------------------------------> <!-- 把helloWorldFactory2放入到spring容器中 factory-bean 指清楚明了工場bean factory-method 指清楚明了該工場bean中的辦法 --> <bean id="helloWorldFactory" class="com.itheima10.spring.createobject.method.HelloWorldFactory2"></bean> <bean id="helloWorld3" factory-bean="helloWorldFactory" factory-method="getInstance"></bean> </beans>
樹立實體類HelloWorld
package com.itheima10.spring.createobject;
public class HelloWorld {
public void hello(){
System.out.println("hello");
}
}
樹立靜態工場HelloWorldFactory
package com.itheima10.spring.createobject.method;
public class HelloWorldFactory {
public static HelloWorld getInstance(){
System.out.println("static method");
return new HelloWorld();
}
}
樹立實體工場HelloWorldFactory2
package com.itheima10.spring.createobject.method;
public class HelloWorldFactory2 {
/**
* 必需先創立工場對象,能力挪用該辦法
* @return
*/
public HelloWorld getInstance(){
return new HelloWorld();
}
}
編寫測試辦法CreateObjectMethodTest
package com.itheima10.spring.createobject.method;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 第二種和第三種發生方法能看明確就OK了
* @author zd
*
*/
public class CreateObjectMethodTest {
/**
* 在默許情形下,spring容器挪用的是一個類的默許的結構函數創立對象
*/
@Test
public void testCreateObject_Default(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
helloWorld.hello();
}
/**
* 應用靜態工場創立對象
* <bean id="helloWorld2"
factory-method="getInstance"
class="com.itheima10.spring.createobject.method.HelloWorldFactory"></bean>
spring容器做的工作:
應用HelloWorldFactory類挪用了getInstance辦法
*/
@Test
public void testCreateObject_StaticFactory(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld2");
helloWorld.hello();
}
/**
* 實例工場辦法創立對象
* <bean id="helloWorldFactory"
* class="com.itheima10.spring.createobject.method.HelloWorldFactory2"></bean>
* <bean id="helloWorld3"
factory-bean="helloWorldFactory"
factory-method="getInstance"></bean>
spring容器外部做的工作:
1、創立一個helloWorldFactory對象
2、由該對象挪用getInstance發生helloWorld對象
*/
@Test
public void testCreateObject_InstanceFactory(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld3");
helloWorld.hello();
}
}
2 對象的scope
對象的scope有兩個屬性:singleton 與prototype。singleton表現單例
2.1 singleton(默許值)
在每一個Spring IoC容器中一個bean界說只要一個對象實例(同享)。
2.2 prototype
許可bean可以被屢次實例化(應用一次就創立一個實例) . Spring不克不及對一個prototype bean的全部性命周期擔任.這就意味著清晰prototype感化域的對象並釋聽任何prototype bean所持有的昂貴資本都是客戶真個義務。
<bean id="helloWorld" class="com.itheima10.spring.scope.HelloWorld" scope="singleton"></bean>
<bean id="helloWorld" class="com.itheima10.spring.scope.HelloWorld" scope="prototype"></bean>
樹立HelloWorld類
public class HelloWorld {
public List<String> lists = new ArrayList<String>();
public HelloWorld(){
System.out.println("new instance");
}
public void hello(){
System.out.println("hello");
}
}
樹立測試類ScopeTest
package com.itheima10.spring.scope;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopeTest {
/**
* 把一個bean放入到spring容器中,默許的是單例
* 假如一個類放入到spring輕易中,而這個類是單例的,那末該類中的屬性將會成為同享的
*/
@Test
public void testCreateObject_Scope_Default(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello1 = (HelloWorld)context.getBean("helloWorld");
hello1.lists.add("aaaa");
hello2= (HelloWorld)context.getBean("helloWorld");
hello2.lists.add("bbbb");
System.out.println(helloWorld.lists.size());//2,而且只輸入一次new instance
}
/**
* 假如spring的設置裝備擺設文件以下:
* <bean id="helloWorld"
class="com.itheima10.spring.scope.HelloWorld"
scope="prototype"></bean>
那末spring容器會為創立多個對象
*/
@Test
public void testCreateObject_Scope_Prototype(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello1 = (HelloWorld)context.getBean("helloWorld");
hello1.lists.add("aaaa");
hello2= (HelloWorld)context.getBean("helloWorld");
hello2.lists.add("bbbb");
System.out.println(helloWorld.lists.size());//1,而且只輸入兩次new instance
}
}
3 初始化bean機會
懶加載——默許情形下會在容器啟動時初始化bean,但我們可以指定Bean節點的lazy-init=“true”來延遲初始化bean,這時候候,只要第一次獲得bean會才初始化bean。如:
<bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>
假如想對一切bean都運用延遲初始化,可以在根節點beans設置default-lazy-init=“true“,以下:
<beans default-lazy-init="true“ ...>
Spring默許在啟動時將一切singleton bean提早停止實例化。提早實例化意味著作為初始化的一部門,ApplicationContext會主動創立並設置裝備擺設一切的singleton bean.平日情形下這是件功德。由於如許在設置裝備擺設中有任何毛病能立刻發明。
Lazy-init 為false,spring容器將在啟動的時刻報錯(比擬好的一種方法)
Lazy-init 為true,spring容器將在挪用該類的時刻失足。
設置裝備擺設applicationContext.xml
<bean id="helloWorld" class="com.itheima10.spring.createobject.when.HelloWorld" scope="prototype"></bean> </beans>
樹立測試類CreateObjectWhenTest
package com.itheima10.spring.createobject.when;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CreateObjectWhenTest {
/**
* 默許情形下的次序
* 1、啟動spring容器
* 2、創立HelloWorld對象
* 3、對象挪用辦法
*
* <bean id="helloWorld" class="com.itheima10.spring.createobject.when.HelloWorld"></bean>
<bean id="helloWorld2" class="com.itheima10.spring.createobject.when.HelloWorld"></bean>
由於在spring容器中聲清楚明了兩個bean,所以spring容器要創立兩個對象
解釋:
假如struts2,hibernate,spring容器整合,假如spring的設置裝備擺設文件中湧現毛病
當 tomcat容器啟動的時刻,就會報錯,毛病會特殊早的顯示出來
假如一個bean寄存了年夜量的數據,這類方法欠好,有能夠會把數據過早的逗留在內存中
假如一個bean不是單例,那末不論怎樣樣設置裝備擺設,都在是context.getBean時才要創立對象
*/
@Test
public void testCreateObject_When_Default(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
context.getBean("helloWorld");
}
/**
* <bean id="helloWorld"
class="com.itheima10.spring.createobject.when.HelloWorld"
lazy-init="true"></bean>
* 次序
* 1、啟動spring容器
* 2、context.getBean
* 3、挪用結構器函數創立對象
* 解釋:
假如struts2,hibernate,spring容器整合,假如spring的設置裝備擺設文件中湧現毛病
只要當用到該bean的時刻才會報錯。
假如一個bean寄存了年夜量的數據,須要的時刻才要加載數據
*/
@Test
public void testCreateObject_When_Lazy(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
context.getBean("helloWorld");
}
}
4 init、destroy辦法
Spring初始化bean或燒毀bean時,有時須要作一些處置任務,是以spring可以在創立和裝配bean的時刻挪用bean的兩個性命周期辦法。可以指定辦法停止操作。
<bean id=“foo” class=“...Foo” init-method=“setup” destory-method=“teardown”/>
當foo被載入到Spring容器中時挪用init-method辦法。當foo自在器中刪除時挪用destory-method(scope = singleton有用)
編寫HelloWorld
public class HelloWorld {
public HelloWorld(){
System.out.println("new instance");
}
public void init(){
System.out.println("init");
}
public void destroy(){
System.out.println("destroy");
}
public void hello(){
System.out.println("hello");
}
}
編寫測試類InitDestroyTest
package com.itheima10.spring.ioc.initdestroy;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InitDestroyTest {
/**
* init-method="init"
* 履行次序:
* 1、啟動spring容器
* 2、創立helloWorld對象
* 3、履行init辦法
* spring容器外部主動履行的
* 4、對象挪用辦法
* 5、只要當spring容器封閉失落的情形下能力履行destroy辦法 條件前提:bean是單例的
* 該辦法也是由spring容器外部挪用的
* 解釋:
* 假如一個bean不是單例的,則spring容器不擔任對象的燒毀。
* 在spring容器中,只要一個bean是單例的情形下,spring容器才要擔任對象的創立、初始化、燒毀任務
* 假如一個bean不是單例,spring容器只擔任創立、初始化
*/
@Test
public void testInitDestroy(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
helloWorld.hello();
ClassPathXmlApplicationContext applicationContext = (ClassPathXmlApplicationContext)context;
applicationContext.close();
}
}
履行次序圖:
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。