[color=blue]AOP术语:
* Cross cutting concern
* Aspect
* Advice
* Pointcut
* Joinpoint
* Weave
* Target Object
* Proxy
* Introduction[/color]
AOP实例:
[color=red]定义切面:[/color]
[color=red]用户接口:[/color]
[color=red]用户实现:[/color]
[color=red]客户端实现:[/color]
[color=red]
applicationContext.xml的配置:[/color]
* Cross cutting concern
* Aspect
* Advice
* Pointcut
* Joinpoint
* Weave
* Target Object
* Proxy
* Introduction[/color]
AOP实例:
[color=red]定义切面:[/color]
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)
*/
@Pointcut("execution(* add*(..)) || execution(* del*(..))")
private void allAddMethod(){};
/**
* 定义Advice,标识在那个切入点何处织入此方法
*/
@Before("allAddMethod()")
private void checkSecurity() {
System.out.println("----------checkSecurity()---------------");
}
}
[color=red]用户接口:[/color]
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);
}
[color=red]用户实现:[/color]
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()----------");
}
// private void checkSecurity() {
// System.out.println("----------checkSecurity()---------------");
// }
}
[color=red]客户端实现:[/color]
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);
}
}
[color=red]
applicationContext.xml的配置:[/color]
<?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>