簡述Spring:
Spring學習價值:
方便解耦,簡化開發:Spring就是一個大工廠,可以將所有對象創建和依賴關係維護,交給Spring管理;
AOP編程的支持: Spring提供面向切面編程,可以方便的實現對程序進行權限攔截、運行監控等功能;
聲明式事務的支持: 只需要通過配置就可以完成對事務的管理,而無需手動編程;
方便程序的測試: Spring對Junit4支持,可以通過註解方便的測試Spring程序;
方便集成各種優秀框架: Spring不排斥各種優秀的開源框架,其內部提供了對各種優秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持;
降低JavaEE API的使用難度: Spring 對JavaEE開發中非常難用的一些API(JDBC、JavaMail、遠程調用等),都提供了封裝,使這些API應用難度大大降低;
JavaEE開發三層結構:
- WEB層:Spring MVC;
- 業務層:Bean管理:(IOC);
- 持久層:Spring的JDBC模板.ORM模板用於整合其他的持久層框架;
IoC底層實現原理(控制反轉. 指的是 對象的創建權反轉(交給)給Spring,作用是實現了程序的解耦合):
Spring開發步驟:
- 步驟1:下載Spring開發包
- 下載地址:http://repo.springsource.org/libs-release-local/org/springframework/spring
- Spring目錄結構:
docs: API和開發規範;
libs: jar包和源碼;
schema: 約束;
- 步驟2:創建Web,引入開發包
- 步驟3:引入相關配置文件
log4j.properties
applicationContext.xml//均放到src下
引入約束:
spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
<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>
- 步驟4:編寫相關的類
public interface UserDao {
public void sayHello();
}
public class UserDaoImpl implements UserDao {
@Override
public void sayHello() {
System.out.println("Hello Spring...");
}
}
- 步驟5:編寫配置
完成UserService的配置
<!-- Spring的入門案例================ -->
<bean id="userDao" class="cn.xp.spring.demo1.UserDaoImpl"></bean>
- 步驟6:編寫測試
@Test
// Spring的方式:
public void demo2(){
// 創建Spring的工廠類:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 通過工廠解析XML獲取Bean的實例.
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.sayHello();
}
Spring中的工廠:ApplicationContext接口
ClassPathXmlApplicationContext :加載類路徑下Spring的配置文件;
FileSystemXmlApplicationContext :加載本地磁盤下Spring的配置文件;
Spring中<bean>標籤的相關配置:
- id屬性與name屬性標籤的配置
id: Bean起個名字, 在約束中採用ID的約束:唯一,必須以字母開始,可以使用字母、數字、連字符、下劃線、句話、冒號 id: 不能出現特殊字符.<bean id=”bookAction”>;
name: Bean起個名字., 沒有采用ID的約束,name:出現特殊字符,如果<bean>沒有id的話 , name可以當做id使用
- scope屬性:Bean的作用範圍
- * singleton :默認值,單例的;
- * prototype :多例的;
- * request :WEB項目中,Spring創建一個Bean的對象,將對象存入到request域中;
- * session :WEB項目中,Spring創建一個Bean的對象,將對象存入到session域中;
* globalSession :WEB項目中,應用在Porlet環境.如果沒有Porlet環境那麼globalSession相當於session;
- Bean的生命週期配置:
通過配置<bean>標籤上的init-method作爲Bean的初始化的時候執行的方法,配置destroy-method作爲Bean的銷燬的時候執行的方法。銷燬方法想要執行,需要是單例創建的Bean而且在工廠關閉的時候,Bean纔會被銷燬;
- class屬性:bean對象的全路徑;
依賴注入(DI):
依賴注入,在Spring框架負責創建bean對象時,動態的將依賴注入到bean對象中;
例如:UserServiceImpl的實現類中有一個屬性,那麼使用Spring的DI功能時可以把屬性的值傳進來;
具體配置:
<bean id="userService" class="com.stx.demo2.UserServiceImpl">
<property name="name" value="買"></property>
</bean>
調用配置://<property name="customerDao" ref="具體bean的ID或者name"></property>
<bean id="CustomerDao" class="com.stx.demo3.CustomerDaoImpl"></bean>
<bean id="CustomerService" class="com.stx.demo3.CustomerServiceImpl">
<property name="customerDao" ref="CustomerDao"></property>
</bean>
Spring框架的屬性注入:
- 對類成員變量注入,常用的有兩種
- 構造方法注入
<!-- 第一種:構造方法的方式 --> <bean id="car" class="cn.xp.spring.demo4.Car"> <constructor-arg name="name" value="保時捷"/> <constructor-arg name="price" value="1000000"/> </bean>
- 屬性setter方法注入(☆)
<!-- 第二種:set方法的方式 --> <bean id="car2" class="cn.itcast.spring.demo4.Car2"> <property name="name" value="QQ"/> <property name="price" value="40000"/> </bean>
- P名稱空間的注入
- spEL注入方式
- 數組,集合(List,Set,Map)properties注入
<bean id="collectionBean" class="cn.xp.spring.demo5.CollectionBean"> <!-- 數組類型的屬性 -->(和集合List注入一樣) <property name="arrs"> <list> <value>111</value> <value>222</value> <value>333</value> </list> </property> <!-- 注入List集合的數據 --> <property name="list"> <list> <value>111</value> <value>222</value> <value>333</value> </list> </property> <!-- 注入Map集合 --> <property name="map"> <map> <entry key="aaa" value="111"/> <entry key="bbb" value="222"/> <entry key="ccc" value="333"/> </map> </property> <!-- Properties的注入 --> <property name="properties"> <props> <prop key="username">root</prop> <prop key="password">123</prop> </props> </property> </bean>
Spring的分配置文件的開發(瞭解)
一種:創建工廠的時候加載多個配置文件:
ApplicationContext applicationContext = newClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
二種:在一個配置文件中包含另一個配置文件:(複製到第一個就行)
<importresource="applicationContext2.xml"></import>