AOP用法实践

一、aop的配置

<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
		http://www.springframework.org/schema/jee 
		http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.1.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
            
	<bean id="aopExampleImpl1" class="com.terisadeng.spring.aop.AopExampleImpl1"></bean>  
	<bean id="aopExampleImpl2" class="com.terisadeng.spring.aop.AopExampleImpl2"></bean>   
	<bean id="timeLogger" class="com.terisadeng.spring.aop.TimeLogger"></bean>   
	
	<aop:config proxy-target-class="true">
		<aop:aspect id="time" ref="timeLogger">
			<aop:pointcut id="AllMethod" expression="execution(* com.terisadeng.spring.aop.AopExample.*(..))"></aop:pointcut>
			<aop:before method="costTime" pointcut-ref="AllMethod"></aop:before>
			<aop:after method="costTime" pointcut-ref="AllMethod"></aop:after>
		</aop:aspect>
		
	</aop:config>    
</beans>

二、创建一个目标接口及两个实现类

package com.terisadeng.spring.aop;

public interface AopExample
{
    void enterAopExample();
    void outAopExample();
}

package com.terisadeng.spring.aop;

public class AopExampleImpl1 implements AopExample
{

		//该方法作为切入点
    @Override
    public void enterAopExample()
    {
        System.out.println("Enter AopExampleImpl1.enterAopExample()");
    }

		//该方法作为切入点
    @Override
    public void outAopExample()
    {
        System.out.println("Enter AopExampleImpl1.outAopExample()");
    }

}

package com.terisadeng.spring.aop;

public class AopExampleImpl2 implements AopExample
{

		//该方法作为切入点
    @Override
    public void enterAopExample()
    {
        System.out.println("Enter AopExampleImpl2.enterAopExample()");
    }

		//该方法作为切入点
    @Override
    public void outAopExample()
    {
        System.out.println("Enter AopExampleImpl2.outAopExample()");
    }

}
该类是拦截到方法后进行的处理

package com.terisadeng.spring.aop;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeLogger
{
    public void costTime(){
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S");
        System.out.println("CurrentTime = " +sdf.format(date));
    }
}

三、测试方法:
1、使用Junit进行测试

package com.terisadeng.spring.aop;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:aop.xml")
public class TestAop
{
    @Resource
    private AopExampleImpl1 aopExampleImpl1;
    
    @Resource
    private AopExampleImpl2 aopExampleImpl2;
    @Test
    public void test(){
        aopExampleImpl1.enterAopExample();
        System.out.println();
        aopExampleImpl1.outAopExample();
        System.out.println();
        aopExampleImpl2.enterAopExample();
        System.out.println();
        aopExampleImpl2.outAopExample();
    }
    
}
2、使用慢main函数进行测试

package com.terisadeng.spring.aop;

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

public class AopDemo
{
    public static void main(String[] args)
    {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("aop.xml");
        AopExampleImpl1 aopExampleImpl1=(AopExampleImpl1)ctx.getBean("aopExampleImpl1");
        AopExampleImpl2 aopExampleImpl2=(AopExampleImpl2)ctx.getBean("aopExampleImpl2");
        
        aopExampleImpl1.enterAopExample();
        System.out.println();
        aopExampleImpl1.outAopExample();
        System.out.println();
        aopExampleImpl2.enterAopExample();
        System.out.println();
        aopExampleImpl2.outAopExample();
    }
}

一开始报错com.sun.proxy.$Proxy2 cannot be cast to com.terisadeng.spring.aop.AopExampleImpl1
表示动态代理生成的类无法转换为自定义的实现类
需要在aop:config标签中增加属性:proxy-target-class="true"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值