四种常用的通知类型(xml)
1、maven依赖
<?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>com.ly.spring</groupId>
<artifactId>spring05</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.6</version>
</dependency>
<!--用于整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> </dependencies>
<!--解决IDEA maven变更后自动重置LanguageLevel和JavaCompiler版本的问题-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>13</source>
<target>13</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、实体类
package com.ly.spring.domain;
import java.io.Serializable;
public class Account implements Serializable {
}
3、service接口
package com.ly.spring.service;
import com.ly.spring.domain.Account;
import java.util.List;
public interface IAccountService {
public List<Account> findAll();
}
4、service实现类
package com.ly.spring.service.impl; import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.springframework.stereotype.Service; import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Override
public List<Account> findAll() {
System.out.println("AccountServiceImpl---findAll");
int i= 1/0;
return null;
}
}
5、通知类
package com.ly.spring.utils;
import org.springframework.stereotype.Component;
@Component("logUtil")
public class LogUtil {
public void beforeFunc() {
System.out.println("---前置通知---");
}
public void afterReturnFunc() {
System.out.println("---后置通知---");
}
public void afterThrowFunc() {
System.out.println("---异常通知---");
}
public void afterFunc() {
System.out.println("--最终通知--");
}
}
6、spring配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置注解扫描的包-->
<context:component-scan base-package="com.ly.spring"></context:component-scan>
<!--aop相关配置-->
<aop:config>
<!--id指定通知的id,ref指定通知bean-->
<aop:aspect id="logAdvisor" ref="logUtil">
<!--配置通知类型,method指定调用通知bean的方法,pointcut-ref指定切入点表达式-->
<!--前置通知,在目标方法执行前执行-->
<aop:before method="beforeFunc" pointcut-ref="logPointCut"></aop:before>
<!--后置通知,在目标方法执行后执行,若目标方法抛出异常,则不会执行-->
<aop:after-returning method="afterReturnFunc" pointcut-ref="logPointCut"></aop:after-returning>
<!--异常通知,在目标方法抛出异常后执行-->
<aop:after-throwing method="afterThrowFunc" pointcut-ref="logPointCut"></aop:after-throwing>
<!--最终通知,不论目标方法是否抛出异常,都会执行-->
<aop:after method="afterFunc" pointcut-ref="logPointCut"></aop:after>
<!--配置切入点表达式-->
<!--
1、若配置在aop:aspect标签内则只对当前切面有效
2、可以配置在aop:aspect标签外,此时aop:pointcut标签必须配置在所有的aop:aspect标签前面,对所有的切面有效
-->
<aop:pointcut id="logPointCut" expression="execution(* com.ly.spring.service.impl.*.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
7、测试类
package com.ly.spring.test; import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//替换junit的main方法
@RunWith(SpringJUnit4ClassRunner.class)
//指定spring配置文件的位置
@ContextConfiguration(locations = "classpath:bean.xml")
public class MainTest {
@Autowired
private IAccountService accountService;
@Test
public void test1() {
accountService.findAll();
}
}
四种常用的通知类型(xml)的更多相关文章
- struts2 Result Type四个常用转跳类型
Result的四个常用转跳类型分别为 Dispatcher 用来转向页面,是Struts的默认形式 Redirect 重定向到一个URL Chain 用来处理Action链 RedirectAc ...
- 转--Android按钮单击事件的四种常用写法总结
这篇文章主要介绍了Android按钮单击事件的四种常用写法总结,比较了常见的四种写法的优劣,有不错的参考借鉴价值,需要的朋友可以参考下 很多学习Android程序设计的人都会发现每个人对代码的 ...
- Android按钮单击事件的四种常用写法
这篇文章主要介绍了Android按钮单击事件的四种常用写法总结,比较了常见的四种写法的优劣,有不错的参考借鉴价值,需要的朋友可以参考下 很多学习Android程序设计的人都会发现每个人对代码的写法都有 ...
- java正则表达式四种常用的处理方式是怎么样呢《匹配、分割、代替、获取》
java 正则表达式高级篇,介绍四种常用的处理方式:匹配.分割.替代.获取,具体内容如下package test; import java.util.regex.Matcher; import jav ...
- 【转】 FPGA设计的四种常用思想与技巧
本文讨论的四种常用FPGA/CPLD设计思想与技巧:乒乓操作.串并转换.流水线操作.数据接口同步化,都是FPGA/CPLD逻辑设计的内在规律的体现,合理地采用这些设计思想能在FPGA/CPLD设计工作 ...
- 四种常用的access连接方式
整理出四种常用的access连接方式,当然,第1种这是最常用的(推荐使用).1. set dbconnection=Server.CreateOBJECT("ADODB.CONNECTION ...
- javaservlet处理四种常用api请求get,put,post,delete
一般在网站搭建中servlet只需处理post,get请求便足已.本篇注重使用javaweb编写restful风格api,在servlet中对四种常用请求进行处理. 在api中对于一个请求要做的通常是 ...
- 【FPGA技巧篇一】FPGA设计的四种常用思想与技巧之一 :乒乓操作
本文篇章将讨论一下的四种常用 FPGA 设计思想与技巧: 乒乓操作. 串并转换. 流水线操作. 数据接口同步化, 都是 FPGA 逻辑设计的内在规律的体现, 合理地采用这些设计思想能在FPGA设计工作 ...
- MySQL中四种常用存储引擎的介绍
MySQL常用的四种引擎的介绍 (1):MyISAM存储引擎: 不支持事务.也不支持外键,优势是访问速度快,对事务完整性没有 要求或者以select,insert为主的应用基本上可以用这个引擎来创建表 ...
随机推荐
- 实验12: OSPF
实验9-1:单区域点到点链路的OSPF 实验目的通过本实验可以掌握:(1)在路由器上启动OSPF 路由进程(2)启用参与路由协议的接口,并且通告网络及所在的区域(3)度量值cost 的计算(4)点到点 ...
- gdiplus exception
<span style="font-size:14px;">#include <windows.h> #include <gdiplus.h> ...
- 超实用的Eclipse快捷键大全(解密为什么他们的代码写的又快又好~)
1.Ctrl+s:快速保存代码 一定要记得随时随地用Ctrl+s来保存我们的代码哦!!!不然等到电脑关机或者是使用的Eclipse突然闪退就欲哭无泪了.此时脑海里就突然出现了哔哔哔的画面~ 2.Alt ...
- 使用卷影拷贝提取ntds.dit
一.简介 通常情况下,即使拥有管理员权限,也无法读取域控制器中的C:\Windows\NTDS\ntds.dit文件.使用windows本地卷影拷贝服务,就可以获得该文件的副本. 在活动目录中,所有的 ...
- 最简单的windows 10 软路由
因为轻信了 小米路由器3潘多拉固件刷机教程 年前把自己的小米路由器3pro 刷程砖了,然后自己有一台 i5256 的三众小主机,连在电信光猫上,可以拨号,勉强可以用,but 家里的设备那么多尤其手机笔 ...
- Go语言实现:【剑指offer】丑数
该题目来源于牛客网<剑指offer>专题. 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7.习惯上我们把1当做是第一个丑 ...
- android 基础学习笔记3
1.XML解析 与 Json 解析 (文件读取一般较耗时 可将相应的解析做成方法 用线程调用) 一.XML解析 (pull解析)res/xml(资源文件 需用到Resources) Xml文本 ...
- Jmeter之设置动态关联
前言 在Jmeter中,如何进行接口关联(上一个接口的返回参数作为下一个接口的入参使用)测试呢?下面我们一起来学习吧! 需求:需要利用商品信息接口的返回结果skuName值作为下一个登录接口参数Use ...
- Update、Insert注入技巧
title: Update.Insert注入技巧 date: 2017-10-23 18:07:57 tags: ["注入"] 审计了不少代码,再看代码的时候最多出现的就是注入,很 ...
- 用户登录并返回token(springboot)
何为token?[如果想直接看代码可以往下翻] 使用基于 Token 的身份验证方法,在服务端不需要存储用户的登录记录.大概的流程是这样的:1. 客户端使用用户名跟密码请求登录2. 服务端收到请求,去 ...