AspectJ的AOP实现
配置文件中的配置
1、增加对注解名空间和schema的支持
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
2、增加AOP的XML名空间和schema的支持
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
3、增加自动扫描注解和使用AspectJ自动代理支持
<context:annotation-config/>
<context:component-scan base-package="com.hank"/>
<aop:aspectj-autoproxy/>
java代码方面
4、编写切面类
@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------");
}
}
具体实现如下
具体代码:
配置文件
<?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>
被代理的具体的类:
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("查看用户信息。。。。。。");
}
}
被代理类实现的接口类
package com.hank.dao;
public interface UserDao {
//保存用户信息
public void saveUser();
//查看用户信息
public void queryUser();
}
代理类,拦截处理类
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-----------");
}
}
测试类
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();
}
}
输出结果
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-----------
注:代理类,拦截类,可以写成如下形式,更加简洁
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-----------");
}
}