首先要导入aspectjrt.jar,aspectjweaver,commons-logging.jar
log4j.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("--------------------");
}
}