aop详解

来源:互联网 发布:现场弹幕源码 编辑:程序博客网 时间:2024/05/01 08:09

关于aop的配置文件

<!-- 告诉spring去扫描某个包下(包括它的子包)的所有注解@Component(@Controller,@Service,@Reposity)的类 -->    <context:component-scan base-package="service"></context:component-scan>    <!-- 定义了一个bean -->    <bean id="logBean" class="util.Log"></bean>        <!-- 这里表示要配置aop -->    <aop:config>       <!-- 切入点 -->       <aop:pointcut id="allServiceMethod" expression="execution(* service.*.*(..))" />          <aop:aspect ref="logBean"><!-- 定义切面,切面下包含很多通知 -->           <!--             <aop:before method="logBefore"  pointcut-ref="allServiceMethod"/>           <aop:after method="logAfter"  pointcut-ref="allServiceMethod"/>           <aop:around method="logAround" pointcut-ref="allServiceMethod"/>           <aop:after-returning method="logReturn" pointcut-ref="allServiceMethod" returning="retVal"/>           <aop:after-throwing method="logThrow" pointcut-ref="allServiceMethod"/>           -->           <aop:after-throwing method="logThrowWithException" pointcut-ref="allServiceMethod" throwing="ex"/>       </aop:aspect>    </aop:config></beans>

切入点<pre name="code" class="html"><aop:pointcut id="allServiceMethod" expression="execution(* service.*.*(..))" />
第一个*代表任意返回值都可以;后面接一个包,如service包;第二个*代表service包下所有的类(加了@Component的类,没
有加的不归spring管理);第三个*指的是类下面的方法;而(..)中的..代表方法里面的参数.
整句话意思是在service下的所有方法进行切入


 <aop:before method="logBefore"  pointcut-ref="allServiceMethod"/> <aop:after method="logAfter"  pointcut-ref="allServiceMethod"/>步骤如下:1.<!-- 定义了一个bean -->  <bean id="logBean" class="util.Log"></bean>  首先定义一个bean,用来切进去。2.<!-- 切入点 -->  <aop:pointcut id="allServiceMethod" expression="execution(* service.*.*(..))" />  定义切入点,每一个点都要切入这个bean。3.<aop:aspect ref="logBean"><!-- 定义切面,切面下包含很多通知 -->  定义了切入点之后,每一个点都要切入这个切面,将上面定义的bean变成切面。<pre name="code" class="html">  <aop:before method="logBefore"  pointcut-ref="allServiceMethod"/>

4.<aop:before method="logBefore" pointcut-ref="allServiceMethod"/>
  这一类其实都是logBean这个类(切面)下的方法。类--切面,方法--通知。
你需要的是什么通知方式,前通知后通知,环绕通知等等。



<span style="font-family:Microsoft YaHei;font-size:14px;">@component<span style="font-size:14px;"></span></span><pre id="best-content-686974203" class="best-text mb-10"><span style="font-family:Microsoft YaHei;font-size:14px;">在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释这里就是说把这个类交给Spring管理,重新起个名字叫userManager,由于不好说这个类属于哪个层面,就用@Component</span>

                                             
0 0