1.logger日志类
package com.springmvc.manage.model;
import org.springframework.stereotype.Component;
@Component
public class Logger {
private int logger_id;
private String logger_name;
private String person;
private String action;
private String date_time;
private String reslut;
public int getLogger_id() {
return logger_id;
}
public void setLogger_id(int logger_id) {
this.logger_id = logger_id;
}
public String getLogger_name() {
return logger_name;
}
public void setLogger_name(String logger_name) {
this.logger_name = logger_name;
}
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getDate_time() {
return date_time;
}
public void setDate_time(String date_time) {
this.date_time = date_time;
}
public String getReslut() {
return reslut;
}
public void setReslut(String reslut) {
this.reslut = reslut;
}
@Override
public String toString() {
return "Logger [logger_id=" + logger_id + ", logger_name=" + logger_name + ", person=" + person + ", action="
+ action + ", date_time=" + date_time + ", reslut=" + reslut + "]";
}
}
2.controllog元注解
package com.springmvc.manage.dao;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Documented
@Retention(RUNTIME)
@Target(ElementType.METHOD)
public @interface ControllerLog {
public String ActionName() default "";
public String ModelName() default "";
}
3.切面实现类
package com.springmvc.manage.daoimpl;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.springmvc.manage.dao.Aoplog;
import com.springmvc.manage.dao.ControllerLog;
import com.springmvc.manage.dao.LogDao;
import com.springmvc.manage.dao.LoggerDao;
import com.springmvc.manage.model.Logger;
import com.springmvc.manage.model.User;
@Aspect
@Component
public class LoggerDaoImpl implements LoggerDao {
@Autowired
private Logger logger;
HttpServletRequest request=null;
@Resource
private LogDao logdao;
@Override
@After("@annotation(com.springmvc.manage.dao.ControllerLog)")
public void beforeMethod(JoinPoint jp) throws Exception {
// TODO Auto-generated method stub
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
/*ServletWebRequest servletWebRequest=new ServletWebRequest(request);
HttpServletResponse response=servletWebRequest.getResponse();*/
HttpSession session=request.getSession();
logger.setPerson(((User)session.getAttribute("user")).getUsername());
Method m=this.getAop(jp);
ControllerLog log=(ControllerLog)m.getAnnotation(ControllerLog.class);
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
//String s=sd.format(date);
logger.setDate_time(sd.format(date));
logger.setAction(log.ActionName());
logger.setLogger_name(log.ModelName());
logdao.insertLog(logger);
System.out.println(logger.toString());
System.out.println("before");
}
@Override
@AfterReturning(pointcut="execution(* com.springmvc.manage.service.*.*(..))",returning="returnValue")
public void afteReturning(JoinPoint jp, Object returnValue) throws Exception {
// TODO Auto-generated method stub
Aoplog log=(Aoplog)this.getAop(jp).getAnnotation(Aoplog.class);
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
//String s=sd.format(date);
logger.setDate_time(sd.format(date));
logger.setAction(log.ActionName());
logger.setLogger_name(log.ModelName());
logger.setReslut(String.valueOf(returnValue));
System.out.println(logger.toString());
System.out.println("afterreturning");
}
@Override
@AfterThrowing(pointcut="execution(* com.springmvc.manage.service.*.*(..))",throwing ="e")
public void afterThrowing(JoinPoint jp, Exception e) {
// TODO Auto-generated method stub
System.out.println("afterthrow");
}
/* @Override
@Around("execution(* com.springmvc.manage.service.*.*(..))")
public Object around(ProceedingJoinPoint jp) {
// TODO Auto-generated method stub
System.out.println("around");
Object o[]=jp.getArgs();
System.out.println(o.length);
System.out.println(o[0]);
return o;
}*/
@Override
@After("execution(* com.springmvc.manage.controller.*.*(..))")
public void afterMethod(JoinPoint jp) {
// TODO Auto-generated method stub
System.out.println("after");
}
public Method getAop(JoinPoint jp) throws NoSuchMethodException, SecurityException{
Signature signature=jp.getSignature();
MethodSignature methodSignature=(MethodSignature)signature;
Method targetMethod=methodSignature.getMethod();
Method realMethod=jp.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
return realMethod;
}
}
4.controller类拦截
package com.springmvc.manage.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.springmvc.manage.dao.ControllerLog;
import com.springmvc.manage.model.User;
import com.springmvc.manage.servicedao.UserServiceDao;
/*@RequestMapping(value = "/jsp", method = {RequestMethod.GET, RequestMethod.POST})*/
@Controller
//@SessionAttributes("user")
public class UserController {
@Autowired
private UserServiceDao userservice;
//ModelMap modelMap=new ModelMap();
ModelAndView mv=new ModelAndView();
@RequestMapping(value = "/toLogin")
public ModelAndView tologin() {
System.out.println("sss123");
mv.setViewName("login");
return mv;
}
@RequestMapping("/login")
@ControllerLog(ActionName="登录",ModelName="用户日志模块")
public ModelAndView Login(HttpServletRequest request,HttpServletResponse response,HttpSession httpSession,User user){
System.out.println("sss");
try {
boolean flag=userservice.validUser(user);
if(flag==true){
httpSession.setAttribute("user",user);
mv.setViewName("success");
}else{
mv.setViewName("error");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
mv.setViewName("error");
}
return mv;
}
}
5.spring和springmvc的配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描组件 排除controller -->
<context:component-scan base-package="com.springmvc.manage"></context:component-scan>
<!-- <context:component-scan base-package="com.springmvc.manage">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan> -->
<aop:aspectj-autoproxy />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties/jdbc.proerties"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<!--初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="maxWait" value="${maxWait}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.springmvc.manage.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userdao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.springmvc.manage.dao.UserDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:resources location="/statics/" mapping="/statics/**" />
</beans>
5.代码稍后上传