Spring学习----------AOP以及Spring配置文件详解


本文转自于:http://blog.csdn.net/lixiang19900718/article/details/8537671


            之前有写了spring的一个特性IOC,现在在来写Spring的另一个特性AOP,AOP中个一些概念,看了网络上的讲解AOP的文章,讲的都很正确,可是不是很好理解,我在来写一点,可能不太精确,但理解容易点。AOP是用动态代理(装饰模式),解决横切行问题:下面看下AOP中概念.











                 Aspect:是一个横切行问题的一个抽象。他不是具体实现,只是一个横切面。

                 

                Adivce:是对横切行问题的一个具体实现,比如我们对Service前进行安全检查,那么Advice可能就是一个安全检查的一个具体的类,其中实现了安全检查的一个具体的方法。

               

                JoinPoint:现在我们已经知道了横切性问题了(Aspect),和横切行问题的具体实现(Adivce)那个,我们在哪植入Advice了,我们在JoinPoint标出,比如Service中的addUser方法。

              

                PointCut:ok,现在有个JoinPoint,那么理解PointCut就容易了,PointCut就是对JoinPoint集合的一个选择规则,比如add*表示,所有的add开头的方法都是JoinPoint

               

                Weaving:是一个过程,是将Adivce植入到JoinPoint的一个动作。

       






     <!--   下面是网友总结的-->

  • 目标对象(Target Object):被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
  • AOP代理(AOP Proxy)在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将<aop:config> 的proxy-target-class 属性设为true





下面我们看下Spring配置文件的具体实现

[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.     xmlns:p="http://www.springframework.org/schema/p"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  9.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"  
  10.      >  
  11.   
  12.     <!-- 配置 dataSource -->  
  13.     <bean id="dataSource"  
  14.           class="org.apache.commons.dbcp.BasicDataSource">  
  15.         <property name="driverClassName"  
  16.             value="com.mysql.jdbc.Driver">  
  17.         </property>  
  18.         <property name="url"  
  19.             value="jdbc:mysql://localhost:3306/blog_ssh">  
  20.         </property>  
  21.         <property name="username" value="root"></property>  
  22.         <property name="password" value="43995201"></property>  
  23.     </bean>  
  24.       
  25.     <!-- 配置sessionFactory -->  
  26.     <bean id="sessionFactory"  
  27.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  28.         <property name="dataSource">  
  29.             <ref bean="dataSource" />  
  30.         </property>  
  31.         <property name="mappingResources">  
  32.            <list>  
  33.               <value>com.lee.pojo.Article.hbm.xml</value>  
  34.               <value>com.lee.pojo.ArticleType.hbm.xml</value>  
  35.               <value>com.lee.pojo.Consumer.hbm.xml</value>  
  36.               <value>com.lee.pojo.Discuss.hbm.xml</value>  
  37.               <value>com.lee.pojo.Friend.hbm.xml</value>  
  38.               <value>com.lee.pojo.Photo.hbm.xml</value>  
  39.               <value>com.lee.pojo.Restore.hbm.xml</value>  
  40.               <value>com.lee.pojo.Vote.hbm.xml</value>  
  41.            </list>  
  42.         </property>  
  43.         <property name="hibernateProperties">  
  44.             <props>  
  45.                 <prop key="hibernate.dialect">  
  46.                     org.hibernate.dialect.MySQLDialect  
  47.                 </prop>  
  48.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  49.                 <prop key="hibernate.show_sql">true</prop>  
  50.             </props>  
  51.         </property>  
  52.     </bean>  
  53.       
  54.     <!-- 配置事务管理器 -->  
  55.     <bean id="tansacationManger" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  56.          <property name="sessionFactory" ref="sessionFactory"></property>  
  57.     </bean>  
  58.       
  59.     <!-- 配置事务的传播特行(JoinPoint) -->  
  60.     <tx:advice id="txAdvice" transaction-manager="tansacationManger">   
  61.       <tx:attributes>  
  62.         <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>  
  63.         <tx:method name="*" read-only="true"/>   
  64.       </tx:attributes>   
  65.     </tx:advice>    
  66.       
  67.     <!-- 配置那些类参与事务(PointCut) -->  
  68.     <aop:config>   
  69.        <aop:pointcut id="transactionPointcut" expression="execution(* com.lee.service.*.*(..))"/>   
  70.        <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>   
  71.     </aop:config>   
  72. </beans>  

<!--这个链接是Spring配置的 5种方法,楼主顶http://www.blogjava.net/robbie/archive/2009/04/05/264003.html-->



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值