spring模块(三)Spring AOP(2)使用

一、demo

1、spring项目

(1)pom

html 复制代码
<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.13.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.13.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.13.RELEASE</version>
		</dependency>
		
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.8.10</version>
		</dependency>

		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.10</version>
		</dependency>



		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
	</dependencies>

(2)applicationContext.xml配置文件

html 复制代码
<?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/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">

	<context:component-scan base-package="com.demo"></context:component-scan>
	
	<aop:aspectj-autoproxy />
</beans>

(3)业务代码

java 复制代码
@Service
public class EmployeeService {
	
   public int addEmployee(Employee emp) {
      System.out.println("添加员工成功");
      return 0;
   }
   public int delEmployee(Employee emp) {
      System.out.println("删除员工成功");
      return 0;
   }
   public int updateEmployee(Employee emp) {
      System.out.println("修改员工成功");
      return 0;
   }
   public Employee findEmployee(String empno) {
      System.out.println("查询员工成功");
      return new Employee();
   }
}
java 复制代码
@Service
public class PersonService{
	
	public void save(String name) {
        System.out.println("我是save()方法");
    }

    public void update(String name, Integer id) {
        System.out.println("我是update()方法");
    }

    public String getPersonName(Integer id) {
        System.out.println("我是getPersonName()方法");
        return "xxx";
    }

}

(4)AOP:

java 复制代码
@Aspect
@Component
public class MyInterceptor {
    @Pointcut("execution (* com.demo.service.PersonService.add*(..))")
    private void anyMethod() {} // 声明一个切入点,anyMethod为切入点名称
    
    @Pointcut("execution (* com.demo.service.EmployeeService.update*(..))")
    private void anotherMethod() {}
    
    // 声明该方法是一个前置通知:在目标方法开始之前执行 
    @Before("anyMethod()")
    public void doAccessCheck() {
        System.out.println("前置通知");
    }
    
    @After(value = "anyMethod()")
    public void closeResource() {
    	System.out.println("关闭数据库连接");
    }
    
    @Before("anotherMethod()")
    public void openSession() {
    	System.out.println("开启session");
    }
}

(5)测试:

java 复制代码
public class SpringAOPTest {
	@Test
	public void interceptorTest() {
		ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
		EmployeeService employeeService = (EmployeeService) cxt.getBean("employeeService");
//		personService.save("xxx");
//		personService.update("zhangsan", 1);
//		personService.getPersonName(1);
		
		employeeService.updateEmployee(new Employee());
	}
}
2、springboot项目
相关推荐
ChinaRainbowSea7 分钟前
3. RabbitMQ 的(Hello World) 和 RabbitMQ 的(Work Queues)工作队列
java·分布式·后端·rabbitmq·ruby·java-rabbitmq
生无谓8 分钟前
SpringAop动态代理和AspectJ静态代理
spring
雾月558 分钟前
LeetCode 914 卡牌分组
java·开发语言·算法·leetcode·职场和发展
melck22 分钟前
liunx日志查询常用命令总结
java·服务器·网络
dleei22 分钟前
MySql安装及SQL语句
数据库·后端·mysql
守护者17027 分钟前
JAVA学习-练习试用Java实现“实现一个Hadoop程序,使用Hive进行复杂查询和数据筛查”
java·学习
程序员 小柴33 分钟前
docker的与使用
java·docker·eureka
CryptoPP37 分钟前
springboot 对接马来西亚数据源API等多个国家的数据源
spring boot·后端·python·金融·区块链
ゞ 正在缓冲99%…38 分钟前
leetcode76.最小覆盖子串
java·算法·leetcode·字符串·双指针·滑动窗口
Source.Liu1 小时前
【学Rust写CAD】27 双线性插值函数(bilinear_interpolation.rs)
后端·rust·cad