spring AOP annotation方式(静态配置)

 首先加入spring_AOP必备的4个jar包:
 spring.jar
 aspectjrt.jar
 aspectweave.jar
 Commons-logging.jar
1.Aspect类:
package annotationSecurityHandler;
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*(..))")
 private void allAddMethod(){}
 
 /**
  * 定义advice,标示在哪个切入点,何处织入此方法before/after
  */
 @Before("allAddMethod()")
 private void checkSecurity(){
   System.out.println("========checkSecurity============");
 }
}
2.接口
package manager;

public interface UserManager {

  public void addUser(String userName,String password);
  
  public void deleteUserById(int id);
  
  public String findUserById(int id);
  
  public void modifyUser(int id,String userName,String password);
}
3.实现类:
package managerImpl;

import manager.UserManager;

public class UserManagerImpl implements UserManager {

 public void addUser(String userName, String password) {
  System.out.println("===UserManagerImpl==addUser==");
 }

 public void deleteUserById(int id) {
  System.out.println("===UserManagerImpl==deleteUserById==");
 }

 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==");
 }

}
4.测试:
package test;

import manager.UserManager;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
  UserManager userManager = (UserManager) beanFactory.getBean("userManager");
  userManager.addUser("", "");
  userManager.deleteUserById(1);
 }

}
6.applicationContext.xml中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: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="annotationSecurityHandler.SecurityHandler" >
  </bean>
 <bean id="userManager"
  class="managerImpl.UserManagerImpl" >
 </bean>
 </beans>

 

你可能感兴趣的:(spring,AOP,String,Class,interface,encoding)