转载自:http://jushi1988.iteye.com/blog/689555
以前做的Spring AOP的小例子,今天拿来看了一下,可以作为初学者学习的一个简单demo。
做的很简单,大致是用到了AOP的注解方式。个人感觉Spring AOP真的很不错。可以做日志处理等。
先贴代码:
1. 接口 IUser.java
- package com.jushi.aop;
- public interface IUser {
- public String findUser(String username);
- public void addUser(String username);
- public void findAll();
- }
2. 接口实现 IUserImpl.java
- package com.jushi.aop;
- import java.util.HashMap;
- import java.util.Map;
- public class IUserImpl implements IUser {
- public static Map map = null;
- public static void init(){
- String[] list = {"Lucy", "Tom", "小明", "Smith", "Hello"};
- Map tmp = new HashMap();
- for(int i=0; i<list.length; i++){
- tmp.put(list[i], list[i]+"00");
- }
- map = tmp;
- }
- public void addUser(String username) {
- init();
- map.put(username, username+"11");
- System.out.println("--------------【addUser】: "+username+" --------------");
- System.out.println("【The new List:"+map+"】");
- }
- public void findAll() {
- init();
- System.out.println("---------------【findAll】: "+map+" ------------------");
- }
- public String findUser(String username) {
- init();
- String password = "没查到该用户";
- if(map.containsKey(username)){
- password = map.get(username).toString();
- }
- System.out.println("-----------------【findUser】-----------------");
- System.out.println("-----------------Username:"+username+"-----------------");
- System.out.println("-----------------【Result】:"+password+"------------------");
- return password;
- }
- }
3. 拦截类 CheckUser.java
- package com.jushi.aop;
- import java.util.Date;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- @Aspect
- public class CheckUser {
- @Pointcut("execution(* com.jushi.aop.*.find*(..))")
- public void checkUser(){
- System.out.println("**************The System is Searching Information For You****************");
- }
- @Pointcut("execution(* com.jushi.aop.*.add*(..))")
- public void checkAdd(){
- System.out.println("**************<< Add User >> Checking.....***************");
- }
- @Before("checkUser()")
- public void beforeCheck(){
- System.out.println(">>>>>>>> 准备搜查用户..........");
- }
- @After("checkUser()")
- public void afterCheck(){
- System.out.println(">>>>>>>> 搜查用户完毕..........");
- }
- @Before("checkAdd()")
- public void beforeAdd(){
- System.out.println(">>>>>>>> 增加用户--检查ing..........");
- }
- @After("checkAdd()")
- public void afterAdd(){
- System.out.println(">>>>>>>> 增加用户--检查完毕!未发现异常!..........");
- }
- }
4. 实体pojo User.java
- package com.jushi.entity;
- public class User {
- private String username;
- private String password;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
5. 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.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <aop:aspectj-autoproxy/>
- <bean id="user" class="com.jushi.aop.IUserImpl"/>
- <bean id="check" class="com.jushi.aop.CheckUser"/>
- <!-- <aop:config>
- <aop:pointcut id="find" expression="execution(* com.jushi.aop.*.find*(..))" />
- <aop:pointcut id="add" expression="execution(* com.jushi.aop.*.add*(..))" />
- <aop:aspect id="checkUser" ref="check">
- <aop:before method="beforeCheck" pointcut-ref="find"/>
- <aop:after method="afterCheck" pointcut-ref="find"/>
- </aop:aspect>
- <aop:aspect id="checkAdd" ref="check">
- <aop:before method="beforeAdd" pointcut-ref="add"/>
- <aop:after method="afterAdd" pointcut-ref="add"/>
- </aop:aspect>
- </aop:config>-->
- </beans>
好了,一个简单AOP demo就配置好了,测试一下
结果如下:
- >>>>>>>> 增加用户--检查ing..........
- --------------【addUser】: haha --------------
- 【The new List:{Lucy=Lucy00, Tom=Tom00, haha=haha11, Smith=Smith00, Hello=Hello00,
- 小明=小明00}】
- >>>>>>>> 增加用户--检查完毕!未发现异常!..........
例子很小,仅供参考...