Spring AOP

本文详细介绍了在Spring框架中如何使用AOP(面向切面编程),包括通过注解和XML配置两种方式实现AOP的具体步骤。从配置文件的编写到AOP切面的定义,再到测试代码的实现,全面展示了AOP在实际项目中的应用。

AOP实现方式:1.annotation方式,2.xml方式

 

方式一:annotation

 

1.配置xml文件(spring-annotation.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd 
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd 
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <context:annotation-config></context:annotation-config>

    <context:component-scan base-package="com.fq.*"></context:component-scan>

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <bean id="test" class="com.fq.server.serverImpl.TestServerImpl"></bean>

</beans>

 

2.设置TestServerImpl.java和TestServer.java

package com.fq.server;

public interface TestServer {

    void sayHello();
}
package com.fq.server.serverImpl;

import com.fq.server.TestServer;

public class TestServerImpl implements TestServer {

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("hello!");
    }

}

 

3. AOP实例(AOPTest.java)

package com.fq.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AopTest {

    //commonPointCut()目的只是代表execution表达式,方便使用。
    @Pointcut("execution(public void com.fq.server.serverImpl.TestServerImpl.sayHello())")
    public void commonPointCut(){}

    //写法一
    @Before(value = "execution(public void com.fq.server.serverImpl.TestServerImpl.sayHello())")
    public void doBefore(){
        System.out.println("method before ");
    }
    //写法二
    @AfterReturning(value="commonPointCut()")
    public void doAfter(){
        System.out.println("method after");
    }
    @Around(value="commonPointCut()")
    public void doAround(ProceedingJoinPoint joinPoint) throws Throwable{

        System.out.println("menthod before !");
        //必须有
        Object relval = joinPoint.proceed();

        System.out.println("method after !");
    }

}

4.编写测试代码(Test.java)

package com.fq.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fq.server.TestServer;

public class Test {
    public static void main(String[] args) {

        ApplicationContext context =new ClassPathXmlApplicationContext("spring/spring-annotation.xml");

        TestServer test =(TestServer) context.getBean("test");

        test.sayHello();

    }
}

5.运行Test查看结果

 

方式二:xml配置

使用annotation方式中的TestServer.java , TestServerImpl.java

1.xml配置文件(spring-xml.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd 
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd 
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <!-- 定义的切面 -->
    <bean id="aopTestXml" class="com.fq.aop.AopTestXml"></bean>

    <aop:config>

        <!--此处定义的pointcut具有全局作用 -->
        <aop:pointcut
            expression="execution(public void com.fq.server.serverImpl.TestServerImpl.sayHello())"
            id="commonPointCut" />

        <aop:aspect id="aopName" ref="aopTestXml">
            <!-- 此处pointcut具有局部作用,只在切名叫aopName下有效 -->
            <aop:pointcut
                expression="execution(public void com.fq.server.serverImpl.TestServerImpl.sayHello())"
                id="myPointCut" />

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

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

            <aop:around method="doAround"
                pointcut="execution(public void com.fq.server.serverImpl.TestServerImpl.sayHello())" />

        </aop:aspect>

    </aop:config>
    <!-- 对象bean -->
    <bean id="testXml" class="com.fq.server.serverImpl.TestServerImpl"></bean>
</beans>

2.xml文件中定义的AopTestXml.java

package com.fq.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class AopTestXml {

    public void doBefore(){
        System.out.println("method before ");
    }
    public void doAfter(){
        System.out.println("method after");
    }
    public void doAround(ProceedingJoinPoint joinPoint) throws Throwable{

        System.out.println("menthod before !");
        //必须有
        Object relval = joinPoint.proceed();

        System.out.println("method after !");
    }

}

3.测试类TestXml.java

package com.fq.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fq.server.TestServer;

public class TestXml {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-xml.xml");

        TestServer testXml = (TestServer) context.getBean("testXml");

        testXml.sayHello();
    }
}

参考来源:

http://blog.youkuaiyun.com/voyage_mh1987/article/details/5821238

http://blog.youkuaiyun.com/lemonyfei/article/details/8664773

转载于:https://www.cnblogs.com/haoke/p/4374386.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值