Spring 4.0 学习日记(9) ---XML配置实现AOP切面

来源:互联网 发布:java随机数生成 编辑:程序博客网 时间:2024/06/07 06:42

Spring创建代理的规则

1.默认使用Java动态代理来创建AOP代理
2.当需要代理的类不是代理接口的时候,Spring会切换为使用CGLIB代理,也可强制使用CGLIB

其实Xml配置更简单
直接看代码就懂了

接口类

package com.wow.AopMessageInstance;public interface HelloWorld {       void printHelloWorld();       void doPrint();       String getReturn();}

实现类1

package com.wow.AopMessageInstance;public class HelloWorldImpl implements HelloWorld {    @Override    public void printHelloWorld() {        System.out.println("Enter HelloWorldImpl.printHelloWorld()");    }    @Override    public void doPrint() {        System.out.println("Enter HelloWorldImpl.doPrint()");    }    @Override    public String getReturn() {        String str = "HelloWorldImpl" ;        System.out.println("Enter HelloWorldImpl.getReturn()");        return str;    }}

实现类2

package com.wow.AopMessageInstance;public class HelloWorldImplAnother implements HelloWorld {    @Override    public void printHelloWorld() {        System.out.println("Enter HelloWorldImplAnother.printHelloWorld()");    }    @Override    public void doPrint() {        System.out.println("Enter HelloWorldImplAnother.doPrint()");    }    @Override    public String getReturn() {        String str = "HelloWorldImpl" ;        System.out.println("Enter HelloWorldImplAnother.getReturn()");        return str;    }}

Aop切面

package com.wow.AopMessageInstance;public class HelloWorldAop {     public void printTime()        {            System.out.println("CurrentTime = " + System.currentTimeMillis());        }     public void  getReturn(String obj)        {            System.out.println("ReturnValue = " + obj);        }}

测试类

package com.wow.AopMessageInstance;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloWorldTest {    public static void main(String[] args) {        ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");        HelloWorld hw = (HelloWorld) app.getBean("helloWorldImpl");        HelloWorld hwa = (HelloWorld) app.getBean("helloWorldImplAnother");        hw.printHelloWorld();        hw.doPrint();        hw.getReturn();        System.out.println("----");        hwa.printHelloWorld();        hwa.doPrint();        hwa.getReturn();    }}

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:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">    <bean id = "helloWorldImpl" class = "com.wow.AopMessageInstance.HelloWorldImpl"></bean>    <bean id = "helloWorldImplAnother" class = "com.wow.AopMessageInstance.HelloWorldImplAnother"></bean>    <bean id = "helloWorldAop" class = "com.wow.AopMessageInstance.HelloWorldAop"></bean>           <!-- 如果只想织入接口中的某些方法 只用修改expresion的匹配方式就好了-->    <aop:config>        <aop:aspect id = "aop" ref = "helloWorldAop">             <aop:pointcut id="pointCut" expression="execution(* com.wow.AopMessageInstance.*.*(..))" />             <aop:before method="printTime" pointcut-ref="pointCut" />             <aop:after method="printTime" pointcut-ref="pointCut" />             <aop:after-returning method="getReturn" pointcut-ref="pointCut" returning="obj"/>                     </aop:aspect>    <!-- 如果需要有多个切面 只要在这里再写一个aop:aspect属性就好了  对于多个切面的前后顺序 可以用到order属性 -->    <!--    <aop:aspect id="time" ref="timeHandler" order="1">                <aop:pointcut id="addTime" expression="execution(* com.xrq.aop.HelloWorld.print*(..))" />                <aop:before method="printTime" pointcut-ref="addTime" />                <aop:after method="printTime" pointcut-ref="addTime" />            </aop:aspect>            <aop:aspect id="log" ref="logHandler" order="2">                <aop:pointcut id="printLog" expression="execution(* com.xrq.aop.HelloWorld.do*(..))" />                <aop:before method="LogBefore" pointcut-ref="printLog" />                <aop:after method="LogAfter" pointcut-ref="printLog" />            </aop:aspect> -->    </aop:config></beans>  

打印信息

八月 02, 2017 11:32:25 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@142b7711: startup date [Wed Aug 02 23:32:25 CST 2017]; root of context hierarchy八月 02, 2017 11:32:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [beans.xml]CurrentTime = 1501687946276Enter HelloWorldImpl.printHelloWorld()CurrentTime = 1501687946276CurrentTime = 1501687946277Enter HelloWorldImpl.doPrint()CurrentTime = 1501687946277CurrentTime = 1501687946277Enter HelloWorldImpl.getReturn()CurrentTime = 1501687946277ReturnValue = HelloWorldImpl----CurrentTime = 1501687946277Enter HelloWorldImplAnother.printHelloWorld()CurrentTime = 1501687946277CurrentTime = 1501687946278Enter HelloWorldImplAnother.doPrint()CurrentTime = 1501687946278CurrentTime = 1501687946278CurrentTime = 1501687946278ReturnValue = HelloWorldImplAnother
阅读全文
0 0
原创粉丝点击