您的位置:首页 > 编程语言 > Java开发

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

2016-08-15 12:00 666 查看
出自: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配置文件的具体实现
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >

<!-- 配置 dataSource -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/blog_ssh">
</property>
<property name="username" value="root"></property>
<property name="password" value="43995201"></property>
</bean>

<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com.lee.pojo.Article.hbm.xml</value>
<value>com.lee.pojo.ArticleType.hbm.xml</value>
<value>com.lee.pojo.Consumer.hbm.xml</value>
<value>com.lee.pojo.Discuss.hbm.xml</value>
<value>com.lee.pojo.Friend.hbm.xml</value>
<value>com.lee.pojo.Photo.hbm.xml</value>
<value>com.lee.pojo.Restore.hbm.xml</value>
<value>com.lee.pojo.Vote.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="tansacationManger" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 配置事务的传播特行(JoinPoint) -->
<tx:advice id="txAdvice" transaction-manager="tansacationManger">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 配置那些类参与事务(PointCut) -->
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.lee.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐