Spring框架初識(一)
阿新 • • 發佈:2017-09-04
持久層 容器 med int reat set map 要求 enter 新的 1. Spring框架概述
1.1 簡介
Spring是分層的Java SE/EE應用 full-stack輕量級開源框架,以IoC(Inverse Of Control:反轉控制)和AOP(Aspect Oriented Programming:面向切面編程)為內核,提供了展現層Spring MVC和持久層Spring JDBC以及業務層事務管理等眾多的企業級應用技術,還能整合開源世界眾多著名的第三方框架和類庫,逐漸成為使用最多的Java EE企業應用開源框架。
1.2 優點
2.2 創建配置文件(默認在src下創建applicationContext.xml配置文件)
3.5 IOC中bean標簽和管理對象細節
3.6 實例化Bean的三種方式(了解)
3.6.1 使用默認無參構造函數
- 方便解耦,簡化開發
- AOP編程的支持
- 聲明式事務的支持
- 方便程序的測試
- 方便集成各種優秀框架
- 降低JavaEE API的使用難度
Java源碼是經典學習範例
Spring的源代碼設計精妙、結構清晰、匠心獨用,處處體現著大師對Java設計模式靈活運用以及對Java技術的高深造詣。它的源代碼無意是Java技術的最佳實踐的範例。
1.3 體系結構 2. Spring框架使用 2.1 導包(基本包6個)
Spring基本包 | spring-beans-4.2.4.RELEASE.jar |
spring-context-4.2.4.RELEASE.jar |
|
spring-core-4.2.4.RELEASE.jar | |
spring-expression-4.2.4.RELEASE.jar | |
log4j | com.springsource.org.apache.commons.logging-1.1.1.jar |
com.springsource.org.apache.log4j-1.2.15.jar |
<?xml version="1.0" encoding="UTF-8"?> <!-- 導入schema 約束的位置在: ..\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html 文件中。 註意:要導入schema約束 --> <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"> </beans>
2.3 模擬配置 2.3.1 Demo1
package com.zycom.demo; import org.junit.Test; import org.springframework.context.ApplicationContext; import com.zycom.service.UserService; import com.zycom.utils.SpringUtils; public class Demo1 { @Test public void t1(){ ApplicationContext ac = SpringUtils.getApplicationContext(); UserService us = (UserService) ac.getBean("userService"); us.login(); System.out.println("login成功..."); } }
2.3.2 Service層
package com.zycom.serviceimpl; import org.springframework.context.ApplicationContext; import com.zycom.dao.UserDao; import com.zycom.service.UserService; import com.zycom.utils.SpringUtils; public class UserServiceImpl implements UserService { @Override public boolean login() { System.out.println("UserSerivice:登錄..."); ApplicationContext ac = SpringUtils.getApplicationContext(); UserDao ud = (UserDao) ac.getBean("userDao"); return ud.login(); } }
2.3.3 DAO層
package com.zycom.daoimpl; import com.zycom.dao.UserDao; public class UserDaoImpl implements UserDao { @Override public boolean login() { System.out.println("UserDao:用戶登錄..."); return true; } }2.3.3 util工具類
package com.zycom.utils; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringUtils { public static ApplicationContext ac =null; static{ ac = new ClassPathXmlApplicationContext("applicationContext.xml"); } public static ApplicationContext getApplicationContext(){ return ac; } }
2.3.4 (src下)applicationContext配置文件
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean definitions here --> <bean id="userService" class="com.zycom.serviceimpl.UserServiceImpl"></bean> <bean id="userDao" class="com.zycom.daoimpl.UserDaoImpl"></bean> </beans>
3.Spring基於XML的IOC細節 3.1 spring中工廠的類結構圖 3.2 BeanFactory(舊)和ApplicationContext(新)的區別
BeanFactory是Spring容器中的頂層接口。
ApplicationContext是它的子接口。
BeanFactory和ApplicationContext的區別:
創建對象的時間點不一樣。
ApplicationContext:只要一讀取配置文件,默認情況下就會創建對象。
BeanFactory:什麽使用什麽時候創建對象。
3.3 ApplicationContext接口的實現類
ClassPathXmlApplicationContext:
它是從類的根路徑下加載配置文件 推薦使用這種
FileSystemXmlApplicationContext:
它是從磁盤路徑上加載配置文件,配置文件可以在磁盤的任意位置。
3.4 IOC中bean標簽和管理對象細節 3.4.1 bean標簽 (1)作用
- 用於配置對象讓spring來創建的。
- 默認情況下它調用的是類中的無參構造函數。如果沒有無參構造函數則不能創建成功。
屬性名稱 | 值 | 作用 | |
id | 給對象在容器中提供一個唯一標識 | 用於獲取對象 | |
class | 指定類的全限定類名 | 用於反射創建對象。默認情況下調用無參構造函數。 | |
scope | singleton | 默認值,單例的 | |
prototype | 多例的 | ||
request | WEB項目中,Spring創建一個Bean的對象,將對象存入到request域中 | ||
session | WEB項目中,Spring創建一個Bean的對象,將對象存入到session域中 | ||
globalSession | WEB項目中,應用在Portlet環境.如果沒有Portlet環境那麽globalSession相當於session | ||
init-method | 指定類中的初始化方法名稱 | ||
destroy-method | 指定類中銷毀方法名稱 |
對象類別 | 屬性值 | 範圍 | 生命周期 |
單例對象 | scope="singleton" | 一個應用只有一個對象的實例。它的作用範圍就是整個引用。 | 對象出生:當應用加載,創建容器時,對象就被創建了。 對象活著:只要容器在,對象一直活著。 對象死亡:當應用卸載,銷毀容器時,對象就被銷毀了。 |
多例對象 | scope="prototype" | 每次訪問對象時,都會重新創建對象實例 | 對象出生:當使用對象時,創建新的對象實例。 對象活著:只要對象在使用中,就一直活著。 對象死亡:當對象長時間不用時,被java的垃圾回收器回收了。 |
<!--在默認情況下: 它會根據默認無參構造函數來創建類對象。如果bean中沒有默認無參構造函數,將會創建失敗。 --> <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl" />
3.6.2 spring管理靜態工廠-使用靜態工廠的方法創建對象
/** * 模擬一個靜態工廠,創建業務層實現類 */ public class StaticFactory { public static ICustomerService createCustomerService(){ return new CustomerServiceImpl(); } }
<!-- 此種方式是: 使用StaticFactory類中的靜態方法createCustomerService創建對象,並存入spring容器 id屬性:指定bean的id,用於從容器中獲取 class屬性:指定靜態工廠的全限定類名 factory-method屬性:指定生產對象的靜態方法 --> <bean id="customerService" class="com.itheima.factory.StaticFactory" factory-method="createCustomerService"></bean>
3.6.2 spring管理實例工廠-使用實例工廠的方法創建對象
/** * 模擬一個實例工廠,創建業務層實現類 * 此工廠創建對象,必須現有工廠實例對象,再調用方法 */ public class InstanceFactory { public ICustomerService createCustomerService(){ return new CustomerServiceImpl(); } }
<!-- 此種方式是: 先把工廠的創建交給spring來管理。 然後在使用工廠的bean來調用裏面的方法 factory-bean屬性:用於指定實例工廠bean的id。 factory-method屬性:用於指定實例工廠中創建對象的方法。 --> <beanid="instancFactory" class="com.itheima.factory.InstanceFactory"></bean> <beanid="customerService" factory-bean="instancFactory" factory-method="createCustomerService"></bean>
3.7 spring的依賴註入(重要)
3.7.1 依賴註入的概念 它是spring框架核心ioc的具體實現方式。簡單的說,就是坐等框架把對象傳入,而不用我們自己去獲取。 3.7.2 構造函數註入 該方法需要bean類提供有參構造方法/** */ public class CustomerServiceImpl implements ICustomerService { private String name; private Integer age; private Date birthday; public CustomerServiceImpl(String name, Integer age, Date birthday) { this.name = name; this.age = age; this.birthday = birthday; } @Override public void saveCustomer() { System.out.println(name+","+age+","+birthday); } }
<!-- 使用構造函數的方式,給service中的屬性傳值 要求: 類中需要提供一個對應參數列表的構造函數。 涉及的標簽: constructor-arg 屬性: index:指定參數在構造函數參數列表的索引位置 type:指定參數在構造函數中的數據類型 name:指定參數在構造函數中的名稱 用這個找給誰賦值 =======上面三個都是找給誰賦值,下面兩個指的是賦什麽值的============== value:它能賦的值是基本數據類型和String類型 ref:它能賦的值是其他bean類型,也就是說,必須得是在配置文件中配置過的bean --> <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl"> <constructor-arg name="name" value="張三"></constructor-arg> <constructor-arg name="age" value="18"></constructor-arg> <constructor-arg name="birthday" ref="now"></constructor-arg> </bean> <bean id="now" class="java.util.Date"></bean>
3.7.3 set方法註入
就是在類中提供需要註入成員的set方法 /** */ public class CustomerServiceImpl implements ICustomerService { private String name; private Integer age; private Date birthday; public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public void saveCustomer() { System.out.println(name+","+age+","+birthday); } }
<!-- 通過配置文件給bean中的屬性傳值:使用set方法的方式 涉及的標簽: property 屬性: name:找的是類中set方法後面的部分 ref:給屬性賦值是其他bean類型的 value:給屬性賦值是基本數據類型和string類型的 實際開發中,此種方式用的較多。 --> <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl"> <property name="name" value="test"></property> <property name="age" value="21"></property> <property name="birthday" ref="now"></property> </bean> <bean id="now" class="java.util.Date"></bean>
3.7.4 使用p名稱空間註入數據(本質還是調用set方法) 此種方式是通過在xml中導入p名稱空間約束,使用p:propertyName來註入數據,它的本質仍然是調用類中的set方法實現註入功能。 java類代碼:
/** * 使用p名稱空間註入,本質還是調用類中的set方法 */ public class CustomerServiceImpl4 implements ICustomerService { private String name; private Integer age; private Date birthday; public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public void saveCustomer() { System.out.println(name+","+age+","+birthday); } }
xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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"> <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl4" p:name="test" p:age="21" p:birthday-ref="now"/> </beans>
3.7.5 註入集合屬性 JAVA代碼:
/** */ public class CustomerServiceImpl implements ICustomerService { private String[] myStrs; private List<String> myList; private Set<String> mySet; private Map<String,String> myMap; private Properties myProps; public void setMyStrs(String[] myStrs) { this.myStrs = myStrs; } public void setMyList(List<String> myList) { this.myList = myList; } public void setMySet(Set<String> mySet) { this.mySet = mySet; } public void setMyMap(Map<String, String> myMap) { this.myMap = myMap; } public void setMyProps(Properties myProps) { this.myProps = myProps; } @Override public void saveCustomer() { System.out.println(Arrays.toString(myStrs)); System.out.println(myList); System.out.println(mySet); System.out.println(myMap); System.out.println(myProps); } }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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"> <!-- 註入集合數據 List結構的: array,list,set Map結構的 map,entry,props,prop --> <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl"> <!-- 在註入集合數據時,只要結構相同,標簽可以互換 --> <!-- 給數組註入數據 --> <property name="myStrs"> <set> <value>AAA</value> <value>BBB</value> <value>CCC</value> </set> </property> <!-- 註入list集合數據 --> <property name="myList"> <array> <value>AAA</value> <value>BBB</value> <value>CCC</value> </array> </property> <!-- 註入set集合數據 --> <property name="mySet"> <list> <value>AAA</value> <value>BBB</value> <value>CCC</value> </list> </property> <!-- 註入Map數據 --> <property name="myMap"> <props> <prop key="testA">aaa</prop> <prop key="testB">bbb</prop> </props> </property> <!-- 註入properties數據 --> <property name="myProps"> <map> <entry key="testA" value="aaa"></entry> <entry key="testB"> <value>bbb</value> </entry> </map> </property> </bean> </beans>
Spring框架初識(一)