Spring AOP编程实例

整个类包如下:

一、具体各个类

1.1前置通知类

package com.yuan.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MymethodBeforeAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2)
			throws Throwable {
		// TODO Auto-generated method stub

	}

}

1.2 代理对象

package com.yuan.service;

public interface UserService {
  public Integer add(String abc);
  public void aduser(String abc);
}

1.3 代理对象实现

package com.yuan.service.impl;

import org.springframework.stereotype.Service;

import com.yuan.service.UserService;
@Service
public class UserServiceimpl implements UserService {
	@Override
	public Integer add(String abc) {
		System.out.println("添加用户信息1。"+abc);
		return 123;
	}

	@Override
	public void aduser(String abc) {
		// TODO Auto-generated method stub
		System.out.println("添加用户信息2。");

	}

}

1.4切面类

package com.yuan.aop;
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 Log {
	/***
	 * 配置切入点,该方法无方法体
	 * 
	 */
	@Pointcut("execution(* com.yuan.service..ad*(..))")
    public void daddff(){};
    /***
     * 配置前置通知
     * @param joinpoint
     */
    @Before("daddff()")
    public void before(){
    	System.out.println("添加日志");
    
    }

}

1.5 配置文件bean.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

  <context:component-scan base-package="com" /> 
  <aop:aspectj-autoproxy/>
       
</beans>

1.6测试类

package com.yuan.test;

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

import com.yuan.service.UserService;

public class AopTest {
   
 public static void main(String[] args){
	 ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
	 UserService userService=(UserService)ac.getBean("userServiceimpl");
	 userService.add("123oscar");
	 //userService.aduser("456");
 }
}

测试结果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

添加日志

添加用户信息1。123oscar

时间: 2024-07-31 14:28:11

Spring AOP编程实例的相关文章

Spring aop 小实例demo

Hadoop从2.4.0版本开始支持hdfs的ACL,在CDH5.0当中也集成了该特性,下面对其进行一些测试: unnamed user (file owner) 文件的拥有者 unnamed group (file group) 文件的所属组 named user 除了文件的拥有者和拥有组之外,的其它用户 named group 除了文件的拥有者和拥有组之外,的其它用户 mask  权限掩码,用于过滤named user和named group的权限 一.启用ACL: <property>

Spring AOP应用实例demo

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

Spring AOP通知实例 – Advice

Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后添加额外的功能. 在Spring AOP中,有 4 种类型通知(advices)的支持: 通知(Advice)之前 - 该方法执行前运行 通知(Advice)返回之后 – 运行后,该方法返回一个结果 通知(Advice)抛出之后 – 运行方法抛出异常后, 环绕通知 – 环绕方法执行运行,结合以上这三

(转)Spring AOP编程原理、Demo

转自: http://pandonix.iteye.com/blog/336873/ Spring AOP 详解 此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题.最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP 来解决.一方面是为了以更加灵活的方式来解决问题,另一方面是借此机会深入学习Spring AOP相关的内容.本文是权当本人的自己AOP学习笔记,以下需求不用AOP肯定也能解决,至于是否牵强附会,仁者见仁智者见智. 对部分函数的调用进行日志记录

spring AOP 编程--AspectJ注解方式 (4)

1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充. AOP 的主要编程对象是切面(aspect), 而切面模块化横切关注点. 在应用 AOP 编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里. AOP 的好处:

spring aop编程

1.AOP,面向切面编程(aspect Oriental programing),使用aop,可以将处理切面aspect的代码注入到主程序,通常主程序的主要目的不是处理这些切面aspect,可以防止代码混乱.拦截机 interceptor是AOP的另一中叫法.(其中使用的模式为代理模式,动态代理模式). 2.通知advise:在aspect的某个连接点(在Spring的aop中,一个连接点就是一个方法的执行)上执行的动作. 切入点pointcut:匹配连接点的断言.通知和一个切入点表达式关联,并

[Spring] AOP, Aspect实例解析

最近要用到切面来统一处理日志记录,写了个小实例练了练手: 具体实现类: public interface PersonServer { public void save(String name); public void update(String name, Integer id); public String getPersonName(Integer id); } import org.springframework.stereotype.Component; @Component("pe

Spring Aop编程的demo

1: 新建一个普通的bean :Role 属性 Id,name,添加无参构造,setter getter方法 2:新建一个接口:RoleService,随便写一个方法printRole 3:新建一个类RoleServiceImpl,实现RoleService接口,重写printRole方法 注意 @Component 注解别忘了 4:定义切面类 RoleAspect 添加四个通知方法 注意: 添加@Aspect注解 execution中的参数一定要写对,例:"execution(* aop.se

AOP编程实例:拦截器拦截拦截某个实现类的方法

第一步:配置实现MethodInterceptor的切面 java代码 1 public class OutsideInvokeLogInterceptor implements MethodInterceptor{ 2 private static Logger log = Logger.getLogger("outsideInvoke"); 3 4 @Override 5 public Object invoke(MethodInvocation invocation) throw