1、Spring框架了解java
Spring框架是一個開源的框架,爲JavaEE應用提供多方面的解決方案,用於簡化企業級應用的開發,至關因而一種容器,能夠集成其餘框架(結構圖以下)。mysql
上圖反映了框架引包的依賴關係(例如:DAO的jar包依賴AOP的jar包,AOP的jar包依賴Core的jar包)spring
上圖也反映了Spring功能有:sql
IOC:控制反轉,Spring的核心功能express
AOP:面向切面編程編程
Web:MVC結構實現、與其餘Web技術融合spring-mvc
DAO:與JDBC整合和事務管理mvc
ORM:與ORM對象映射實現的框架整合app
JEE:與JavaEE服務整合框架
2、SpringIOC(Inversion Of Control)
SpringIOC控制反轉,是指程序中對象的獲取方式發生反轉,由最初的new方式建立,轉爲由框架建立注入,這樣能夠下降對象之間的耦合度。
IOC的主要做用是管理程序的組件,建立組件對象和維護對象之間的關係。
Spring容器:在Spring中,任何的Java類都被當成Bean組件,經過容器管理和使用,Spring容器實現了IOC和AOP機制,
Spring容器有ApplicationContext和BeanFactory兩種類型。
ApplicationContext繼承自BeanFactory,提供了更多的方法,建議使用ApplicationContext。
ApplicationContext實例化途徑:
1.從classpath下加載配置文件實例化:ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
2.從文件系統中加載配置文件實例化:ApplicationContext ac = new FileSystemXmlApplicationContext("D:\\applicationContext.xml");
Bean對象的建立:
Spring容器建立Bean對象有三種方法:
1.使用構造器來實例化
2.使用靜態工廠方法來實例化
3.使用動態(實例)工廠方法來實例化
Spring容器建立Bean對象的步驟:
第一步:導入SpringIOC相關包到WebRoot/WBE-INF/lib下(想要相關包的網友請留言私聊)
commons-logging-1.2.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
第二步:編寫實體類和實體工廠類
實體類:
1 package com.springioc.entity; 2 3 public class User { 4 private Integer id; 5 private String username; 6 private String password; 7 public User() { 8 super(); 9 // TODO Auto-generated constructor stub 10 } 11 public User(Integer id, String username, String password) { 12 super(); 13 this.id = id; 14 this.username = username; 15 this.password = password; 16 } 17 public Integer getId() { 18 return id; 19 } 20 public void setId(Integer id) { 21 this.id = id; 22 } 23 public String getUsername() { 24 return username; 25 } 26 public void setUsername(String username) { 27 this.username = username; 28 } 29 public String getPassword() { 30 return password; 31 } 32 public void setPassword(String password) { 33 this.password = password; 34 } 35 @Override 36 public int hashCode() { 37 final int prime = 31; 38 int result = 1; 39 result = prime * result + ((id == null) ? 0 : id.hashCode()); 40 result = prime * result 41 + ((password == null) ? 0 : password.hashCode()); 42 result = prime * result 43 + ((username == null) ? 0 : username.hashCode()); 44 return result; 45 } 46 @Override 47 public boolean equals(Object obj) { 48 if (this == obj) 49 return true; 50 if (obj == null) 51 return false; 52 if (getClass() != obj.getClass()) 53 return false; 54 User other = (User) obj; 55 if (id == null) { 56 if (other.id != null) 57 return false; 58 } else if (!id.equals(other.id)) 59 return false; 60 if (password == null) { 61 if (other.password != null) 62 return false; 63 } else if (!password.equals(other.password)) 64 return false; 65 if (username == null) { 66 if (other.username != null) 67 return false; 68 } else if (!username.equals(other.username)) 69 return false; 70 return true; 71 } 72 @Override 73 public String toString() { 74 return "User [id=" + id + ", username=" + username + ", password=" 75 + password + "]"; 76 } 77 78 }
實體工廠類:
1 package com.springioc.entity; 2 3 public class UserFactory { 4 public static User CreateUser(){ 5 return new User(); 6 } 7 8 public User DynamicCreateUser(){ 9 return new User(); 10 } 11 }
第三步:編寫實例化配置文件applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 8 xmlns:jee="http://www.springframework.org/schema/jee" 9 xmlns:mvc="http://www.springframework.org/schema/mvc" 10 xmlns:util="http://www.springframework.org/schema/util" 11 xsi:schemaLocation=" 12 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 14 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 15 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 16 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 17 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 18 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 19 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 20 21 <!-- ************************************************************************************************************************ --> 22 <!-- 在容器配置文件applicationContext中添加bean的定義,等價於將Bean組件放入Spring容器,後續由Spring負責建立對象 --> 23 <!-- 在容器中最基本的單位就是bean標籤,一個bean標籤表明一個對象 --> 24 <!-- 默認狀況下,每個bean都是單例的,在單例(singleton)狀況下,隨容器的建立而建立,隨着容器的銷燬而銷燬 --> 25 <!-- Bean建立對象的三種方式 --> 26 <!-- 1.構造器 27 語法格式: 28 <bean id="對象標識符" class="對象權限定名"></bean> 29 舉例: 30 <bean id="user" class="com.springioc.entity.User"></bean> 31 --> 32 <!-- 2.靜態工廠 33 語法格式: 34 <bean id="靜態工廠標識符" class="工廠權限定名" factory-method="靜態方法名"></bean> 35 舉例: 36 <bean id="staticFactory" class="com.springioc.entity.UserFactory" factory-method="CreateUser"></bean> 37 --> 38 <!-- 3.動態(實例)工廠 39 語法格式: 40 <bean id="工廠標識符" class="工廠權限定名"></bean> 41 <bean id="動態工廠標識符" factory-bean="工廠標識符" factory-method="動態方法名"></bean> 42 舉例: 43 <bean id="userFactory" class="com.springioc.entity.UserFactory"></bean> 44 <bean id="dynamicCreateUser" factory-bean="userFactory" factory-method="DynamicCreateUser"></bean> 45 --> 46 <bean id="userFactory" class="com.springioc.entity.UserFactory"></bean> 47 <bean id="dynamicCreateUser" factory-bean="userFactory" factory-method="DynamicCreateUser"></bean> 48 <!-- ************************************************************************************************************* --> 49 <!-- Bean便籤經常使用屬性: 50 id:bean對象標識符 51 factory-bean:bean對象建立工廠標識符 52 class:權限定名 53 factory-method:對象建立工廠函數 54 init-method:bean對象初始化方法名 55 destory-method:bean對象銷燬方法名 56 lazy-init:延遲bean對象實例化,當設置爲true的時候,無論是否是單例(singleton),當調用getBean()的時候纔會建立對象 57 58 Beans標籤經常使用屬性:(做用於beans標籤中的全部bean標籤) 59 default-init-method:默認bean對象初始化方法名 60 default-destory-method:默認bean對象銷燬方法名 61 default-lazy-init:默認延遲bean對象實例化 62 --> 63 <!-- ************************************************************************************************************** --> 64 <!-- Bean對象的參數注入 65 注入類型能夠爲字符串、集合、Bean對象 66 1.setter注入 67 name:實體類屬性名 68 value:注入參數的值 69 ref:注入bean對象的值 70 舉例: 71 <bean id = "user" class = "com.springioc.entity.User"> 72 <property name="id" value="1"></property> 73 <property name="username" value="admin"></property> 74 <property name="password" value="123"></property> 75 </bean> 76 至關於以前 User user = new User(); 77 user.setId(1); 78 user.setUsername("admin"); 79 user.setPassword("123"); 80 81 2.構造器注入 82 index:實體類構造函數的形參順序,0表示第一個參數,1表示第二參數,2表示第三個參數... 83 value:注入參數的值 84 ref:注入bean對象的值 85 舉例: 86 <bean id = "user" class = "com.springioc.entity.User"> 87 <constructor-arg index="0" value="2"></constructor-arg> 88 <constructor-arg index="1" value="username"></constructor-arg> 89 <constructor-arg index="2" value="password"></constructor-arg> 90 </bean> 91 注意:(以上兩種注入方式都是以字符串的注入類型來舉例的,下面是Bean對象注入和集合注入) 92 Bean對象的注入:若是一個實體類中的屬性是另外一個實體類時,注入時需將property標籤的value屬性改成ref屬性 93 集合注入:(集合有set,list,map,props) 94 概述:假設一個實體類的屬性中有集合,那麼參數注入集合應該採用集合注入 95 舉例: 96 <bean id = "girl" class = "com.springioc.entity.Girl"> 97 <property name="glist"> 98 <list> 99 <value>范冰冰</value> 100 <value>楊冪</value> 101 <value>王祖賢</value> 102 </list> 103 </property> 104 <property name="gset"> 105 <set> 106 <ref bean = "user1"/> 107 <ref bean = "user2"/> 108 </set> 109 </property> 110 <property name="gmap"> 111 <map> 112 <entry key="美女" value="楊冪"></entry> 113 <entry key="美人"> 114 <value>范冰冰</value> 115 </entry> 116 </map> 117 </property> 118 <property name="gprops"> 119 <props> 120 <prop key="美女">楊冪</prop> 121 <prop key="美人">范冰冰</prop> 122 </props> 123 </property> 124 </bean> 125 --> 126 <!-- *************************************************************************************************************** --> 127 <!-- 一個Bean對象參數注入的實例:加載JDBC驅動--> 128 <!-- 加載資源文件 --> 129 <!-- 第一步:先建一個文件,文件名爲jdbc.properties,放在src/下,文件中寫 admin=root 130 password=root 131 url=jdbc:mysql://localhost:3306/test 132 JDBCdriver=com.mysql.jdbc.Driver 133 第二步:在applicationContext.xml中寫 134 <util:properties id="jdbc" location="classpath:jdbc.properties"></util:properties> 135 --> 136 <!-- 配置數據源 --> 137 <!-- Spring引入一種表達式語言,它和EL的語法類似,能夠讀取一個Bean對象/集合中的數據 --> 138 <!-- 第一步:編寫一個和jdbc.properties文件相對應的實體類JDBCTest,屬性名須要相等 139 第二步:在applicationContext.xml中寫 140 <bean id="jdbc" class="com.springioc.test.JDBCTest"> 141 <property name="admin" value="#{db.admin}"></property> 142 <property name="root" value="#{db.password}"></property> 143 <property name="url" value="#{db.url}"></property> 144 <property name="JDBCdriver" value="#{db.JDBCdriver}"></property> 145 </bean> 146 --> 147 <!-- ************************************************************************************************************** --> 148 </beans>
第四步:編寫實例化測試類
1 package com.springioc.test; 2 3 import org.springframework.context.ApplicationContext;//spring-context.jar包中的 4 import org.springframework.context.support.ClassPathXmlApplicationContext;//spring-context-support.jar包中的 5 6 import com.springioc.entity.User; 7 8 public class ApplicationContextBuildUserTest { 9 public static void main(String[] args) { 10 //之前的方式建立對象 11 // User user = new User(); 12 // System.out.println(user); 13 14 //如今使用Spring框架的容器來建立對象 15 //第一步:建立Spring容器 16 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 17 18 /*第二步:從ApplicationContext容器獲取Bean對象 19 * 1.構造器獲取對象 20 * 語法格式: 21 * 類型名 變量名 = 容器對象.getBean("對象標識符",組件類型); 22 * 舉例: 23 * User user = ac.getBean("user", User.class); 24 * 2.靜態工廠獲取對象 25 * 語法格式: 26 * 類型名 變量名 = 容器對象.getBean("靜態工廠標識符",組件類型); 27 * 舉例: 28 * User user = ac.getBean("staticFactory", User.class); 29 * */ 30 User user = ac.getBean("dynamicCreateUser", User.class); 31 System.out.println(user); 32 } 33 }
第五步:運行結果
Bean對象的做用域:
指定Spring容器建立Bean對象的做用域:
注意:singleton單例模式,經過getBean獲取的對象HashCode是相同的,即user1 = user2返回true
prototype多例模式,經過getBean獲取的對象HashCode是不一樣的,即user1 = user2返回false
Bean對象的參數注入:(看上面的applicationContext.xml中註釋寫的知識點)
容器能夠創建Bean對象之間的關係,實現技術途徑就是DI注入,Spring DI注入setter注入和構造器注入兩種
組件掃描:Spring提供了一套基於註解配置的的使用方法,使用該方法能夠大大簡化XML的配置信息。
開啓組件掃描,能夠利用註解方式應用IOC,使用方法以下:
第一步:除了須要引入springIOC相關jar包以外還須要引入SpringAOP相關jar包(須要相關jar包的網友請留言私聊)
aopalliance-1.0.jar
aspectjweaver-1.5.3.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
第二步:在applicationContext.xml中添加啓用標記(用了組件掃描就能夠不用bean標籤了)
<context:component-scan base-package="包路徑(用最大的,例如:com.springioc)"/>
第三步:在組件類中追加如下標記
注意:1.掃描組件後,默認id值爲組件類名首字母小寫,也能夠自定義id。例如:@Component("stu")
2.掃描組件後,默認scope爲singleton單例,也能夠進行指定。例如:@Scope("prototyppe")
3.也能夠指定初始化和銷燬的方法,例如,在組件類的方法前追加@PostConstruct來指定初始化方法,追加@PreDestory來指定銷燬方法
4.將全部bean組件掃描到Spring容器後,能夠使用如下註解指定注入關係
@Autowired/@Qulifier:能夠處理構造器注入和setter注入
@Resource:只能處理Setter注入,但大部分狀況都是setter注入
5.@Value註解能夠注入Spring表達式值
首先讀取db.properties文件,封裝成Properties對象,在applicationContext.xml中添加
<util:properties id="jdbc" location="classpath:jdbc.properties"></util:properties>
而後在組件類屬性變量或Setter方法前使用@Value註解
@Value("#{db.url}")
private String url;
追加標記舉例:
1 @Component//默認實體類id名稱是實體類名稱(首字母小寫)
2 @Scope("prototype")//默認爲單例模式,此處修改成多例模式 3 public class User { 4 }
第四步:測試獲取bean對象:
1 package com.springioc.test; 2 3 import org.springframework.context.ApplicationContext;//spring-context.jar包中的 4 import org.springframework.context.support.ClassPathXmlApplicationContext;//spring-context-support.jar包中的 5 6 import com.springioc.entity.User; 7 8 public class ApplicationContextBuildUserTest { 9 public static void main(String[] args) { 10 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 11 User user = ac.getBean("user", User.class);//此處的id:user是從@Component默認實體類id中得出來的 12 System.out.println(user); 13 } 14 }
運行結果:
SpringIOC實現解耦(重點理解)
解耦主題:經過hibernate和JDBC兩種技術實現一個用戶的刪除操做
1.XML配置文件版的解耦
第一步:編寫用戶實體類和用戶DAO接口
1 //用戶DAO實現接口 2 package com.springiocDecoupling.usedao; 3 4 public interface UserDAO { 5 public void delete(); 6 }
第二步:編寫Hibernate技術和JDBC技術分別實現刪除操做的類
1 //JDBC實現DAO刪除操做 2 package com.springiocDecoupling.usedao; 3 4 public class JDBCDAO implements UserDAO{ 5 public void delete(){ 6 System.out.println("經過JDBC實現User刪除"); 7 } 8 }
1 //Hibernate實現DAO刪除操做 2 package com.springiocDecoupling.usedao; 3 4 public class HibernateDAO implements UserDAO{ 5 public void delete(){ 6 System.out.println("經過Hibernate實現User刪除"); 7 } 8 }
第三步:編寫一個控制用戶刪除操做的控制類
//控制類,控制刪除操做 package com.springiocDecoupling.entity.Controller; import org.springframework.stereotype.Controller; import com.springioc_Decoupling.usedao.UserDAO; @Controller//用於Spring框架管理 public class UserController { private UserDAO dao; public UserDAO getDao() { return dao; } public void setDao(UserDAO dao) { this.dao = dao; } public void delete(){ dao.delete(); } }
第四步:配置applicationContext.xml信息
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 8 xmlns:jee="http://www.springframework.org/schema/jee" 9 xmlns:mvc="http://www.springframework.org/schema/mvc" 10 xmlns:util="http://www.springframework.org/schema/util" 11 xsi:schemaLocation=" 12 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 14 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 15 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 16 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 17 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 18 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 19 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 20 <bean id = "jdao" class = "com.springioc_Decoupling.usedao.JDBCDAO"></bean> 21 <bean id = "hdao" class = "com.springioc_Decoupling.usedao.HibernateDAO"></bean> 22 23 <bean id = "ucontroller" class="com.springiocDecoupling.entity.Controller.UserController"> 24 <property name="dao" ref="jdao"></property><!--解耦操做就體如今此處,要用jdbc,ref屬性修改成jdao就好了,要用hibernate,ref屬性修改成hdao就好了--> 25 </bean> 26 </beans>
第五步:編寫測試類
1 package com.springiocDecoupling.entity.test; 2 3 import org.springframework.context.ApplicationContext;//spring-context.jar包中的 4 import org.springframework.context.support.ClassPathXmlApplicationContext;//spring-context-support.jar包中的 5 6 import com.springiocDecoupling.entity.Controller.UserController; 7 8 public class ApplicationContextBuildUserTest { 9 public static void main(String[] args) { 10 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 11 UserController ucon = ac.getBean("ucontroller", UserController.class); 12 ucon.delete(); 13 } 14 }
測試結果:
2.經過註解的方式進行解耦
第一步:編寫用戶實體類和用戶DAO接口
第二步:第二步:編寫Hibernate技術和JDBC技術分別實現刪除操做的類
1 package com.springioc_Decoupling.usedao; 2 3 import org.springframework.stereotype.Repository; 4 5 @Repository("jdao") 6 public class JDBCDAO implements UserDAO{ 7 public void delete(){ 8 System.out.println("經過JDBC實現User刪除"); 9 } 10 }
1 package com.springioc_Decoupling.usedao; 2 3 import org.springframework.stereotype.Repository; 4 5 @Repository("hdao") 6 public class HibernateDAO implements UserDAO{ 7 public void delete(){ 8 System.out.println("經過Hibernate實現User刪除"); 9 } 10 }
第三步:編寫一個控制用戶刪除操做的控制類
1 package com.springiocDecoupling.entity.Controller; 2 import javax.annotation.Resource; 3 4 import org.springframework.stereotype.Controller; 5 6 import com.springioc_Decoupling.usedao.UserDAO; 7 8 @Controller("ucontroller") 9 public class UserController { 10 //若是使用Autowired的話須要Qualifier搭配,例如:
11 //@Autowired
11 //@Qualifier("jdao") 11 @Resource(name="jdao")//解耦操做就體如今此處,要用jdbc修改成jdao就好了,要用hibernate修改成hdao就好了 12 private UserDAO dao; 13 public UserDAO getDao() { 14 return dao; 15 } 16 public void setDao(UserDAO dao) { 17 this.dao = dao; 18 } 19 public void delete(){ 20 dao.delete(); 21 } 22 }
第四步:配置applicationContext.xml信息,啓動組件掃描
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 8 xmlns:jee="http://www.springframework.org/schema/jee" 9 xmlns:mvc="http://www.springframework.org/schema/mvc" 10 xmlns:util="http://www.springframework.org/schema/util" 11 xsi:schemaLocation=" 12 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 14 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 15 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 16 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 17 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 18 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 19 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 20 21 <!-- 22 <bean id = "jdao" class = "com.springioc_Decoupling.usedao.JDBCDAO"></bean> 23 <bean id = "hdao" class = "com.springioc_Decoupling.usedao.HibernateDAO"></bean> 24 25 <bean id = "ucontroller" class="com.springiocDecoupling.entity.Controller.UserController"> 26 <property name="dao" ref="hdao"></property> 27 </bean> 28 --> 29 30 <context:component-scan base-package="com.springiocDecoupling"></context:component-scan> 31 </beans>
第五步:編寫測試類
測試結果: