spring-AOP实现方式(3----AspectJ的AOP实现)

[size=medium][b][color=blue]AspectJ的AOP实现[/color][/b][/size]

[b][color=brown]配置文件中的配置[/color][/b]
[b][color=indigo]1、增加对注解名空间和schema的支持[/color][/b]
[color=olive]xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd[/color]

[b][color=indigo]2、增加AOP的XML名空间和schema的支持[/color][/b]
[color=olive]xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd[/color]

[b][color=indigo]3、增加自动扫描注解和使用AspectJ自动代理支持[/color][/b]
[color=olive]<context:annotation-config/>
<context:component-scan base-package="com.hank"/>

<aop:aspectj-autoproxy/>[/color]

[b][color=brown]java代码方面[/color][/b]
[b][color=indigo]4、编写切面类[/color][/b]
[color=olive]@Aspect //声明为切面
@Component //声明为组件
public class UserAspectJProxy {
@Before("execution(public void com.hank.dao.impl.UserDaoImpl.*User())")
public void before() {
System.out.println("------before execute method------");
}

@AfterReturning("execution(public void com.hank.dao.impl.UserDaoImpl.*User())")
public void after() {
System.out.println("------after execute method------");
}
}[/color]

[size=medium][b][color=blue]具体实现如下[/color][/b][/size]
[img]http://dl2.iteye.com/upload/attachment/0094/1202/7437844a-2270-39cb-9268-9aac09031aed.jpg[/img]

[b][color=brown]具体代码:[/color][/b]
[b][color=indigo]配置文件[/color][/b]
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<context:annotation-config/>
<context:component-scan base-package="com.hank"/>

<aop:aspectj-autoproxy/>

<!-- 1.定义具体被代理类 -->
<bean id="userDaoImpl" class="com.hank.dao.impl.UserDaoImpl"/>
</beans>

[b][color=indigo]被代理的具体的类:[/color][/b]
package com.hank.dao.impl;
import com.hank.dao.UserDao;

public class UserDaoImpl implements UserDao{
public void saveUser() {
System.out.println("保存用户信息。。。。。。");
}

public void queryUser() {
System.out.println("查看用户信息。。。。。。");
}
}


[b][color=indigo]被代理类实现的接口类[/color][/b]
package com.hank.dao;

public interface UserDao {
//保存用户信息
public void saveUser();
//查看用户信息
public void queryUser();
}


[b][color=indigo]代理类,拦截处理类[/color][/b]
package com.hank.proxy;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect //声明为注解
@Component //声明为组件
public class UserDaoAspectJProxy {

@Before("execution(public void com.hank.dao.impl.UserDaoImpl.*User())")
public void before() {
System.out.println("------before execute method-----------");
}

@AfterReturning("execution(public void com.hank.dao.impl.UserDaoImpl.*User())")
public void after() {
System.out.println("------after execute method-----------");
}
}

[b][color=indigo]测试类[/color][/b]
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hank.dao.UserDao;
import junit.framework.TestCase;

public class TestAopByAspectJProxy extends TestCase{
public void test1() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao)ctx.getBean("userDaoImpl");
userDao.saveUser();
}
}



[b][color=indigo]输出结果[/color][/b]
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
------before execute method-----------
保存用户信息。。。。。。
------after execute method-----------


[b][color=brown]注:代理类,拦截类,可以写成如下形式,更加简洁[/color][/b]
package com.hank.proxy;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect //声明为注解
@Component //声明为组件
public class UserDaoAspectJProxy {

@Pointcut("execution(public void com.hank.dao.impl.UserDaoImpl.*User())")
public void doFilter() {

}

@Before("doFilter()")
public void before() {
System.out.println("------before execute method-----------");
}

@AfterReturning("doFilter()")
public void after() {
System.out.println("------after execute method-----------");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值