• SpringAOP 注解方式


    Spring-service-mvc.xml

    <context:component-scan base-package="com.restful.controller,com.restful.aspect"/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    applicationcontext.xml

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <bean name="dapAspect" class="com.restful.aspect.DaoAspect"/>

    DaoAspect:

    package com.restful.aspect;


    import com.restful.model.MonitorAspectModel;
    import org.apache.log4j.Logger;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.stereotype.Component;

    import java.util.Date;

    @Aspect
    @Component
    public class DaoAspect {


    private static Logger logger = Logger.getLogger(MonitorAspect.class);

    private MonitorAspectModel mam = new MonitorAspectModel();

    @Around(value = "execution(* com.restful.service.UserServiceImpl.*(..))")
    public Object controllerRunTime(ProceedingJoinPoint joinPoint) throws Throwable {

    mam.setClassName(joinPoint.getClass().getName());

    mam.setMethodName(joinPoint.getSignature().getName());

    mam.setStartTime(System.currentTimeMillis());

    mam.setStartDate(new Date());

    logger.info(" ==UserService切面开始计时== ");

    // 代理类 执行目标
    Object object = joinPoint.proceed();

    mam.setEndTime(System.currentTimeMillis());

    mam.setRunTime(mam.getStartTime()-mam.getEndTime());

    logger.info(" info 日志记录: UserServiceImpl开始时间:" + mam.getStartDate() + "| 响应时间:" + mam.getRunTime() + "ms" + " | 执行类:" + mam.getClassName() + " | 执行方法:" + mam.getMethodName() + " ");
    logger.debug(" debug 日志记录: UserServiceImpl开始时间:" + mam.getStartDate() + "| 响应时间:" + mam.getRunTime() + "ms" + " | 执行类:" + mam.getClassName() + " | 执行方法:" + mam.getMethodName() + " ");

    return object;
    }


    }

    MonitorAspectModel:

    package com.restful.aspect;

    import com.restful.model.MonitorAspectModel;
    import org.apache.log4j.Logger;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.junit.Test;
    import org.springframework.stereotype.Component;

    import java.util.Date;

    @Aspect
    @Component
    public class MonitorAspect {

    private static Logger logger = Logger.getLogger(MonitorAspect.class);

    private MonitorAspectModel mam = new MonitorAspectModel();

    private Date date;


    @Test
    public void trest(){
    logger.info("测试类");
    }

    // @Pointcut(value = "execution(* com.restful.controller.*Controller*.*(..))")
    // private void controllerointcut(){}

    // @Around(value = "controllerointcut()")
    @Around(value = "execution(* com.restful.controller.RestController.*(..))")
    public Object controllerRunTime(ProceedingJoinPoint joinPoint) throws Throwable {

    mam.setClassName(joinPoint.getClass().getName());

    mam.setMethodName(joinPoint.getSignature().getName());

    mam.setStartTime(System.currentTimeMillis());

    mam.setStartDate(new Date());

    logger.info(" ==Controller切面开始计时== ");

    // 代理类 执行目标
    Object object = joinPoint.proceed();

    mam.setEndTime(System.currentTimeMillis());

    mam.setRunTime(mam.getStartTime()-mam.getEndTime());

    logger.info(" info 日志记录: Controller开始时间:" + mam.getStartDate() + "| 响应时间:" + mam.getRunTime() + "ms" + " | 执行类:" + mam.getClassName() + " | 执行方法:" + mam.getMethodName() + " ");
    logger.debug(" debug 日志记录: Controller开始时间:" + mam.getStartDate() + "| 响应时间:" + mam.getRunTime() + "ms" + " | 执行类:" + mam.getClassName() + " | 执行方法:" + mam.getMethodName() + " ");

    return object;
    }


    }

    Controller:

    package com.restful.controller;


    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.restful.model.Page;
    import com.restful.model.User;
    import com.restful.service.StudentServiceImpl;
    import com.restful.service.UserServiceImpl;
    import org.apache.log4j.Logger;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.servlet.ModelAndView;

    import javax.servlet.http.HttpSession;
    import java.util.List;

    @Controller
    @RequestMapping("/urg")
    //@RunWith(SpringJUnit4ClassRunner.class)
    //@ContextConfiguration(locations = {"classpath*:springmybatis.xml"})
    public class RestController {

    private Logger logger = Logger.getLogger(RestController.class.getName());
    private String message = "可能遇到点问题…";
    private final String SUCCESS = "success";

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springmybatis.xml");
    UserServiceImpl userService = (UserServiceImpl) applicationContext.getBean("userService");


    String mseeage = "似乎遇到点问题……";
    String info = "信息";

    // 根据姓名查询,跳转到user页面
    @RequestMapping(value = "/user/{name}",method = RequestMethod.POST)
    public ModelAndView getStudentForName(@RequestParam("name")String name, ModelAndView modelAndView ){

    logger.info(" 输入的查询:" + name + " ");

    User user = new User();
    user.setName(name);
    List<User> users = userService.findByUser(user);

    modelAndView.addObject("users",users);
    modelAndView.setViewName("/userlist");

    return modelAndView;
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public String updre(@RequestParam("id")Integer id,Model model){
    logger.info("跳转upd修改页面,修改id:"+id);
    User user = userService.findById(id);
    user.setId(id);
    model.addAttribute("upduser",user);
    return "upd";
    }

    // @ResponseBody
    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public ModelAndView upd(User user,ModelAndView modelAndView){
    logger.info("修改的数据:"+user);
    message = "修改失败";
    if(user!=null){
    if(userService.updateOne(user)!=0){
    message = "修改成功!";
    info = "修改信息";
    modelAndView.addObject("reguser",user);
    }
    }
    modelAndView.addObject("message",message);
    modelAndView.addObject("info",info);
    modelAndView.setViewName("redirect:user/usereditinfo");
    return modelAndView;
    }


    @RequestMapping(value = "/user",method = RequestMethod.DELETE)
    public String del(@RequestParam("id")Integer id,Model model){
    logger.info("删除id:"+id);
    int del = userService.deleteOne(id);
    info="删除信息";
    if(del!=0) {
    message = "删除成功";
    }else {
    message = "删除失败";
    }
    model.addAttribute("message",message);
    return "redirect:list";
    }

    @ResponseBody
    @RequestMapping(value = "/user/json",method = RequestMethod.GET)
    public ModelAndView jsonlist(@RequestParam(value = "jsonname",required = false)String name,ModelAndView modelAndView){
    logger.info("json 传入的name:" + name );
    User user = new User();
    if(name!=null && name!=""){
    user.setName(name);
    List<User> users = userService.findByUser(user);
    logger.info("查询到的users:" + users + "users的size:" + users.size());
    boolean flag = users.size()==0;
    logger.info(flag);
    if( users.size()==0 ){
    message = "查询失败";
    }else {
    //json字符串
    String usersJsonStr = JSON.toJSONString(users);
    //json对象
    JSON usersJson = (JSON) JSON.toJSON(users);
    message = "查询成功";
    modelAndView.addObject("usersJsonStr",usersJsonStr);
    modelAndView.addObject("usersJson",usersJson);
    logger.info(usersJsonStr);
    }
    }else {
    //前台已经检查是否为空,后台信息
    message = "查询姓名不能为空";
    }
    modelAndView.setViewName("jsonlist");
    modelAndView.addObject("message",message);
    return modelAndView;
    }

    @RequestMapping(value = "/user/list")
    public String userList(@RequestParam(value = "nowpage",defaultValue = "1")Integer nowpage,
    @RequestParam(value = "pagesize",defaultValue = "5")Integer pagesize,Model model){

    Page<User> page = userService.findByPage(nowpage,pagesize);
    model.addAttribute("nowPages",page);
    // requestToViewNameTranslator
    return "list";
    // return model;
    }

    @RequestMapping(value = "/user/reg")
    public String reg(){
    logger.info("跳转reg注册页面");
    return "reg";
    }
    // @ResponseBody
    @RequestMapping(value = "/user/reg",method = RequestMethod.POST)
    public String reg(Model model,User user){
    logger.info("注册的参数:"+user.toString());
    int ids = userService.insertOne(user);
    if(ids!=0){
    message = "注册成功!";
    model.addAttribute("reguser",user);
    }
    model.addAttribute("message",message);
    return "userlist";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestParam("name")String name, @RequestParam("pwd")String pwd, HttpSession httpSession, Model model){

    User user = new User();
    user.setName(name);
    user.setPwd(pwd);
    logger.info("登录信息:"+user.toString());
    info = "登录信息";
    message = "没有登陆信息!登录失败";
    List<User> users = userService.findByUser(user);
    if(users.size()!=0){
    for(User us : users){
    logger.info("查询匹配信息:" + us.toString());
    if(us.getName().equals(name) && us.getPwd().equals(pwd)){
    logger.info("正确匹配的:"+us.getName()+":"+us.getPwd()+" | ");
    message = "登陆成功!";
    httpSession.setAttribute("username",name);
    model.addAttribute("reguser",us);
    }else {
    logger.info("未匹配的:"+us.getName()+":"+us.getPwd()+" | ");
    message = "登陆失败!";
    }
    }
    }else {
    mseeage = "账号密码错误,登录失败!";
    }
    model.addAttribute("message",message);

    return "userlist";
    }

    //测试redirect:user/usereditinfo 跳转userlist 只有修改方法upd() 调用
    @RequestMapping(value = "user/usereditinfo")
    public String userlist(@RequestParam(value = "message",required = false)String message,
    @RequestParam(value = "info",required = false)String info,Model model){
    model.addAttribute("message",message);
    model.addAttribute("info",info);
    return "userlist";
    }

    // 测试
    // @RequestMapping("/login")
    // public String logOut(HttpSession httpSession){
    // httpSession.invalidate();
    // return "forward:list";
    // }

    // 测试
    // @RequestMapping(value = "/jgg")
    // public String jgg(){
    // logger.info("跳转jgg");
    // return "jgg";
    // }






    }
  • 相关阅读:
    centos的DNS服务工作流程及搭建
    将博客搬至CSDN
    全自动网络安装centos(一)安装前准备工作
    网站私有CA证书制作
    LINUX之启动流程
    脚本判断选择语句常用命令
    linux 常用端口
    linux之网卡绑定
    centos7 无法启动网络(service network restart)错误解决办法(转)
    Linux的磁盘配额详解(Quota)
  • 原文地址:https://www.cnblogs.com/meijsuger/p/9066257.html
Copyright © 2020-2023  润新知