spring

IOC控制反轉

意思是將你設計的類交給系統(指的是spring容器)去控制,而不是在你自己的類內部控制
通過spring容器對類進行初始化,實例化,調用,銷燬
如果沒有引入spring容器,你所設計的類與類間存在依賴關係,通過類與類,對象與對象間的互相合作,完成所要完成的業務邏輯
這裏寫圖片描述
通過引入spring ioc容器,就像給各個類間加上了一個第三方,類與類間,對象與對象間失去了依賴,軟件系統中的耦合度大大降低
這裏寫圖片描述
Spring IoC使用方法:
—如果UserService調用UserDAO—
a.在UserService實現類中,添加一個UserDAO接口類型屬性及setter方法.
b.將UserService和UserDAO都交給Spring容器管理
c.在UserService的定義中使用元素指定調用關係.
定義格式如下

    <property name="屬性名" ref="容器中bean組件id值">

DI依賴注入

DI是實現IoC控制的主要技術.
例如有類A與類B
在B中需要調用類A
原來我們在沒有學習spring依賴注入時的做法是
在類B中new一個類A

在瞭解spring依賴注入之前,我們首先要了解什麼是依賴注入,簡單的來說,依賴注入就是在你需要的時候,spring爲你提供你所需要的,而無需你自己去實例化你所需要的

注入的兩種方法:
a.setter方式注入(推薦使用) 設值注入
b.構造方式注入
注入的使用
使用時需要注入各種類型數據,主要有以下類型
a.注入Bean對象
b.注入基本類型,例如字符串,數值,日期等
c.注入集合類型,例如List,Set,Map,Properties
如果設值注入與構造注入同時存在,最後我們看到的將是設值注入的結果,因爲在實例化一個對象時,總會先執行構造方法,再初始化成員變量。所以我們最後看到的會是設值注入的結果

AOP面向切面編程

代理模式,
spring AOP 代理管理事務

spring 是貫穿表現層,業務層,實體層的多層的j2ee應用框架
這裏寫圖片描述

spring的配置文件applicationContext.xml的配置
1.放在src目錄底下
參考applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- DriverMangerDataSource -->
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 所用數據庫驅動 -->
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<!--所用數據庫路徑 -->
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"></property>
<!-- 所用數據庫賬號密碼 -->
<property name="username" value="wsx"></property>
<property name="password" value="wsx"></property>
</bean>
<bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop> key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect
</prop>
<prop>
key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext
</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:com/bean/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 事務管理器 -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事務通知 -->
<tx:advice id="basicAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- spring AOP 代理管理事務 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="basicPoint" expression="execution(* com.service.*.*(..))" />
<aop:advisor advice-ref="basicAdvice" pointcut-ref="basicPoint" />
<bean name="dictionaryIFC" class="com.service.impl.DictionaryService">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean name="noticeService" class="com.service.NoticeService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="mailService" class="com.service.MailService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="surveyService" class="com.service.SurveyService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>

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