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("\n==UserService切面开始计时==\n"); // 代理类 执行目标
Object object = joinPoint.proceed(); mam.setEndTime(System.currentTimeMillis()); mam.setRunTime(mam.getStartTime()-mam.getEndTime()); logger.info("\n info 日志记录: UserServiceImpl开始时间:" + mam.getStartDate() + "| 响应时间:" + mam.getRunTime() + "ms" + " | 执行类:" + mam.getClassName() + " | 执行方法:" + mam.getMethodName() + "\n");
logger.debug("\n debug 日志记录: UserServiceImpl开始时间:" + mam.getStartDate() + "| 响应时间:" + mam.getRunTime() + "ms" + " | 执行类:" + mam.getClassName() + " | 执行方法:" + mam.getMethodName() + "\n"); 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("\n==Controller切面开始计时==\n"); // 代理类 执行目标
Object object = joinPoint.proceed(); mam.setEndTime(System.currentTimeMillis()); mam.setRunTime(mam.getStartTime()-mam.getEndTime()); logger.info("\n info 日志记录: Controller开始时间:" + mam.getStartDate() + "| 响应时间:" + mam.getRunTime() + "ms" + " | 执行类:" + mam.getClassName() + " | 执行方法:" + mam.getMethodName() + "\n");
logger.debug("\n debug 日志记录: Controller开始时间:" + mam.getStartDate() + "| 响应时间:" + mam.getRunTime() + "ms" + " | 执行类:" + mam.getClassName() + " | 执行方法:" + mam.getMethodName() + "\n"); 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("\n 输入的查询:" + name + "\n"); 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";
// } }
SpringAOP 注解方式的更多相关文章
- SpringAOP(注解方式实现面向切面编程)之常用Before、After、Around
一.首先在Springmvc.xml文件中引入如下内容(本示例是在ssm框架基础上实现的) 1.引入命名空间 xmlns:aop="http://www.springframework.or ...
- springAOP注解方式定义切入点报错error at ::0 can't find referenced pointcut
[说明] 1.使用spring版本:4.0.4 2.springAOP相关依赖包: 1)aopalliance-1.0.jar 2)aspectjweaver-1.8.9.jar 3)aspectjr ...
- springAOP注解方式实现日志操作
通过自定义注解调用方法执行日志存储: package com.zktx.platform.log2; import java.lang.reflect.Method; import java.util ...
- 【SpringAop】【统一日志处理】注解方式理解以及使用
[注意:本次代码的demo会存在百度网盘,由于公司的保密,禁止上传,所以仅本人可见] 目前公司在做数据资产项目,数据质量部分使用到了springaop做统一日志处理,以前对这块有了解,有点模糊不清,今 ...
- spring与hibernate整合配置基于Annotation注解方式管理实务
1.配置数据源 数据库连接基本信息存放到properties文件中,因此先加载properties文件 <!-- jdbc连接信息 --> <context:property-pla ...
- 注解方式实现Spring声明式事务管理
1.编写实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() { re ...
- 来一手 AOP 注解方式进行日志记录
系统日志对于定位/排查问题的重要性不言而喻,相信许多开发和运维都深有体会. 通过日志追踪代码运行状况,模拟系统执行情况,并迅速定位代码/部署环境问题. 系统日志同样也是数据统计/建模的重要依据,通过分 ...
- spring 纯注解方式 与AOP
spring注解方式 以前我也使用过纯注解方式.现在在这里做个记录 我们先认识几个我们都耳熟能详的注解 @configuration :从spring3.0这个注解就可以用于定义配置类,可以替换xml ...
- Spring声明式事务管理(基于注解方式实现)
----------------------siwuxie095 Spring 声明式事务管理(基于注解方式实现) 以转 ...
随机推荐
- Vue父子组件传值 | 父传子 | 子传父
父传子 父容器 <template> <div> <zdy :module='test'></zdy> </div> </templa ...
- 经典排序js实现
https://www.cnblogs.com/onepixel/articles/7674659.html
- 浅谈JS数据遍历的几种方式
遍历对象(数组)是我们日常撸码的必不可少的部分,如何从性能上优化代码,提高运行效率?下文为你揭开真像: 第一种:普通的for循环 for(j = 0; j < arr.length; j++) ...
- 【BZOJ】 4813: [Cqoi2017]小Q的棋盘
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4813 暴力转移就好,考虑以某一个点为根的子树分为是否走回来两种情况 ${f_{i,j}}$ ...
- java笔记 -- 数学函数与常量
Math类中, 包含了各种数学函数 不用在数学方法名和常量名前添加前缀Math., 只要在源文件的顶部加上这行代码: import static java.lang.Math.*; (静态导入) 例: ...
- 二叉树分派硬币 Distribute Coins in Binary Tree
2019-03-27 15:53:38 问题描述: 问题求解: 很有意思的题目.充分体现了二叉树的自底向上的递归思路. 自底向上进行运算,对于最底层的二叉子树,我们需要计算每个节点向其parent传送 ...
- ES6之Array数组
定义数组 ,]; const arr = new Array(1,2,3,4); const array1 = new Array(); array1[]="test"; 给数组不 ...
- Python- - -函数目录
一.函数的定义.调用.返回值.参数. 二.名称空间.作用域.加载顺序.取值顺序. 三.函数的嵌套.作用域链.函数名的应用.闭包. 四.装饰器
- ceph Luminous版手动安装零散记录
1.安装必要的依赖包,关防火墙,向/etc/hosts内添加域名等 2.安装ceph 配置yum源 (如果嫌慢,可以配置cachedir=/home/yum/$basearch/$releasever ...
- 小程序都报wxss编译错误
解决方法: 在控制台输入openVendor() ,清除里面的wcc.exe wcsc.exe 然后重启工具