Spring框架学习笔记3:AOP示例-异常日志和操作日志的记录

AOP示例

  1)异常日志的记录 (异常通知类型)
  2)操作日志的记录 (环绕通知类型)
    —-思考选用哪种类型的通知—-
    —-思考切入点表达式的写法—-

引入spring框架包jar包:
commons-logging.jar
spring.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar

工具类:

FileUtil.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.weishuzhai.util;
 
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
 
public class FileUtil {
	/**
	 * 写文件操作
	 * 
	 * @param filePath
	 *            文件路径及其名字
	 * @param append
	 *            true追加,false覆盖
	 * @param msg
	 *            消息
	 * @throws IOException
	 */
	public static void write(String filePath, boolean append, String msg)
			throws IOException {
		FileWriter out = new FileWriter(filePath, append);
		PrintWriter pw = new PrintWriter(out);
		pw.println(msg);
		pw.close();
	}
}

PropertiesUtil.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.weishuzhai.util;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
/**
 * 读取配置文件的一个工具类 提供访问.properties文件的相关方法
 * 
 * @author Administrator
 * 
 */
public class PropertiesUtil {
	private static Properties props = new Properties();
	static {
		InputStream ips = PropertiesUtil.class.getClassLoader()
				.getResourceAsStream("opt.properties");
		try {
			props.load(ips);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
	public static String getValue(String key) {
		return props.getProperty(key);
	}
}

opt.properties文件:

1
2
3
UserService.regist=\u7528\u6237\u6ce8\u518c\u64cd\u4f5c
UserService.login=\u7528\u6237\u767b\u5f55\u64cd\u4f5c
DeptService.delete=\u90e8\u95e8\u5220\u9664

properties文件中的值是汉字转为的ascii字符,如下:
UserService.regist=用户注册操作
UserService.login=用户登录操作
DeptService.delete=部门删除

widows下可以使用jdk->bin目录下的native2ascii工具转换,
linux下可以在终端中使用命令:native2ascii.

关注点切面类:
ExceptionLogger.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.weishuzhai.aspect;
 
import java.io.IOException;
import java.util.Date;
 
import com.weishuzhai.util.FileUtil;
 
public class ExceptionLogger {
	// 把异常信息以文件形式记录下来
	public void loggerException(Exception ex) {
		try {
			System.out.println("------Has Exception------");
			String msg = "异常类型:" + ex.getMessage() + "时间:" + new Date();
			FileUtil.write("exception.log", true, msg);
			FileUtil.write("exception.log", true, ex.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

OptLogger.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.weishuzhai.aspect;
 
import org.aspectj.lang.ProceedingJoinPoint;
 
import com.weishuzhai.util.FileUtil;
import com.weishuzhai.util.PropertiesUtil;
 
public class OptLogger {
	//将用户操作记录下来
	public Object loggerOpt(ProceedingJoinPoint pjp) throws Throwable{
		//获取要执行的方法名和目标对象类型
		String methodName = pjp.getSignature().getName();
		String clazzName = pjp.getTarget().getClass().getSimpleName();
		//去opt.properties中寻找对应中文操作信息
		String key = clazzName+"."+methodName;
		String optMsg = PropertiesUtil.getValue(key);
		System.out.println("key:"+key);
		System.out.println("optMsg:"+optMsg);
 
		Object retVal = pjp.proceed();//调用目标对象的方法
		//记录操作
		FileUtil.write("opt.log",true,optMsg);
		return retVal;
	}
}

DAO使用学习笔记2中的DAO:
UserDAO.java:

1
2
3
4
5
6
7
package com.weishuzhai.target;
 
public interface UserDAO {
	public void save();
 
	public void findByEmail(String email);
}

实现类UserDAOImpl.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.weishuzhai.target;
 
public class JDBCUserDAOImpl implements UserDAO {
 
	public void findByEmail(String email) {
		System.out.println("采用JDBC技术保存用户信息");
 
	}
 
	public void save() {
		System.out.println("采用JDBC技术查询用户信息");
 
	}
 
}

目标对象,为了进行测试,故意制造了一些异常。
UserService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package tarena.user;
 
import tarena.target.UserDAO;
 
public class UserService {
	private UserDAO userDao;
 
	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}
 
	/**
	 * 注册业务方法
	 * 
	 */
	public void regist() {
		// System.out.println("--欢迎使用该功能---");
		System.out.println("调用DAO执行注册处理");
		userDao.save();
	}
 
	/**
	 * 登录业务方法
	 * 
	 */
	public void login() {
		// System.out.println("--欢迎使用该功能---");
		System.out.println("调用DAO执行登录处理");
		userDao.findByEmail("[email protected]");
		String s = null;
		s.length();
	}
 
}

package com.weishuzhai.user;

public class DeptService {
public void delete() {
System.out.println(“删除部门信息操作”);
String s = “abc”;
Integer i = Integer.parseInt(s);
}
}

spring配置文件spring-aop.xml进行配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
< ?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"
	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">
		<bean id="userService" class="com.weishuzhai.user.UserService">
			<property name="userDao" ref="userDAO"></property>
		</bean>	
		<bean id="userDAO" class="com.weishuzhai.target.JDBCUserDAOImpl"></bean>
		<bean id="deptService" class="com.weishuzhai.user.DeptService"></bean>		
		<!-- 以AOP方式记录异常信息和成功操作信息 -->
		<bean id="exLogger" class="com.weishuzhai.aspect.ExceptionLogger"></bean>
		<bean id="optLogger" class="com.weishuzhai.aspect.OptLogger"></bean>
		<aop:config>
			<!-- 定义切入点 -->
			<aop:pointcut id="loggerPointcut" expression="bean(*Service)"></aop:pointcut>
			<!-- 定义切面,记录异常日志 -->
			<aop:aspect id="exLoggerAspect" ref="exLogger">
				<!-- 异常通知 throwing属性值为loggerException方法的参数名 -->
				<aop:after -throwing pointcut-ref="loggerPointcut" method="loggerException" throwing="ex"/>
			</aop:aspect>
			<!-- 定义切面,记录成功操作日志 -->
			<aop:aspect id="optloggerAspect" ref="optLogger">
				 <!-- 环绕通知 -->
				 <aop:around pointcut-ref="loggerPointcut" method="loggerOpt"/>			
			</aop:aspect>
		</aop:config>
</beans>

测试:
TestLogger.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 package com.weishuzhai.test;
 
 
 
import org.junit.Test;
 
import org.springframework.context.ApplicationContext;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
 
import com.weishuzhai.user.DeptService;
 
import com.weishuzhai.user.UserService;
 
 
 
/**
 
 * 测试异常日志和成功操作日志
 
 * 
 
 * @author Administrator
 
 * 
 
 */
 
public class TestLogger {
 
	@Test
 
	public void testRegist() {
 
		ApplicationContext ac = new ClassPathXmlApplicationContext(
 
				"spring-aop.xml");
 
		UserService userService = (UserService) ac.getBean("userService");
 
		userService.regist();
 
	}
 
 
 
	@Test
 
	public void testLogin() {
 
		ApplicationContext ac = new ClassPathXmlApplicationContext(
 
				"spring-aop.xml");
 
		UserService userService = (UserService) ac.getBean("userService");
 
		userService.login();
 
	}
 
 
 
	@Test
 
	public void testDeptDelete() {
 
		ApplicationContext ac = new ClassPathXmlApplicationContext(
 
				"spring-aop.xml");
 
		DeptService deptService = (DeptService) ac.getBean("deptService");
 
		deptService.delete();
 
	} 
}

运行结果:

控制台:

exception.log:

opt.log:

—————————————————

你可能感兴趣的:(java,spring,AOP,框架,jdbc)