spring_AOP_XML

例子下载

  对于xml的AOP配置主要集中在配置文件中,所以只要设置好配置文件就行了

beans.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.bjsxt"/>

    <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
    <aop:config>
        <aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))" id="logService"/>
        <aop:aspect id="logAspect" ref="logInterceptor">
            <aop:before method="before" pointcut-ref="logService" />
        </aop:aspect>
    </aop:config>
</beans>

程序运行时先会查看aop:pointcut里面的expression,如果调用的方法在此里面,则调用相应的切面。

上面总括的意思为:程序在调用com.bjsxt.service包下面的add方法或子包等下面add方法(任意参数)时,先调用com.bjsxt.aop.LogInterceptor里面的before方法。

spring_AOP_XML

时间: 2024-08-09 22:00:00

spring_AOP_XML的相关文章

Spring_AOP_XML使用Aspect实现动态代理(常用)

Spring_AOP_XML使用Aspect实现动态代理(常用) XML使用Aspect实现动态代理此方式比较常用,和使用注解最大的好处是我们不用每个方法前面定义横切点上面加入PointCut的说明,在XML中只需要定义一次就可以多出使用. 在上面Spring_AOP_Annotation使用Aspect实现动态代理的基础上修改,去除LogAspect中方法上的注解. XML文件配置: <?xml version="1.0" encoding="UTF-8"?

spring的AOP编程

基于注解的方式 cfg.xml中 1 <!-- 通过注释的方式来实现AOP所以要扫描包 --> 2 <context:component-scan base-package="spring_aop_helloworld"></context:component-scan> 3 <!-- 要把切面自动生成代理写上 --> 4 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>