2.Spring的優勢:html
2.1方便解耦,簡化開發 (高內聚低耦合)java
2.2Spring就是一個大工廠(容器),能夠將全部對象建立和依賴關係維護,交給Spring管理spring
spring工廠是用於生成bean編程
2.3 AOP編程的支持服務器
Spring提供面向切面編程,能夠方便的實現對程序進行權限攔截、運行監控等功能架構
2.4 聲明式事務的支持app
只須要經過配置就能夠完成對事務的管理,而無需手動編程框架
2.5方便程序的測試ide
Spring對Junit4支持,能夠經過註解方便的測試Spring程序函數
2.6方便集成各類優秀框架
Spring不排斥各類優秀的開源框架,其內部提供了對各類優秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
2.7下降JavaEE API的使用難度
3 入門案例
3.1導入jar包
3.2在src目錄下創建目標和xml文件
3.3 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.xsd"> <!-- 配置service <bean> 配置須要建立的對象 id :用於以後從spring容器得到實例時使用的 setBookdao >Bookdao >bookdao class :須要建立實例的全限定類名 --> <bean id="bookServiceId" class="com.heima.spring_day01.bookServiceImpl" scope="prototype"> <property name="bookdao" ref="bookDaoId"></property> </bean> <bean id="bookDaoId" class="com.heima.spring_day01.bookDaoImpl"> </bean> </beans>
創建兩個接口和兩個實現類,在bookService裏調用bookDao類中的add方法,因此xml裏須要兩個Bean,一個sericebean 一個daobean,之前用的時候都是經過new一個對象來實現,如今利用spring的話,把new的操做交給spring來執行,只須要調用getbean方法就能夠獲取對應的bean對象。
<bean id="bookServiceId" class="com.heima.spring_day01.bookServiceImpl" scope="prototype"> 這個id至關於一個引用鍵值,class是用來產生一個對象的。
<property>是class對應的屬性。
ref--引用的bean.
測試類代碼:
@Test public void fun() { String xmlPath = "/com/heima/spring_day01/bean.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); bookService bookService = (com.heima.spring_day01.bookService) applicationContext.getBean("bookServiceId"); bookService bookService1 = (com.heima.spring_day01.bookService) applicationContext.getBean("bookServiceId"); System.out.println(bookService==bookService1); bookService.save(); }
String xmlPath = "/com/heima/spring_day01/bean.xml"; 這個是bean.xml的路徑
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);經過讀取配置文件來創建一個beanFactory。
bookService bookService = (com.heima.spring_day01.bookService) applicationContext.getBean("bookServiceId"); 經過xml文件的id鍵值來得到對應的beanFactory中的對象。
測試:
5.spring的屬性注入
1.構造函數
經過bean.xml文件就能給對應類的構造函數進行賦值操做
使用 <constructor-arg>來進行配置
2.setter方法
<property name = "" value=""/>屬性來進行賦值
3.集合注入
使用 <property name="dStrings"> //dStrings是數據類型 能夠是 set map class list 等
<array> 對應數據類型:<map><list>等
</array>
</property>
4.集合注入的xml配置:
<map>要使用<entry key="rose" value="肉絲"/>來設置key---value
<?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"> <bean id="collectDataId" class="com.sping.bean_set.Collect"> <property name="dStrings"> <array> <value>ha1</value> <value>ha2</value> <value>ha3</value> <value>ha4</value> </array> </property> <property name="setStrings"> <set> <value>小夥子1</value> <value>小夥子2</value> <value>小夥子3</value> <value>小夥子4</value> </set> </property> <property name="listStrings"> <list> <value>hhhh1</value> <value>hhhh2</value> <value>hhhh3</value> <value>hhhh4</value> <value>hhhh5</value> <value>hhhh6</value> </list> </property> <property name="mapStrings"> <map> <entry key="rose" value="肉絲"/> <entry key="jack" value="肉絲"/> </map> </property> </bean> </beans>
package com.sping.bean_set; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.Map; import java.util.Properties; public class Collect { private String[] dStrings; private Set<String> setStrings; private List<String> listStrings; private Map<String, String> mapStrings; private Properties propsData; public String[] getdStrings() { return dStrings; } public void setdStrings(String[] dStrings) { this.dStrings = dStrings; } public Set<String> getSetStrings() { return setStrings; } public void setSetStrings(Set<String> setStrings) { this.setStrings = setStrings; } public List<String> getListStrings() { return listStrings; } public void setListStrings(List<String> listStrings) { this.listStrings = listStrings; } public Map<String, String> getMapStrings() { return mapStrings; } public void setMapStrings(Map<String, String> mapStrings) { this.mapStrings = mapStrings; } public Properties getPropsData() { return propsData; } public void setPropsData(Properties propsData) { this.propsData = propsData; } @Override public String toString() { return "Collect [\ndStrings=" + Arrays.toString(dStrings) + ", \nsetStrings=" + setStrings + ", \nlistStrings=" + listStrings + ", \nmapStrings=" + mapStrings + ", \npropsData=" + propsData + "]"; } }
測試類:
public class Testfun { @Test public void fun() { String xml = "/com/sping/bean_set/bean.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xml); Collect collect = (Collect) applicationContext.getBean("collectDataId"); System.out.println(collect.toString()); } }