Spring的AOP和事物管理
AOP(面向切面)技術(shù): Aspect-Oriented Programming,是對OOP的一種補(bǔ)充。
1.AOP和OOP的主要區(qū)別、:OOP針對實(shí)體及其屬性和行為進(jìn)行抽象封裝
AOP針對業(yè)務(wù)處理過程中的切面進(jìn)行提取,它所面對的是處理過程中的某個(gè)步驟或階段,比如權(quán)限檢查,日志寫入,事務(wù)管理,并不需要對OOP采取嵌入式代碼,而是采取在將通知在切點(diǎn)織入的方式。 還是通過容器注入進(jìn)行管理。
2. AOP常用術(shù)語:a.切面(Aspect):是你要實(shí)現(xiàn)的交叉功能,是一種描述性的思想。
b.連接點(diǎn)(Joinpoint):應(yīng)用程序執(zhí)行過程中插入切面的地點(diǎn)(如方法調(diào)用,異常拋出),但連接點(diǎn)不一定要插入切面。
c.通知(Advice):切面的實(shí)際實(shí)現(xiàn),是一種邏輯代碼。
d.切入點(diǎn)(Pointcut):定義了通知應(yīng)用在哪些連接點(diǎn)。e.目標(biāo)對象(Target):被通知對象
e.代理(Proxy):將通知應(yīng)用到目標(biāo)對象后創(chuàng)建的對象。
f.織入:(Weaving):將切面應(yīng)用到目標(biāo)對象從而創(chuàng)建一個(gè)新的代理對象的過程。
備注:代理模式的優(yōu)勢:授權(quán)機(jī)制:不同級(jí)別的用戶對同一對象擁有不同的訪問權(quán)利
某個(gè)客戶端不能直接操作到某個(gè)對象,但又必須和那個(gè)對象有所互動(dòng),可節(jié)省內(nèi)存開銷
客戶端只與接口交互,通過代理模式可以get不同的業(yè)務(wù)對象。
七:面向切面編程的應(yīng)用:事務(wù)管理(Spring分程序控制和聲明式)
1.描述事務(wù)的縮寫詞(ACID)
a.(Atomic):原子性:事務(wù)由一個(gè)或多個(gè)行為綁在一起組成,好像是一個(gè)單獨(dú)工作單元。
原子性確保在事務(wù)中的所有操作要么都發(fā)生,要么都不發(fā)生。
b.(Consistent):一致性:一旦一個(gè)事物結(jié)束了(不管成功與否),系統(tǒng)所處的狀態(tài)和它的業(yè)務(wù)規(guī)則是一致的。
c.(Isolated):隔離性:事務(wù)應(yīng)該允許多個(gè)用戶操作同一個(gè)數(shù)據(jù),一個(gè)用戶的操作不會(huì)和其他用戶的操作相混淆。
d.(Durable):一旦事務(wù)完成,事務(wù)的結(jié)果應(yīng)該持久化。
2.事務(wù)屬性:是對事務(wù)策略如何應(yīng)用到方法的描述。
a.傳播行為(Propagation):定義了客戶端和被調(diào)用方法的事務(wù)邊界,和ejb中傳播規(guī)則相同,只是加了前綴PROPAGATION,如:PROPAGATION_REQUIRED
b.隔離級(jí)別(Isolation):一般采用數(shù)據(jù)庫默認(rèn)行為即可,在理想狀態(tài)下,事物要完全相互隔離,然而完全隔離會(huì)影響性能,有時(shí)需要在事務(wù)隔離上有些彈性。
c.只讀(readOnly):如果一個(gè)事務(wù)只對數(shù)據(jù)庫執(zhí)行讀操作,數(shù)據(jù)庫就可能利用事務(wù)只讀的特性,使用某些優(yōu)化措施。
d.事務(wù)超時(shí)(timeOut):只對具有啟動(dòng)新事務(wù)的傳播行為的方法的事務(wù)設(shè)置超時(shí)才有意義,即:required,required_new,nested.
3.聲明式事務(wù)
a.model:public class User {private Integer id; private String name;
private Integer age; //getter/setter; }
b.Dao: public interface UserDao { public Integer addUser(User user);
public User getUser(Integer userId); public List getUsers(); }
c.DaoImpl: public class UserDaoHibernate extends HibernateDaoSupport implements UserDao { public Integer addUser(User user) {
return (Integer) getHibernateTemplate().save(user); }
public User getUser(Integer userId) {
return (User) getHibernateTemplate().load(User.class, userId); }
public List getUsers() { String hql="from User";
return getHibernateTemplate().find(hql); } }
d.service: public interface UserManager { public Integer addUser(User user);}
e.serviceImpl: public class UserManagerImpl implements UserManager {
private UserDao userDao;
public Integer addUser(User user) {Integer userId=userDao.addUser(user);
return userId; } //對數(shù)據(jù)庫的操作根本不用代碼嵌入式transaction
public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
f.hbm.xml: <hibernate-mapping package="mypack"><class name="User" table="user">
<id name="id" column="id" type="integer"><generator class="identity"/> </id>
<property name="name" column="name" type="string"></property>
<property name="age" column="age" type="integer"></property> </class>
</hibernate-mapping>
g.applicationContext.xml: <bean id="userDao" class="mypack.UserDaoHibernate">
<property name="sessionFactory" ref="sessionFactory" /> //為supportDao注入
<bean id="userManagerTarget" class="mypack.UserManagerImpl">
<property name="userDao" ref="userDao"></property> </bean>
<bean id="transactionManager" //真正的事務(wù)管理實(shí)體對象
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/> </bean>
<bean id="userManager"http://通過代理模式參照一個(gè)方法的事務(wù)屬性決定如何執(zhí)行事務(wù)策略。
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"></property>//通知
<property name="target" ref="userManagerTarget"></property> //目標(biāo)
<property name="transactionAttributes">
<props> <prop key="add*">PROPAGATION_REQUIRED</prop> //切點(diǎn)
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> </props>
</property> </bean>
h.測試:public void testTransaction(){User user=new User();user.setName("liming");
user.setAge(22); Integer userId=userMgr.addUser(user); }
4.用元數(shù)據(jù)聲明事務(wù):
a.將service接口加上@ Transactional聲明,當(dāng)然也可以加在serviceImpl實(shí)現(xiàn)類上
@Transactional(propagation=Propagation.REQUIRES_NEW),此處表示聲明該類所有方法
public interface UserManager { public Integer addUser(User user);}
//也可對具體方法聲明
b.applicationContext.xml變成如下形式:使支持對元數(shù)據(jù)的支持
<bean id="userManager" class="mypack.UserManagerImpl">
<property name="userDao" ref="userDao"></property> </bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.
AnnotationTransactionAttributeSource"></bean> </property> </bean>
<bean class="org.springframework.transaction.interceptor.
TransactionAttributeSourceAdvisor"> <property name="transactionInterceptor" ref="transactionInterceptor"></property> </bean>
posted on 2007-08-19 14:42 mrklmxy 閱讀(1543) 評(píng)論(0) 編輯 收藏