Spring理解

         Spring是一個開源框架,Spring是於 2003 年興起的一個輕量級的 Java 開發框架,框架的主要優勢之一就是其分層架構,分層架構允許使用者選擇使用哪一個組件,同時爲 J2EE 應用程序開發提供集成的框架。Spring 使用基本的 JavaBean來完成以前只可能由 EJB 完成的事情。然而, Spring 的用途不僅限於服務器端的開發。從簡單性、可測試性和鬆耦合的角度而言,任何 Java 應用都可以從 Spring 中受益。 Spring 的核心是控制反轉(IoC)和麪向切面(AOP)

IOC: Inversion of Control 控制反轉. 指的是 對象的創建權反轉(交給)給 Spring,作用是實現了程序的解耦合。

DI :Dependency Injection 依賴注入.需要有 IOC 的環境,Spring 創建這個類的過程中,Spring 將類的依賴的屬性設置進去。
 

AOP:即面向切面編程,AOP 對程序進行增強:不修改源碼的情況下,可以進行權限校驗,日誌記錄,性能監控,事務控制。

 


spring AOP底層實現原理:爲容器中的管理對象生成動態代理對象

代理機制:
Spring 的 AOP 的底層用到兩種代理機制:
    JDK 的動態代理 :針對實現了接口的類產生代理,沒有實現接口就不能產生動態代理對象
    Cglib 的動態代理 :針對沒有實現接口的類產生代理,應用的是底層的字節碼增強的技術,生成當前類的子類對象。
 

 

 

Java 開發分三層架構:

WEB層:spring

業務層:bean管理

持久層:bean管理,Spring 的 JDBC 模板.ORM(對象關係映射) 模板用於整合其他的持久層框架。

 

Spring 生成 Bean 的時候幾種方式:
【無參數的構造方法的方式:】
<!-- 方式一:無參數的構造方法的實例化 -->
<bean id="bean1" class="cn.itcast.spring.demo3.Bean1"></bean>
Spring 的屬性注入:對象類型的注入:
<!-- 注入對象類型的屬性 -->
<bean id="person" class="cn.itcast.spring.demo4.Person">
      <property name="name" value="會希"/>
      <!-- ref 屬性:引用另一個 bean 的 id 或 name -->
      <property name="car2" ref="car2"/>
</bean>

Spring 的 Bean 的屬性注入:

<!-- 第一種:構造方法的方式 -->
<bean id="car" class="cn.itcast.spring.demo4.Car">
      <constructor-arg name="name" value="保時捷"/>
      <constructor-arg name="price" value="1000000"/>
</bean>

<!-- 第二種: set 方法的方式 -->
<bean id="car2" class="cn.itcast.spring.demo4.Car2">
      <property name="name" value="奇瑞 QQ"/>
      <property name="price" value="40000"/>
</bean>

Spring 的分配置文件的開發

第一種:創建工廠的時候加載多個配置文件:
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
第二種:在一個配置文件中包含另一個配置文件:
<import resource="applicationContext2.xml"></import>


spring中Bean常用的註解:

@Component:組件.(作用在類上)
@Controller :WEB 層
@Service :業務層
@Repository :持久層
屬性注入的註解:
@Value :用於注入普通類型.
@Autowired :自動裝配:
默認按類型進行裝配.
按名稱注入:
@Qualifier:強制使用名稱注入.
@Resource 相當於:
@Autowired 和@Qualifier 一起使用.
bean作用範圍

@Scope:
singleton:單例
prototype:多例
bean的生命週期

@PostConstruct : 相當於 init-method
@PreDestroy : 相當於 destroy-method
 

AOP 的開發中的相關術語:
Joinpoint(連接點):所謂連接點是指那些被攔截到的點。在 spring 中,這些點指的是方法,因爲 spring 只支持方法類型的連接點.
Pointcut(切入點):所謂切入點是指我們要對哪些 Joinpoint 進行攔截的定義.
Advice(通知/增強):所謂通知是指攔截到 Joinpoint 之後所要做的事情就是通知.通知分爲前置通知,後置通知,異常通知,最終通知,環繞通知(切面要完成的功能)
Introduction(引介):引介是一種特殊的通知在不修改類代碼的前提下, Introduction 可以在運行期爲類動態地添加一些方法或Field.
Target(目標對象):代理的目標對象
Weaving(織入):是指把增強應用到目標對象來創建新的代理對象的過程.spring 採用動態代理織入,而 AspectJ 採用編譯期織入和類裝在期織入
Proxy(代理) :一個類被 AOP 織入增強後,就產生一個結果代理類
Aspect(切面): 是切入點和通知(引介)的結合
 

事務的回顧:
什麼是事務:

事務邏輯上的一組操作,組成這組操作的各個邏輯單元,要麼一起成功,要麼一起失敗.


事務特性:
原子性 :強調事務的不可分割.
一致性 :事務的執行的前後數據的完整性保持一致.
隔離性 :一個事務執行的過程中,不應該受到其他事務的干擾
持久性 :事務一旦結束,數據就持久到數據庫


如果不考慮隔離性引發安全性問題:
髒讀 :一個事務讀到了另一個事務的未提交的數據
不可重複讀 :一個事務讀到了另一個事務已經提交的 update 的數據導致多次查詢結果不一致.
虛幻讀 :一個事務讀到了另一個事務已經提交的 insert 的數據導致多次查詢結果不一致.


 解決讀問題:設置事務隔離級別
未提交讀 :髒讀,不可重複讀,虛讀都有可能發生
已提交讀 :避免髒讀。但是不可重複讀和虛讀有可能發生
可重複讀 :避免髒讀和不可重複讀.但是虛讀有可能發生.
串行化的 :避免以上所有讀問題.


Mysql 默認:可重複讀
Oracle 默認:讀已提交
 

事務的傳播行爲  PROPAGION_XXX
* 保證同一個事務中
PROPAGATION_REQUIRED 支持當前事務,如果不存在 就新建一個(默認)
PROPAGATION_SUPPORTS 支持當前事務,如果不存在,就不使用事務
PROPAGATION_MANDATORY 支持當前事務,如果不存在,拋出異常
* 保證沒有在同一個事務中
PROPAGATION_REQUIRES_NEW 如果有事務存在,掛起當前事務,創建一個新的事務
PROPAGATION_NOT_SUPPORTED 以非事務方式運行,如果有事務存在,掛起當前事務
PROPAGATION_NEVER 以非事務方式運行,如果有事務存在,拋出異常
PROPAGATION_NESTED 如果當前事務存在,則嵌套事務執行


<!--開啓事務管理的註解:-->
<tx:annotation-driven transaction-manager="transactionManager"/>

SSM項目的註解事務:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	<!-- 事務管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 數據源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 傳播行爲 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* cn.e3mall.service..*.*(..))" />
	</aop:config>
</beans>
<!-- 加載配置文件 -->
	<context:property-placeholder location="classpath:conf/*.properties" />
	<!-- 數據庫連接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="maxActive" value="10" />
		<property name="minIdle" value="5" />
	</bean>

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章