spring_AOP的注解开发
logger日志类:
package cn.mepu.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* 记录日志,提供公共代码
* @author shkstart
* @create 2019-11-09 15:25
*/
@Component("logger")
@Aspect //表示当前类是一个切面类
public class logger {
//表达式
@Pointcut("execution(* cn.mepu.service.imp.*.*(..))")
private void pt1(){
}
/**
* 前置日志
*/
@Before("pt1()")
public void beforePrintLog(){
System.out.println(" logger中前置开始记录日志" );
}
/**
* 后置日志
*/
@AfterReturning("pt1()")
public void afterreturningPrintLog(){
System.out.println(" logger中后置开始记录日志" );
}
/**
* 异常日志
*/
@AfterThrowing("pt1()")
public void afterThrowingPrintLog(){
System.out.println(" logger中异常开始记录日志" );
}
/**
* 最终日志
*/
@After("pt1()")
public void afterPrintLog(){
System.out.println(" logger中最终开始记录日志" );
}
//@Around("pt1()")
public Object aroundPringLog(ProceedingJoinPoint pjp){
Object reValue = null;
try {
Object[] args = pjp.getArgs();//得到方法所需的参数
//前置通知
System.out.println("logger中环绕通知开始记录日志前置" );
pjp.proceed(args);//明确调用切入点方法
//后置通知
System.out.println("logger中环绕通知开始记录日志后置" );
return reValue;
} catch (Throwable throwable) {
//异常通知
System.out.println("logger中环绕通知开始记录日志异常" );
throw new RuntimeException(throwable);
}finally {
//最终通知
System.out.println("logger中环绕通知开始记录日志最终" );
}
}
}
service层:
package cn.mepu.service.imp;
import cn.mepu.service.IAccountService;
import org.springframework.stereotype.Service;
/**
* 账户业务层实现类
* @author shkstart
* @create 2019-11-09 15:23
*/
@Service("accountService")
public class AccountServiceImp implements IAccountService {
@Override
public void saveAccount() {
System.out.println("保存");
}
@Override
public void updateAccount(int i) {
System.out.println("更新"+i);
}
@Override
public int deleteAccount() {
System.out.println("删除");
return 0;
}
}
测试类:
package cn.mepu.Test;
import cn.mepu.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试Aop
* @author shkstart
* @create 2019-11-09 15:59
*/
public class AopTest {
public static void main(String[] args) {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("Bean.xml");
//2.获取对象
IAccountService as = (IAccountService) ac.getBean("accountService");
//3.执行方法
as.saveAccount();
}
}
Bea文件:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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.xsd">
<!-- 配置spring创建容器时要扫描的包-->
<context:component-scan base-package="cn.mepu"></context:component-scan>
<!-- 配置spring开启注解AOP的支持 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
POM文件;
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.mepu</groupId>
<artifactId>day05_spring_aop_annotation</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
</project>
spring_AOP的注解开发的更多相关文章
- Spring_AOP基于AspectJ的注解开发&JDBC的模板使用&事务管理(学习笔记3)
一:AOP基于AspectJ的注解开发 1,简单的实例: 1)引入相应的jar包 2)在配置文件里引入相关约束 <beans xmlns="http://www.springfra ...
- SpringMVC注解开发初步
一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...
- SpringMVC的注解开发入门
1.Spring MVC框架简介 支持REST风格的URL 添加更多注解,可完全注解驱动 引入HTTP输入输出转换器(HttpMessageConverter) 和数据转换.格式化.验证框架无缝集成 ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- Java自定义注解开发
一.背景 最近在自己搞一个项目时,遇到可需要开发自定义注解的需求,对于没有怎么关注这些java新特性的来说,比较尴尬,索性就拿出一些时间,来进行研究下自定义注解开发的步骤以及使用方式.今天在这里记下, ...
- Annotation(四)——Struts2注解开发
Hibernate和Spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...
- Annotation(三)——Spring注解开发
Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...
- Annotation(一)——注解开发介绍
<p>在编程中,一直强调的一点就是注释的编写,注释的规范等等.尤其是越是核心,程序越复杂,逻辑越多的清空下,注释的编写对我们以后的阅读代码,维护软件起着至关重要的作用.一款软件有着好的注释 ...
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
随机推荐
- 在$CF$水题の记录
CF1158C CF1163E update after CF1173 很好,我!expert!掉rating了!! 成为pupil指日可待== 下次要记得合理安排时间== ps.一道题都没写的\(a ...
- Elasticsearch学习,请先看这一篇
题记: Elasticsearch研究有一段时间了,现特将Elasticsearch相关核心知识.原理从初学者认知.学习的角度,从以下9个方面进行详细梳理.欢迎讨论-- 0. 带着问题上路--ES是如 ...
- MYSQL中常用的工具
1.mysql(客户端链接工具): -u :指定用户名 -p:指定密码 -h:指定服务器ip或者域名 -P(大写):指定端口 例子:mysql -u root -h 202.194. ...
- go中字符类型的使用小结
示例 // 字符类型的用法 package main import ( "fmt" "unsafe" ) func main() { // 未指定类型并赋值字符 ...
- brew install ''package卡在Updating Homebrew
关闭自动更新: export HOMEBREW_NO_AUTO_UPDATE=true
- Python向方法中传递自定义类型参数
定义类型 class Fish: def __init__(self,x): self.num = xclass Turtle: def __init__(self,y): self.num = yc ...
- python读写excel(xlrd、xlwt)
一.读excel表 读excel用到xlrd模块,写excel用到xlwt模块: # 1.导入模块 import xlrd # 2.打开Excel文件读取数据 workbook = xlrd.open ...
- 使用jxls导出Excel报表
导入jar包: <dependency> <groupId>net.sf.jxls</groupId> <artifactId>jxls-core< ...
- Laravel 事务中使用悲观锁
laravel 提供了方便快捷的数据库事务使用方式,在使用中遇到过几个容易混淆和被误导的地方,这里做个记录,希望哪里写的不对的地方各位大神指点一下 laravel 事务分为手动方式和自动方式. 但如果 ...
- 【leetcode】835. Image Overlap
题目如下: 解题思路:抛开移动的过程只看移动完成的结果,记图片左上角为顶点,正方形边长为board,要使得两个图片要有重叠,那么一定其中一张图片的顶点和另外一张图片的某一个点重合.假设图片A的顶点A( ...