四种常用的通知类型(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为主的应用基本上可以用这个引擎来创建表 ...
随机推荐
- C陷阱与缺陷学习笔记
导读 程序是由符号(token)序列所组成的,将程序分解成符号的过程,成为"词法分析". 符号构成更大的单元--语句和声明,语法细节最终决定了语义. 词法陷阱 符号(token)指 ...
- VS下解决_CRT_SECURE_NO_WARNINGS 警告
1.带有警告的文件加 #define _CRT_SECURE_NO_WARNINGS 2.右击工程 - 属性 - 配置属性 - C/C++ - 命令行 命令行增加 /D _CRT_SECURE_NO ...
- mysql随机查询若干条数据
条不重复的数据,使用以下: 秒以上 搜索Google,网上基本上都是查询max(id) * rand()来随机获取数据. SELECT * FROM `table` AS t1 JOIN (SELE ...
- @ComponentScan注解,basePackages参数通配符
@ComponentScan(basePackages = "com.ofo.test")当basePackages的直使用通配符,使用**,不能使用*.引用:https://bl ...
- 关于Icon,Image,ImageIcon的简单的对比参考
Icon: Icon位于javax.swing包中,它是一个接口 public interface Icon,介绍为:一个小的固定大小的图片,通常用于装饰组件 有三个方法: int getIconHe ...
- Codeforces 1050D Three Religions (dp+序列自动机)
题意: 给一个1e5的串str,然后有三个起始空串,不超过1000次操作,对三个字符串的一个尾部加一个字符或者减一个字符,保证每个字符不会超过250 每次操作之后询问你这三个串是不是可以组成str的子 ...
- java10幸运抽奖
public class jh_01_知识点回顾 { public static void main(String[] args) { int a = 10; // 变量.标签. // 重新给a赋值. ...
- 题解 bzoj1954【Pku3764 The xor – longest Path】
做该题之前,至少要先会做这道题. 记 \(d[u]\) 表示 \(1\) 到 \(u\) 简单路径的异或和,该数组可以通过一次遍历求得. \(~\) 考虑 \(u\) 到 \(v\) 简单路径的异或和 ...
- sql 忘记密码 解决方法(window cmd命令解决)
cd wamp\bin\mysql\mysql5.6.17\bin mysqld --skip-grant-tables
- Python 实现转堆排序算法原理及时间复杂度(多图解释)
原创文章出自公众号:「码农富哥」,欢迎转载和关注,如转载请注明出处! 堆基本概念 堆排序是一个很重要的排序算法,它是高效率的排序算法,复杂度是O(nlogn),堆排序不仅是面试进场考的重点,而且在很多 ...