我想实现下面这样一个功能,但是一时没有思路:当我一个用户登录进系统以后,我想记录这个当前用户在系统中做的所有操作,如删除资源,登录,修改权限等,(不要说在每个地方都写一段语句插入日志哦,那样维护太麻烦啦)
解决方案 »
- 关于webservice开发,请指教
- jsp taglib 报错问题,can't find the taglibrary for 。。。
- 请问高人在html中div把iframe盖住了怎么解决?
- 招北京地区兼职java,.net程序员
- 找工作
- 学jsp前,需要什么软件
- eclipse 3.01 汉化后 整合tomcat/struts 中出现的问题???????
- 一个初学ejb的问题
- Hibernate 能不能得到部分字段的内容?
- Hibernate删除表的时候删除了触发器怎么办?
- 在action把actionForm存到request和在struts-config.xml中配置actionform有什么区别
- js简单问题
package com.hundsun.fund.website.webapp.log; import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint; public class UserOprationLog { protected Logger logger = Logger.getLogger("WEBAPP"); public void userLog(ProceedingJoinPoint pjp){
Object obj[] = pjp.getArgs();
String paremeters = "";
for(int i = 0; i < obj.length; i++){
paremeters += obj[i].toString() + ",";
}
if("addUser".equals(pjp.getSignature().getName())){
logger.info("添加用户:" + pjp.getSignature().getName() + "(" +paremeters + ")");
}
//System.out.println("user action");
} } applicationContext.xml:
<!-- 配置日志AOP begin -->
<bean id="userLog" class="com.hundsun.fund.website.webapp.log.UserOprationLog" />
<bean id="systemLog" class="com.hundsun.fund.website.webapp.log.SysExceptionLog" />
<aop:config proxy-target-class="true">
<!-- 用户操作日志切面 -->
<aop:aspect ref="userLog" id="userLogAspect">
<aop:pointcut id="userLogPointcut" expression="execution(* com.hundsun.fund.website.webapp.dao.user.*.*(..))"/>
<aop:after-returning pointcut-ref="userLogPointcut" method="userLog" />
</aop:aspect>
<!-- 系统异常日志切面 -->
<aop:aspect ref="systemLog" id="systemLogAspect">
<aop:pointcut id="systemLogPointcut" expression="execution(* com.hundsun.fund.website.webapp.dao.*.*.*(..))"/>
<aop:after-throwing pointcut-ref="systemLogPointcut" method="systemLog" throwing="e"/>
</aop:aspect>
</aop:config>
<!-- 配置日志AOP end --> 参考下这个吧,希望有帮助
最简单的:先写一个类,这个类中写个记录日志的方法,参数就是你要记录的内容,里面的代码功能就是可以把参数的内容写入记事本或者数据库。
然后你在后台业务逻辑中调用这个方法
比如你新增数据,那么肯定出发后台Action中的add方法,那么在这个方法里调用上面写的记录日志的方法,然后传你的操作内容的参数过去,日志就保存了。
优点是:简单明了
缺点:每个业务方法中需要添加代码
<?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.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">
<!-- 自定义切面 -->
<bean id="welcomeAdvice" class="com.ebuilds.aspect.WelcomAdvice"></bean>
<bean id="throwAdvice" class="com.ebuilds.aspect.RuntimeException"></bean>
<!--配置Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<!-- 下面定义事务传播属性-->
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreator-->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
<property name="beanNames">
<!-- 下面是所有需要自动创建事务代理的bean-->
<list>
<!--value 这个就是配置中的bean的id,不可以重复
BeanNameAutoProxyCreator
通过name可以找到实现了某接口的类,此时会通过反射机制得到这个实现类的对象
得到对象以后可以通过getClass().getInterfaces()得到他实现的对象,这时
就可以通过java的动态代理机制来生成代理对象,代理对象也会实现目标对象所实现
的接口,代理对象的操作会调用invok(Object,Mothed,Object[])方法,在invok
方法里可以首先具体的advice,然后利用方法的反射调用目标对象里的具体的CRUD
-->
<value>userManager</value>
<value>logManager</value>
</list>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property name="interceptorNames">
<list>
<!-- 此处可增加其他新的Interceptor -->
<value>transactionInterceptor</value>
<value>welcomeAdvice</value>
<value>throwAdvice</value>
</list>
</property>
</bean>
</beans>
如果没有spring也可以使用AOP这个思想去做一个日志记录
过滤器也是一种选择([a])日志记录:理应在数据保存后进行记录,也就是在[b]段进行记录
在数据保存前,即[a]段进行记录也行,问题是一旦数据操作失败,就不应该记录,却被记录下来,这时就必须执行一个撤销操作.