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[email protected]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

时间: 2024-08-27 13:19:59

spring aop例子的相关文章

spring AOP + 自定义注解实现权限控制小例子

今天看了一下黑马程序员的视频,上面讲到一个使用spring AOP + 自定义注解的方式来实现权限控制的一个小例子,个人觉得还是可以借鉴,整理出来与大家分享. 需求:service层有一些方法,这些方法需要不同的权限才能访问. 实现方案:自定义一个PrivilegeInfo的注解,使用这个注解为service层中的方法进行权限配置,在aop中根据PrivilegeInfo注解的值,判断用户是否拥有访问目标方法的权限,有则访问目标方法,没有则给出提示. 关键技术:自定义注解及注解解析,spring

用spring aop实现动态代理的例子

下面由我来给大家展示用spring aop实现动态代理的例子(电脑打印) 下面就看一下具体的代码: 先定义一个打印机的接口 1 package aop007_ComPrint; 2 3 public interface Print { 4 5 public void ColorPrint(); //彩色打印 6 public void WhitePrint(); //黑白打印 7 } 然后定义两个实现类,分别实现彩色打印和黑白打印 1 package aop007_ComPrint; 2 3 p

峰Spring4学习(6)spring AOP的应用例子

一.AOP简介: 二.AOP实例: 三.使用的例子 需求:在student添加的前后,打印日志信息: 0)spring AOP需要引用的jar包: 1)StudentService.java接口: package com.cy.service; public interface StudentService { public void addStudent(String name); } 2)StudentServiceImpl.java实现类: package com.cy.service.i

Spring AOP 学习例子

http://outofmemory.cn/code-snippet/3762/Spring-AOP-learn-example 工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大说过:“开发人员不可能一天到晚只有工作,肯定是需要自我学习.第一:为了更充实自己,保持进步状态.第二:为了提升技术,提高开发能力.第三:保持程序员对技术和学习的热情,工作的激情.程序员还是需要把基础打扎实,修炼自己的内功.” 所以赶紧把学习的东西总结一下,加深印象.之前有说了下AOP的原理 (http://

spring aop 原理

http://blog.csdn.net/moreevan/article/details/11977115 Spring AOP 实现原理 2013-09-24 15:23 79554人阅读 评论(11) 收藏 举报  分类: spring(2)  版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)

spring AOP和通知

1.  spring的通知 1.1.  AOP的概念 切面(Aspect):一个关注点的模块化,这个关注点可能会横切多个对象.事务管理是J2EE应用中一个关于横切关注点的很好的例子.在Spring AOP中,切面可以使用基于模式或者基于注解的方式来实现. 连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候.在Spring AOP中,一个连接点总是表示一个方法的执行. 通知(Advice):在切面的某个特定的连接点上执行的动作.其中包括了"aroun

JavaEE学习之Spring aop

一.基本概念 AOP——Aspect-Oriented Programming,面向切面编程,它是spring框架的一个重要组成部分.一般的业务逻辑都有先后关系,我们可以理解为纵向关系,而AOP关注的是横向关系,每一个关注点可以理解为一个横切面.例如我们的大部分代码都会涉及到日志记录,很多的数据库操作都会涉及到事务的创建和提交.那么从横向关注这些逻辑,他们都一个个的切面. AOP技术的具体实现,可以通过动态代理技术或者是在程序编译期间进行静态的"织入"方式.AOP经常使用的场景包括:日

Spring AOP的实现原理

原文出处: Listen AOP(Aspect Orient Programming),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统中分布于各个模块的横切关注点,比如事务管理.日志.缓存等等.AOP实现的关键在于AOP框架自动创建的AOP代理,AOP代理主要分为静态代理和动态代理,静态代理的代表为AspectJ:而动态代理则以Spring AOP为代表.本文会分别对AspectJ和Spring AOP的实现进行分析和介绍. 使用AspectJ的编译时增强实现AOP 之前

Spring AOP小结

一. AOP介绍 AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入封装.继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合.当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力.也就是说,OOP允许你定义从上到下的关系,但并不适合定义从左到右的关系.例如日志功能.日志代码往往水平地散布在所有对象层次中,而与它所散布到的对象的核心功能