码迷,mamicode.com
首页 > 编程语言 > 详细

Spring AOP编程实例

时间:2015-05-14 12:22:05      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:

整个类包如下:

技术分享

一、具体各个类

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


Spring AOP编程实例

标签:

原文地址:http://my.oschina.net/u/2308739/blog/414702

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!