SpringAOP的最简单应用

SpringAOP, 面向切面编程,是Spring的一大特色之一,掌握切面编程,实际体验一把。

1. 引入jar包

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>3.2.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.8.9</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.9</version>
		</dependency>

依赖四个jar包。最基本的,缺一不可。

2. 配置文件打开aop配置。

	<!--打开aop -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

3. 定义切面类。

package cn.jim.java.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class springAOP {
	
	//定义切入点表达式
	@Pointcut("execution(* cn..*.*(..))")
	public void pt(){
		
	}
	
	@Before("pt()")
	public void begin(){
		System.out.println("我开始了");
		
	}
	
	@After("pt()")
	public void end(){
		System.out.println("我结束了");
	}

}

切入点表达式 * cn..*.*(..) 

第一个*表示任意返回值的方法,如果想指定入void返回值,可以写作 void  cn..*.*(..)

cn..*表示cn包及子包中的类。cn.*表示cn包中的类。

*(..)表示任意方法的执行。

切入点

@Before, @After, 切入点,指代方法执行前,执行后。

4. 测试

利用之前写的webservice接口

	public void sayHello(String request) {
		// TODO Auto-generated method stub
		System.out.println(request);
	}

发送一个请求,得到打印内容:

我开始了
{"say":"hello"}
我结束了

参考大神的博客:

https://mp.weixin.qq.com/s?__biz=MzI4Njg5MDA5NA==&mid=2247483954&idx=1&sn=b34e385ed716edf6f58998ec329f9867&chksm=ebd74333dca0ca257a77c02ab458300ef982adff3cf37eb6d8d2f985f11df5cc07ef17f659d4#rd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP(面向切面编程)是Spring框架提供的一个重要特性,用于在运行时动态地将代码切入到类的指定方法、指定位置进行增强。 下面是Spring AOP简单应用示例: 1. 首先,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.9.RELEASE</version> </dependency> ``` 2. 创建一个接口和它的实现类: ```java public interface HelloService { void sayHello(String name); } public class HelloServiceImpl implements HelloService { @Override public void sayHello(String name) { System.out.println("Hello, " + name); } } ``` 3. 创建一个切面类,在方法执行前后打印日志: ```java @Aspect public class LogAspect { @Before("execution(* com.example.service.HelloService.*(..))") public void before(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature().getName()); } @After("execution(* com.example.service.HelloService.*(..))") public void after(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature().getName()); } } ``` 4. 在Spring配置文件中配置: ```xml <bean id="helloService" class="com.example.service.HelloServiceImpl"></bean> <bean id="logAspect" class="com.example.aspect.LogAspect"></bean> <aop:config> <aop:aspect ref="logAspect"> <aop:pointcut id="helloMethod" expression="execution(* com.example.service.HelloService.*(..))"/> <aop:before pointcut-ref="helloMethod" method="before"/> <aop:after pointcut-ref="helloMethod" method="after"/> </aop:aspect> </aop:config> ``` 5. 测试: ```java public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = (HelloService)context.getBean("helloService"); helloService.sayHello("John"); } } ``` 输出结果: ``` Before method: sayHello Hello, John After method: sayHello ``` 以上就是Spring AOP简单应用示例。通过这个示例,我们可以看到如何在Spring中使用AOP来实现切面编程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值