Spring AOP @Aspect support XML

9 篇文章 0 订阅
  1. SpringAOP在集成了AspectJ后,立刻就显得方便了许多。本篇用同样的例子,来实现同样的功能。

    前文提到过,@Aspect也支持完全annotation方式和XML配置的方式。为了方便比较,我这里先用XML来配置,基于上篇文章的代码。

     

  2. 添加AspectJdependency

    <dependency>

                <groupId>org.aspectj</groupId>

                <artifactId>aspectjrt</artifactId>

                <version>1.6.12</version>

            </dependency>

     

            <dependency>

                <groupId>org.aspectj</groupId>

                <artifactId>aspectjweaver</artifactId>

                <version>1.6.12</version>

            </dependency>

  3. 创建Aspect

    @Aspect

    public class MyAspect {

     

    public void before(JoinPoint joinPoint)

    {

    System.out.println("Before method...");

    Statistic.increaseTotalTraffic();

    }

     

    public void after(JoinPoint joinPoint)

    {

    System.out.println("After return...");

    Statistic.increaseIncome(HALL_NAME.DINOSAUR, BigDecimal.valueOf(2l));

    }

    }

  4. 修改配置文件
    1. 引入aopschema

    <beans xmlns="http://www.springframework.org/schema/beans"

          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

          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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    1. 加入aop内容

    <beans xmlns="http://www.springframework.org/schema/beans"

          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

          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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

     

        <!-- Configure Target Objects -->

        <bean id="zoo" class="com.edi.poc.Zoo">

            <constructor-arg value="People"/>

        </bean>

        <bean id="dinoHall" class="com.edi.poc.DinoHall"/>

        <bean id="jack" class="com.edi.poc.Tourer">

            <constructor-arg value="Jack"/>

        </bean>

     

        <bean id="myAspect" class="com.edi.poc.aop.MyAspect"/>

       

        <aop:config>

            <aop:aspect id="customAspect" ref="myAspect">

                <!-- @Before -->

                <aop:pointcut id="pointcutBefore" expression="execution(* com.edi.poc.Zoo.enter(..))"/>

                <aop:pointcut id="pointcutAfter" expression="execution(* com.edi.poc.ifc.Hall.visit(..))"/>

                <aop:before method="before" pointcut-ref="pointcutBefore"/>

                <aop:after-returning method="after" pointcut-ref="pointcutAfter"/>

            </aop:aspect>

        </aop:config>

    </beans>

    1. 运行

    输出

    Dinosaur hall is opened.

    The People Zoo is opened.

    Before method...

    Charge Jack $1.00 for ticket.

    Jack needs to be charged first.

    Dianosaur hall charges Jack $2.00

    Jack visited diano hall.

    After return...

    Current traffic: 1

    Income of DINOSAUR: ¥2.00

    Dinosaur hall is closed.

    The People Zoo is closed.

    跟上面输出一致。

  5. 总结

    明显看到这个实现简单明了多了。expression是个非常强大的东东,有了它,就可以避免在配置里写一堆的pointcutadviceadvisor等。

     

    本文源码:

    https://github.com/EdisonXu/POC/tree/master/intro-aop

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 XML 方式实现的简单 Spring AOP 示例: 首先,需要定义一个切面类和一个目标类。假设切面类是 LoggingAspect,目标类是 HelloWorld。 LoggingAspect.java: ```java public class LoggingAspect { public void beforeAdvice() { System.out.println("Going to setup student profile."); } public void afterAdvice() { System.out.println("Student profile has been setup."); } } ``` HelloWorld.java: ```java public class HelloWorld { private String message; public void setMessage(String message) { this.message = message; } public void getMessage() { System.out.println("Your Message : " + message); } } ``` 然后,在 Spring 配置文件中定义切面和目标对象,并将它们织入到一起。在此示例中,我们使用 XML 配置 AOP。 applicationContext.xml: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="helloWorld" class="com.example.HelloWorld"> <property name="message" value="Hello World!" /> </bean> <bean id="loggingAspect" class="com.example.LoggingAspect" /> <aop:config> <aop:aspect id="aspect" ref="loggingAspect"> <aop:pointcut id="allMethods" expression="execution(* com.example.HelloWorld.*(..))" /> <aop:before pointcut-ref="allMethods" method="beforeAdvice" /> <aop:after pointcut-ref="allMethods" method="afterAdvice" /> </aop:aspect> </aop:config> </beans> ``` 在此配置文件中,我们定义了一个名为 helloWorld 的 bean(即目标对象)和一个名为 loggingAspect 的 bean(即切面)。然后,我们使用 aop:config 元素定义了一个切面,其中包含一个 pointcut(即 allMethods),它定义了要拦截的方法。我们还使用 aop:before 和 aop:after 元素定义了要在目标方法之前和之后执行的通知(即 beforeAdvice 和 afterAdvice 方法)。 最后,在应用程序中加载 Spring 配置文件,并使用它来获取 HelloWorld bean 并调用 getMessage 方法: ```java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); } } ``` 输出将如下所示: ``` Going to setup student profile. Your Message : Hello World! Student profile has been setup. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值