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项目
相关推荐
paopaokaka_luck1 分钟前
基于Spring Boot+Vue的助农销售平台(协同过滤算法、限流算法、支付宝沙盒支付、实时聊天、图形化分析)
java·spring boot·小程序·毕业设计·mybatis·1024程序员节
m0_594526305 分钟前
Python批量合并多个PDF
java·python·pdf
咕哧普拉啦8 分钟前
乐尚代驾十订单支付seata、rabbitmq异步消息、redisson延迟队列
java·spring boot·mysql·spring·maven·乐尚代驾·java最新项目
✿゚卡笨卡9 分钟前
pdf 添加页眉页脚,获取前五页
java·pdf
九鼎科技-Leo9 分钟前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
斌斌_____33 分钟前
通过反射机制,比较两个对象的字段值的差异
java
cooldream200937 分钟前
Spring Boot中集成MyBatis操作数据库详细教程
java·数据库·spring boot·mybatis
阑梦清川1 小时前
JavaEE进阶---第一个SprintBoot项目创建过程&&&我的感受
java·java-ee·springboot
程序员清风1 小时前
浅析Web实时通信技术!
java·后端·面试
wyh要好好学习1 小时前
SSM— spring,springMVC,mybatis整合
java·spring