maven下搭建注解形式的Spring aop

目录结构如下:



pom.xml文件内容如下:

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>cn.sigangjun.architecture</groupId>
	<artifactId>spring_aop</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring_aop Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.6.11</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.6.11</version>
		</dependency>

		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.1</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>spring_aop</finalName>
	</build>
</project>


在src/main/java下新建包cn.sigangjun.spring.aop,并在该包下创建如下文件:

MyAdvice.java

package cn.sigangjun.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;

//定义通知
public class MyAdvice {

	// before通知方法
	public void beforeShow() {
		System.out.println(getClass().toString() + " before show");
	}

	// after通知方法
	public void afterShow() {
		System.out.println(getClass().toString() + " after show");
	}

	// afterReturn通知方法
	public void afterReturnShow() {
		System.out.println(getClass().toString() + " afterReturn show");
	}

	// afterThrowing通知方法
	public void afterThrowingShow() {
		System.out.println(getClass().toString() + " afterThrowing show");
	}

	// around通知方法
	public void aroundShow(ProceedingJoinPoint jpoint) {

		try {
			System.out.println(getClass().toString() + " around before show");
			// 执行目标对象的连接点处的方法
			jpoint.proceed();
			System.out.println(getClass().toString() + " around after show");
		} catch (Throwable e) {
			System.out.println(getClass().toString() + " around afterThrowing show");
		}
	}
}

MyService.java

package cn.sigangjun.spring.aop;

public class MyService {

	public void show() {
		System.out.println(getClass().toString() + " show Hello World!");
	}
}

在src/java/resources下创建spring-aop.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<aop:config>
		<!-- 定义切面 引用通知的bean -->
		<aop:aspect id="my-aspect1" ref="myAdvice">
			<!-- 设置切入点 -->
			<aop:pointcut id="pointcut1" expression="execution(* cn.sigangjun.spring.aop.MyService.*(..))" />
			<!--指定before通知方法为,myAdvice.beforeShow(),引用切入点pointcut1 -->
			<aop:before method="beforeShow" pointcut-ref="pointcut1" />
			<!--指定after通知方法为,myAdvice.afterShow(),引用切入点pointcut1 -->
			<aop:after method="afterShow" pointcut-ref="pointcut1" />
			<!--指定afterReturn通知方法为,myAdvice.afterReturnShow(),引用切入点pointcut1 -->
			<aop:after-returning method="afterReturnShow" pointcut-ref="pointcut1" />
			<!--指定afterThrowing通知方法为,myAdvice.afterThrowingShow(),引用切入点pointcut1 -->
			<aop:after-throwing method="afterThrowingShow" pointcut-ref="pointcut1" />
			<!--指定around通知方法为,myAdvice.aroundShow(),引用切入点pointcut1 -->
			<aop:around method="aroundShow" pointcut-ref="pointcut1" />
		</aop:aspect>
	</aop:config>

	<!-- 生成切面通知的bean -->
	<bean id="myAdvice" class="cn.sigangjun.spring.aop.MyAdvice" />
	<!-- 生成cn.sigangjun.spring.aop.MyService的bean -->
	<bean id="myService" class="cn.sigangjun.spring.aop.MyService"></bean>
</beans>

在src/test/java下创建测试文件MyTest.java

import static org.junit.Assert.assertTrue;

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 cn.sigangjun.spring.aop.MyService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-aop.xml"})
public class MyTest {
	@Autowired
	private MyService myService;

	@Test
	public void testService() {
		myService.show();
		assertTrue(true);
	}

}


Spring 中使用注解配置 AOP 的步骤主要包括以下几个方面: ### 1. 引入必要的依赖 首先需要确保项目的构建文件中有相应的 AOP 支持库。如果是 Maven 构建工具,可以在 `pom.xml` 文件中添加如下依赖: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>{Spring版本号}</version> </dependency> <!-- AspectJ --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>{AspectJ版本号}</version> </dependency> ``` ### 2. 启用 AOP 注解支持 要在应用上下文中启用基于注解AOP 功能,需在主配置类上加上 `@EnableAspectJAutoProxy` 注解: ```java @Configuration @EnableAspectJAutoProxy public class AppConfig { } ``` 这行代码告诉 Spring 容器开启对 @Aspect 标记切面的支持。 ### 3. 创建切面(Aop) 接下来创建一个普通的 Java 类,在该类顶部声明它是一个切面(`@Aspect`) 并注册为 Bean (`@Component`): ```java @Component @Aspect public class LoggingAspect { // 切点表达式指定拦截规则 @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice(){ System.out.println("Method is going to start"); } @AfterReturning("execution(* com.example.service.*.*(..))") public void afterReturningAdvice() { System.out.println("Method has completed successfully."); } @AfterThrowing(pointcut="execution(* com.example.service.*.*(..))", throwing="ex") public void afterThrownException(Exception ex){ System.err.println("Error occurred in method:" + ex.getMessage()); } @Around("execution(* com.example.service.*.*(..))") public Object aroundAdvice(ProceedingJoinPoint point) throws Throwable{ long beginTime=System.currentTimeMillis(); try { return point.proceed(); // 执行原定方法内容 }finally{ System.out.println("The execution time of the method was "+ (System.currentTimeMillis()-beginTime)); } } } ``` 以上示例展示了如何利用四种基本的通知类型来实现前置、后置及环绕通知等功能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值