上篇讲到了,通过log4j在自己计算机上生成日志文件。这篇主要是和spring的AOP整合起来,将日志作为一个切面横切到系统中。
一)、相关知识:
Log4j:
Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码(百度百科)。
AOP:
面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点。
二、具体实现
1、总体结构
2、Dao层
UserDao 和 GroupDao
public interface UserDao { public void addUser(); public void deleteUser(); public void updateUser(); } public interface GroupDao { public void addGroup(); public void deleteGroup(); public void updateGroup(); }
UserDaoImpl 和 GroupDaoImpl
public class UserDaoIml implements UserDao { public void addUser() { int i = 0; int n = 2 / 0; System.err.println("save user to database"); } public void deleteUser() { System.err.println("delete user from database"); } public void updateUser() { System.err.println("update user to database"); } } public class GroupDaoIml implements GroupDao { public void addGroup() { System.out.println("save Group to database"); } public void deleteGroup() { System.out.println("delete Group to database"); } public void updateGroup() { System.out.println("update Group to database"); } }
3、Service层
UserService 和 GroupService
public interface UserService { public void addUser(); public void deleteUser(); public void updateUser(); } public interface GroupService { public void addGroup(); public void deleteGroup(); public void updateGroup(); }
UserServiceImpl 和 GroupServiceImpl
public class UserServiceIml implements UserService { private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void addUser() { userDao.addUser(); } public void deleteUser() { userDao.deleteUser(); } public void updateUser() { userDao.updateUser(); } } public class GroupServiceIml implements GroupService { private GroupDao groupDao; public GroupDao getGroupDao() { return groupDao; } public void setGroupDao(GroupDao groupDao) { this.groupDao = groupDao; } public void addGroup() { groupDao.addGroup(); } public void deleteGroup() { groupDao.deleteGroup(); } public void updateGroup() { groupDao.updateGroup(); } }
4、Logger横切面
public class UserLog { Logger logger = Logger.getLogger(UserLog.class); public void begin(JoinPoint joinPoint) { logger.info("开始执行-------------" + joinPoint.getTarget().getClass() + "." + (joinPoint.getSignature().getName())); } public void excption(JoinPoint joinPoint, Throwable ex) { logger.error("出现异常-------------" + joinPoint.getTarget().getClass() + "." + joinPoint.getSignature().getName() + "-----异常信息:" + ex.getMessage() +"\r\n"+ getExceptionTrace(ex)); } public void end(JoinPoint joinPoint) { logger.info("执行完毕-------------" + joinPoint.getTarget().getClass() + "." + (joinPoint.getSignature().getName())); } public static String getExceptionTrace(Throwable ex) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); ex.printStackTrace(pw); return sw.toString(); } }
5、log4j.properties
log4j.rootLogger=Info,info,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[%-5p] [%d{yyy-MM-dd-HH:mm:ss}] - %m %l %n log4j.logger.info=Info,info log4j.appender.info=org.apache.log4j.DailyRollingFileAppender log4j.appender.info.layout=org.apache.log4j.PatternLayout log4j.appender.info.layout.ConversionPattern=[%-5p] [%d{yyy-MM-dd-HH:mm:ss}] - %m %l %n log4j.appender.info.datePattern='.'yyyy-MM-dd log4j.appender.info.File=D\:/SystemInfo.log
6、Application.xml
<bean id="groupDao" class="com.tgb.dao.GroupDaoIml"> </bean> <bean id="groupService" class="com.tgb.service.GroupServiceIml" scope="prototype"> <property name="groupDao" ref="groupDao"></property> </bean> <bean id="userDao" class="com.tgb.dao.UserDaoIml"> </bean> <bean id="userService" class="com.tgb.service.UserServiceIml" scope="prototype"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userLog" class="com.tgb.dao.UserLog"></bean> <aop:config> <!-- 定义一个切面 --> <aop:aspect ref="userLog"> <!-- 切点 --> <aop:pointcut expression="execution(* com.tgb.dao.*.*(..))" id="add" /> <!-- 方法执行之前 --> <aop:before method="begin" pointcut-ref="add" /> <!-- 方法执行之后 --> <aop:after method="end" pointcut-ref="add" /> <!-- 出现异常 --> <aop:after-throwing pointcut-ref="add" method="excption" throwing="ex" /> </aop:aspect> </aop:config> </beans>
7、测试
@Test public void testAddUser() { ApplicationContext aContext = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml"); UserService userService = (UserService) aContext.getBean("userService"); userService.deleteUser(); userService.addUser(); GroupService groupService = (GroupService) aContext.getBean("groupService"); groupService.updateGroup(); groupService.deleteGroup(); }
8、结果
控制台输出:
[INFO ] [2015-03-28-12:27:19] - Refreshing org.[email protected]490d6c15: startup date [Sat Mar 28 12:27:19 CST 2015]; root of context hierarchy org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:495) [INFO ] [2015-03-28-12:27:19] - Loading XML bean definitions from file [C:\Users\Lei\Workspaces\MyEclipse10\AopLog\WebRoot\WEB-INF\applicationContext.xml] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:315) [INFO ] [2015-03-28-12:27:20] - Pre-instantiating singletons in org.s[email protected]68bbe345: defining beans [groupDao,groupService,userDao,userService,userLog,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,add]; root of factory hierarchy org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557) [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.GroupDaoIml.updateGroup com.tgb.dao.UserLog.begin(UserLog.java:20) update Group to database [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.GroupDaoIml.updateGroup com.tgb.dao.UserLog.end(UserLog.java:33) [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.GroupDaoIml.deleteGroup com.tgb.dao.UserLog.begin(UserLog.java:20) delete Group to database [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.GroupDaoIml.deleteGroup com.tgb.dao.UserLog.end(UserLog.java:33) [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.UserDaoIml.deleteUser com.tgb.dao.UserLog.begin(UserLog.java:20) delete user from database [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.UserDaoIml.deleteUser com.tgb.dao.UserLog.end(UserLog.java:33) [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.UserDaoIml.addUser com.tgb.dao.UserLog.begin(UserLog.java:20) [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.UserDaoIml.addUser com.tgb.dao.UserLog.end(UserLog.java:33) [ERROR] [2015-03-28-12:27:21] - 出现异常-------------class com.tgb.dao.UserDaoIml.addUser 异常信息:java.lang.ArithmeticException: / by zero at com.tgb.dao.UserDaoIml.addUser(UserDaoIml.java:7) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at com.sun.proxy.$Proxy3.addUser(Unknown Source) at com.tgb.service.UserServiceIml.addUser(UserServiceIml.java:19) at com.tgb.test.TestUser.testAddUser(TestUser.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) com.tgb.dao.UserLog.excption(UserLog.java:26)
D盘SystemInfo.log
[INFO ] [2015-03-28-12:27:19] - Refreshing org.[email protected]490d6c15: startup date [Sat Mar 28 12:27:19 CST 2015]; root of context hierarchy org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:495) [INFO ] [2015-03-28-12:27:19] - Loading XML bean definitions from file [C:\Users\Lei\Workspaces\MyEclipse10\AopLog\WebRoot\WEB-INF\applicationContext.xml] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:315) [INFO ] [2015-03-28-12:27:20] - Pre-instantiating singletons in org.s[email protected]68bbe345: defining beans [groupDao,groupService,userDao,userService,userLog,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,add]; root of factory hierarchy org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557) [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.GroupDaoIml.updateGroup com.tgb.dao.UserLog.begin(UserLog.java:20) [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.GroupDaoIml.updateGroup com.tgb.dao.UserLog.end(UserLog.java:33) [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.GroupDaoIml.deleteGroup com.tgb.dao.UserLog.begin(UserLog.java:20) [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.GroupDaoIml.deleteGroup com.tgb.dao.UserLog.end(UserLog.java:33) [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.UserDaoIml.deleteUser com.tgb.dao.UserLog.begin(UserLog.java:20) [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.UserDaoIml.deleteUser com.tgb.dao.UserLog.end(UserLog.java:33) [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.UserDaoIml.addUser com.tgb.dao.UserLog.begin(UserLog.java:20) [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.UserDaoIml.addUser com.tgb.dao.UserLog.end(UserLog.java:33) [ERROR] [2015-03-28-12:27:21] - 出现异常-------------class com.tgb.dao.UserDaoIml.addUser 异常信息:java.lang.ArithmeticException: / by zero at com.tgb.dao.UserDaoIml.addUser(UserDaoIml.java:7) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at com.sun.proxy.$Proxy3.addUser(Unknown Source) at com.tgb.service.UserServiceIml.addUser(UserServiceIml.java:19) at com.tgb.test.TestUser.testAddUser(TestUser.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) com.tgb.dao.UserLog.excption(UserLog.java:26)
(完)
三、小结
AOP作为对OOP的一个补充,使得那些独立于业务之外的公共服务例如日志,事务,安全验证等横切过来,从而实现了与业务的松耦合。再结合自定义log4j属性文件,从而AOP的实现系统的日志记录服务!
时间: 2024-11-08 23:35:27