maven下搭建注解形式的Spring aop

14 篇文章 0 订阅
6 篇文章 0 订阅

目录结构如下:



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);
	}

}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值