Spring-aop lesson 1

1.首先要写个接口,把你想要做为切面的方法声明好.

 

package com.bjsxt.spring;

public interface UserManager {
	
	public void addUser(String name, String password);
	
	public void delUser(int id);
	
	public void modifyUser(int id, String name, String 
password);

}

 

2. 在写个类实现上面的接口,想做什么就在里面搞就好了.

 

package com.bjsxt.spring;

public class UserManagerImpl implements UserManager {
	
	
	public void addUser(String name, String password) {
		System.out.println("UserManagerImpl.addUser() -- name: " + name);
	}

	public void delUser(int id) {
		System.out.println("UserManagerImpl.delUser() -- id: " + id);
	}

	public void modifyUser(int id, String name, String password) {
		System.out.println("UserManagerImpl.modifyUser() -- id: " + id);
	}

}

 

3. 在写拦截接口.

 

package com.bjsxt.spring;

public interface MySecurityManager {
	
	public void checkSecurity();
	
}

 

4. 同样写个实现类,要在拦截的时候做什么事情?

 

package com.bjsxt.spring;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 定义切面 
 */
@Aspect
public class MySecurityManagerImpl implements MySecurityManager {

	/**
	 * 定义一个切入点,切入点的名称:allAddMethod(该方法的返回值需要是void,该
	 * 方法只是一个标识),切入点的内容是一个表达式,以描述需要横切那些对象的那些方法
	 * 的调用
	 * 
	 * 具体表达是式的用法,参考spring文档6.2.3.4章节
	 */
	@Pointcut("execution(* add*(..)) || execution(* del*(..)) || execution(* modify*(..))")
	private void allAddMethod() {}
	
	/**
	 * 定一个advice,标识在那些切入点的何处插入本方法
	 */
	@Before("allAddMethod()")
	public void checkSecurity() {
		//...
		//...
		System.out.println("进行安全检查!!");
	}
	
}

 

5.不用我说啦, 做配置咯~

 

<?xml version="1.0" encoding="UTF-8"?>

<!--
  - Application context definition for JPetStore's business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
  -->
<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="userManager" class="com.bjsxt.spring.UserManagerImpl"/>
  	
  	<bean id="mySecurityManager" class="com.bjsxt.spring.MySecurityManagerImpl"/>
  	
</beans>

 

6.大功告成? 别着急,写个TestCase啊 ~

 

package com.bjsxt.spring;

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

import junit.framework.TestCase;


public class TestAop extends TestCase {
	
	public void testAop1() {
		//读取配置文件,获取BeanFactory
		BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-beans.xml");
		UserManager userManager = (UserManager)factory.getBean("userManager");
		userManager.addUser("张三", "123");
		userManager.delUser(1);
		userManager.modifyUser(1, "李四", "abc");
	}
	
}

 7. 运行结果是,测试成功!~ 大功告成~!

 

进行安全检查!!
UserManagerImpl.addUser() -- name: 张三
进行安全检查!!
UserManagerImpl.delUser() -- id: 1
进行安全检查!!
UserManagerImpl.modifyUser() -- id: 1

 8. 所用的包有如下:

 

 

你可能感兴趣的:(spring,AOP,bean,xml,JUnit)