Spring AOP配置详解
AOP(Aspect-Oriented Programming), 面向切面编程。
Spring提供的AOP技术可以让我们把软件系统中存在的通用处理作成一个切面,从而可以减少开发人员的工作量。其次,也使得代码更加容易管理。
在软件系统中最常见的权限控制、日志记录都可以做成一个切面,下面将结合以日志切面为例来介绍Spring AOP的配置和使用。
首先,以下介绍的都在另一篇博文的基础之上展开,如果你还未阅读过,建议你先阅读一下:Spring框架配置详情
本文将按照如下的思路进行介绍:
1.导入Spring AOP 以及 LOG4J 所必须的包
2.编写applicationContext.xml文件
3.编写日志切面类
4.编写log4j.properties日志配置文件并新建一个日志文件
5.运行测试类并检查切面中的日志记录是否写入了日志文件
一、导入Spring AOP 以及 LOG4J 所必须的包
点击这里下载:http://pan.baidu.com/s/1o6E1vzs 密码:uijc
下载所需的Jar包导到WEB-INF/lib目录。
二、编写applicationContext.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-2.5.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 15 16 17 <!-- Spring整合Hibernate:直接饮用hibernate配置文件--> 18 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 19 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 20 </bean> 21 22 <!-- 创建baseDao对象 --> 23 <bean id="baseDao" class="com.basehibernate.dao.impl.BaseDaoImpl"> 24 <!-- 在Dao中使用hibernateTemplate必须注入sessionFactory --> 25 <property name="sessionFactory" ref="sessionFactory"/> 26 </bean> 27 28 <!-- 将日志切面交给Spring容器管理 开始--> 29 <bean id = "logAspectJ" class="com.basehibernate.aop.LogAspectJ"/> 30 <aop:config> 31 <aop:aspect id="logaop" ref="logAspectJ"> 32 <!-- 配置切入点 --> 33 <aop:pointcut id="logpointcut" expression="execution(* com.basehibernate.dao.impl.*.*(..))"/> 34 35 <aop:before pointcut-ref="logpointcut" method="beforeAdvice"/> 36 <aop:after pointcut-ref="logpointcut" method="afterAdvice"/> 37 </aop:aspect> 38 </aop:config> 39 <!-- 将日志切面交给Spring容器管理 结束--> 40 </beans>
第28-40行代码为声明日志切面
三、编写日志切面类
LogAspectJ.java类:
1 /** 2 * 日志切面 3 */ 4 package com.basehibernate.aop; 5 6 import org.apache.log4j.Logger; 7 import org.aspectj.lang.JoinPoint; 8 9 public class LogAspectJ { 10 11 public Logger logger = Logger.getLogger(LogAspectJ.class); 12 13 //前置通知 14 public void beforeAdvice(JoinPoint jp) 15 { 16 String info = "【前置通知】:" + jp.getTarget().getClass().getName() + "类的" + jp.getSignature().getName() + "方法开始执行!"; 17 logger.info(info); 18 } 19 20 //最终通知 21 public void afterAdvice(JoinPoint jp) 22 { 23 String info = "【最终通知】:" + jp.getTarget().getClass().getName() + "类的" + jp.getSignature().getName() + "方法开始执行!"; 24 logger.info(info); 25 } 26 }
五、编写log4j.properties日志配置文件并新建一个日志文件
在src目录下新建log4j.properties文件,配置LOG4J:
1 #设置级别 2 log4j.rootLogger=info,appender1 3 4 #输出到文件(这里默认为追加方式) 5 log4j.appender.appender1=org.apache.log4j.FileAppender 6 #设置文件输出路径为文本文件 7 log4j.appender.appender1.File=D:/Workspaces/BaseSpringAOP/WebRoot/log/system.log 8 #设置文件输出样式 9 log4j.appender.appender1.layout=org.apache.log4j.PatternLayout 10 log4j.appender.appender1.layout.ConversionPattern=[%d{yyyy-MM-dd HH\:mm\:sss,SSS}] -%m%n
在WebRoot下新建log文件夹,在log文件夹新建system.log日志文件。
六、运行测试类并检查切面中的日志记录是否写入了日志文件
1 package com.basehibernate.test; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 import com.basehibernate.dao.BaseDao; 7 import com.basehibernate.pojo.Employee; 8 9 public class SpringTest { 10 11 static ApplicationContext ctx = null; 12 @Test public void initParam() 13 { 14 ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 15 BaseDao dao = (BaseDao)ctx.getBean("baseDao"); 16 17 Employee empl = new Employee(); 18 empl.setEmplName("员工222"); 19 empl.setPassword("2222"); 20 empl.setPwFlag(true); 21 empl.setAutoEat(true); 22 23 dao.save(empl); 24 } 25 }
如果成功,将在system.log文件下可以看到: