spring aop 的四种执行方式此为第一种
spring对AOP的只是(采用Annotation的方式)
1、spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/aspectj/*.jar
2、采用Aspect定义切面
2、在Aspect定义Pointcut和Advice
4、启用AspectJ对Annotation的支持并且将Aspect类和目标对象配置到Ioc容器中
注意:在这种方法定义中,切入点的方法是不被执行的,它存在的目的仅仅是为了重用切入点
即Advice中通过方法名引用这个切人点
AOP:
* Cross cutting concern
* Aspect
* Advice
* Pointcut
* Joinpoint
* Weave
* Target Object
* Proxy
* Introduction
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager)factory.getBean("userManager");
userManager.addUser("张三", "123");
userManager.deleteUser(1);
}
}
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 定义Aspect
* @author Administrator
*
*/
@Aspect
public class SecurityHandler {
/**
* 定义Pointcut,Pointcut的名称就是allAddMethod,此方法不能有返回值和参数,该方法只是一个
* 标识
*
* Pointcut的内容是一个表达式,描述那些对象的那些方法(订阅Joinpoint)
*/
//第一个*表示返回值或不返回值 add*表示缀为add的所有方法,(..)表示有参数或无参数
@Pointcut("execution(* add*(..)) || execution(* del*(..))")
private void allAddMethod(){};
/**
* 定义Advice,标识在那个切入点何处织入此方法
*/
@Before("allAddMethod()")
private void checkSecurity() {
System.out.println("----------checkSecurity()---------------");
}
}
public interface UserManager {
public void addUser(String username, String password);
public void deleteUser(int id);
public void modifyUser(int id, String username, String password);
public String findUserById(int id);
}
public class UserManagerImpl implements UserManager {
public void addUser(String username, String password) {
System.out.println("-------UserManagerImpl.addUser()----------");
}
public void deleteUser(int id) {
System.out.println("-------UserManagerImpl.deleteUser()----------");
}
public String findUserById(int id) {
System.out.println("-------UserManagerImpl.findUserById()----------");
return null;
}
public void modifyUser(int id, String username, String password) {
System.out.println("-------UserManagerImpl.modifyUser()----------");
}
}
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<aop:aspectj-autoproxy/>
<bean id="securityHandler" class="com.bjsxt.spring.SecurityHandler"/>
<bean id="userManager" class="com.bjsxt.spring.UserManagerImpl"/>
</beans>