首页 > 代码库 > spring aop例子

spring aop例子

 

package aoptest;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;public class AopLog {    // 方法执行的前后调用    public Object runOnAround(ProceedingJoinPoint point) throws Throwable {        System.out.println("begin around");        Object object = point.proceed();        //System.out.println(point.getTarget());        System.out.println("end around");        return object;    }}

 

/** *  */package aoptest;/** * @author Administrator * */public class AopOperation {        private int num;    public int getNum() {        return num;    }    public void setNum(int num) {        this.num = num;    }        public void queryOpLog(){        System.out.println(123);    }}

 

/* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package aoptest;import java.io.Serializable;import org.springframework.beans.factory.annotation.Configurable;@Configurable("configuredBean")@SuppressWarnings("serial")public class ShouldBeConfiguredBySpring implements Serializable {    private String name;    public void setName(String name) {        this.name = name;    }    public String getName() {        System.out.println(this.name);        return this.name;    }    }

 

package aoptest;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import aoptest.ShouldBeConfiguredBySpring;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.ContextLoader;/** * @author Administrator * *///@ContextConfiguration("meta/springConfigured.xml")public class TestAop {    @Test    public void aopTest(){        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("meta/springConfigured.xml");        ShouldBeConfiguredBySpring myObject = (ShouldBeConfiguredBySpring) context.getBean("configuredBean");        System.out.println(myObject.getName()+",wj");        //AopOperation aopOp = new AopOperation();        //aopOp.queryOpLog();    }}

 

<?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:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">    <context:spring-configured />    <aop:aspectj-autoproxy />    <bean id="aopLog" class="aoptest.AopLog" />    <aop:config>        <aop:aspect ref="aopLog">            <aop:around method="runOnAround"                pointcut="execution (* aoptest.ShouldBeConfiguredBySpring.getName(..))" />        </aop:aspect>    </aop:config>    <bean id="configuredBean" class="aoptest.ShouldBeConfiguredBySpring"        lazy-init="true">        <property name="name" value="Rod" />    </bean></beans>

 

pom.xml文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>commonproject</groupId>    <artifactId>common</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.1.1.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aspects</artifactId>            <version>4.1.1.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>4.1.1.RELEASE</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.10</version>        </dependency>    </dependencies></project>

 

控制台输出结果如下:

十月 19, 2014 8:33:57 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15f550a: startup date [Sun Oct 19 20:33:57 CST 2014]; root of context hierarchy
十月 19, 2014 8:33:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [meta/springConfigured.xml]
begin around
Rod
end around
Rod,wj

spring aop例子