Spring aop 记录日志

首先要导入aspectjrt.jar,aspectjweaver,commons-logging.jar
log4j.jar


<?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="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/bookmarks" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
destroy-method="destroy">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>onlyfun/caterpillar/model/User.hbm.xml
</value>
<value>onlyfun/caterpillar/model/Bookmark.hbm.xml
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="userDAO" class="onlyfun.caterpillar.model.UserDAO">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<bean id="bookmarkDAO" class="onlyfun.caterpillar.model.BookmarkDAO">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<!-- 要设定您的 Smtp Server 与寄件人 -->
<bean id="simpleMail" class="onlyfun.caterpillar.model.SimpleMail">
<property name="smtpHost" value="your_smtp_server" />
<property name="from" value="your_admin_address" />
</bean>

<!--**********************************************************-->
**********************************************************-->
<!-- 利用aop执行log记录日志-->
<bean id="myLog" class="onlyfun.caterpillar.util.LogHandler" />

<aop:config>
<aop:aspect id="logAspect" ref="myLog">
<aop:pointcut id="allControllerMethod"
expression="execution(* onlyfun.caterpillar.model.*.*(..))" />
<aop:before method="logging" pointcut-ref="allControllerMethod" />
</aop:aspect>

</aop:config>
</beans>




package onlyfun.caterpillar.util;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.aspectj.lang.JoinPoint;

public class LogHandler {
private static Logger logger = Logger.getLogger(LogHandler.class);
private void logging(JoinPoint joinPoint) {
// 获得当前目录路径
// String filePath=this.getClass().getResource("/").getPath();
// 找到log4j.properties配置文件所在的目录(已经创建好)
// filePath=filePath.substring(1).replace("bin", "src");
// System.out.println(filePath);
// loger所需的配置文件路径
// PropertyConfigurator.configure("log4j.properties");
// PropertyConfigurator.configure(" C:\\eclipse\\workspace\\SpringBookmarkMySql\\src\\log4j.properties");
System.out.println("logInfo: 调用" + joinPoint.getTarget().getClass()
+ " : " + joinPoint.getSignature().getName());
logger.info("-------------------------------");
// logger.error("--------------------");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值