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 {
private String accountName; public String getAccountName() {
return accountName;
} public void setAccountName(String accountName) {
this.accountName = accountName;
} @Override
public String toString() {
return "Account{" +
"accountName='" + accountName + '\'' +
'}';
}
}

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.ArrayList;
import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Override
public List<Account> findAll() {
System.out.println("AccountServiceImpl---findAll");
List<Account> list = new ArrayList<>();
Account account = new Account();
account.setAccountName("hehe2");
list.add(account);
return list;
}
}

5、通知类

package com.ly.spring.utils;

import org.aspectj.lang.ProceedingJoinPoint;
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("--最终通知--");
} /**
*1、spring要求给环绕通知的方法增加ProceedingJoinPoint类型的参数,可实现环绕通知的相关逻辑
*2、环绕通知方法的返回值即业务方法的返回值
*3、环绕通知方法内可具体指定前置通知、后置通知、异常通知、最终通知代码的位置
*/
public Object arroundFunc(ProceedingJoinPoint pjp) {
try {
Object result = null;
Object[] args = pjp.getArgs();//获得调用业务方法的参数
beforeFunc();//前置通知
result = pjp.proceed(args);//执行业务参数
afterReturnFunc();//后置通知
return result;
}catch (Throwable t) {
afterThrowFunc();//异常通知
throw new RuntimeException(t);
}finally {
afterFunc();//最终通知
}
}
}

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">
<!--环绕通知-->
<aop:around method="arroundFunc" pointcut-ref="logPointCut"></aop:around>
<!--配置切入点表达式-->
<!--
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; import java.util.List; //替换junit的main方法
@RunWith(SpringJUnit4ClassRunner.class)
//指定spring配置文件的位置
@ContextConfiguration(locations = "classpath:bean.xml")
public class MainTest {
@Autowired
private IAccountService accountService;
@Test
public void test1() {
List<Account> result = accountService.findAll();
System.out.println(result);
}
}

最新文章

  1. Mybatis学习--Mapper.xml映射文件
  2. easyui-datagrid 的loader属性用法
  3. hdu 4069 福州赛区网络赛I DLC ***
  4. Leetcode: Binary Watch
  5. Unity脚本在层级面板中的执行顺序测试4-附加整理
  6. 二十四种设计模式:中介者模式(Mediator Pattern)
  7. 树莓派(Rospberry Pi B+)到货亲測
  8. WSAAsyncSelect模型
  9. [HDU 1535]Invitation Cards[SPFA反向思维]
  10. Java三大特征之封装(一)
  11. Windows API 之 OpenProcessToken、GetTokenInformation
  12. Arquillian Exception:java.lang.NoClassDefFoundError
  13. centos-7 yum装docker-ce后启动失败
  14. eclipse team 没有svn
  15. CPanel/服务器文件及目录
  16. jquery实现一个标签图标hover到上面的时候显示tooltip
  17. MongoDB的web可视化管理工具
  18. Linux shell(1)
  19. APIO2018酱油记
  20. php的自动加载函数spl_autoload_register和__autoload

热门文章

  1. js笔记(3)--js实现数组转置(两种方法)
  2. Codeforces_462_B
  3. Why all application lack a kind of most really charm ?
  4. C2440 “初始化”: 无法从“std::_Vector_const_iterator&lt;std::_Vector_val&lt;std::_Simple_types&lt;_Ty&gt;&gt;&gt;”转换为“std::_Vector_iterator&lt;std::_Vector_val&lt;std::_Simple_types&lt;_Ty&gt;&gt;&gt;”
  5. CentOS使用Postfix发送邮件
  6. 数据算法 --hadoop/spark数据处理技巧 --(11.K-均值聚类 12. k-近邻)
  7. ELF文件之四——使用链接脚本-2个函数-data
  8. Linux相关知识笔记
  9. Linux 查看CPU、内存、机器型号等硬件信息
  10. FastDFS 配置文件 storage.conf