学习spring-AOP

       AOP作为Spring与IOC一样重要的核心点,学好AOP对系统的管理会有很大的好处,AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充。定义是这样的,但是确实还是不知道AOP到底是干什么又该怎么样,所以直接看代码来回答问题吧!

       首先这里使用 AspectJ 注解声明切面的方式

1)你需要导入相关的jar包以及工程目录、将要建的包。

2)创建接口Computer.java及ComputerImpl,java,并把ComputerImpl,java放入IOC容器里。

package com.spring.day05;

public interface Computer {
	public int add(int i,int j);
	public int div(int i,int j);
	public int sub(int i,int j);
	public int mul(int i,int j);
}
package com.spring.day05;

import javax.xml.transform.Result;
@Component
public class ComputerImpl implements Computer {
	@Override
	public int add(int i, int j) {
		int result=i+j;
		return result;
	}
	@Override
	public int div(int i, int j) {
		int result=i/j;
		return result;
	}
	@Override
	public int sub(int i, int j) {
		int result=i*j;
		return result;
	}
	@Override
	public int mul(int i, int j) {
		int result=i-j;
		return result;
	}
}

 3)创建application.xml,配置自动扫描com.spring.day05,不知道这一步是干什么的看上一章。

<?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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<context:component-scan base-package="com.spring.day05"></context:component-scan>

</beans>

 4)创建Main.java运行看看,会发现只有单纯的运算结果如果,你需要在每次算数之前有一个日志提醒或是算数之后有一个日记记录,那么你会要在之前ComputerImpl内每个方法里写System.out.println(.....),这样是比较麻烦的,当你想改System.out.println(.....)的内容时还得一个一个改,为了解决的这个问题,看看AOP是怎么处理的。

package com.spring.day05;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext cxt=new ClassPathXmlApplicationContext("application.xml");
		
		Computer computer=(Computer) cxt.getBean("computerImpl");
		int result=computer.add(5, 5);
		System.out.println(result);
	}
}

5)创建LoggingAspect.java,首先@Component把这个类放入IOC容器里,@Aspect再声明为一个切面。

但这样还不行,你还得写明这个方法是要用到哪个类的哪个方法前,所以需要声明前置通知@Before("execution(public int com.spring.day05.Computer.*(..))")

package com.spring.day05;

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

@Component
@Aspect
public class LoggingAspect {
	@Before("execution(public int com.spring.day05.Computer.*(..))")
	public void beforeMethod() {
		System.out.println("this method begin.........");
	}
}

6)这样还不行,还需要在application.xml内配置Aspect,为的就是使Aspect注解起作用,自动为匹配的类生成代理对象。然后再运行下程序试试

<?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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

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

    AspectJ 支持 5 种类型的通知注解,使用方法是一样的,这里就不每个都展示了。

  1.  @Before: 前置通知, 在方法执行之前执行
  2. @After: 后置通知, 在方法执行之后执行
  3. @AfterRunning: 返回通知, 在方法返回结果之后执行
  4. @AfterThrowing: 异常通知, 在方法抛出异常之后
  5. @Around: 环绕通知, 围绕着方法执行

如果想对前置方法添加细节,可以添加JoinPoint,这里就不详细说明了,自己运行下试试。

package com.spring.day05;

import java.util.Arrays;
import java.util.List;

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

@Component
@Aspect
public class LoggingAspect {
	@Before("execution(public int com.spring.day05.Computer.*(..))")
	public void beforeMethod(JoinPoint joinPoint) {
		String nameMethod=joinPoint.getSignature().getName();
		List<Object> arg=Arrays.asList(joinPoint.getArgs());
		System.out.println("the method begin [ "+nameMethod+","+arg+" ]");
	}
}

 

如果将execution(public int com.spring.day05.Computer.*(..))指向为方法,可以使用@Pointcut,运行程序依然是可以的。

package com.spring.day05;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(1)
@Component
@Aspect
public class LoggingAspect {
	@Pointcut("execution(public int com.spring.day05.Computer.*(..))")
	public void yaya() {}
	
	@Before("yaya()")
	public void beforeMethod(JoinPoint joinPoint) {
		String nameMethod=joinPoint.getSignature().getName();
		List<Object> arg=Arrays.asList(joinPoint.getArgs());
		System.out.println("the method begin [ "+nameMethod+","+arg+" ]");
	}
}

            那么现在使用xml注解的方式试试怎么写:

1)把之前的在LoggingAspect.java、ComputerImpl.java的注入全部去掉如@Component、@Aspect、@Before、@Pointcut等这些都要去掉。方法基本不用改,只要改application.xml就可以了,现在来看看application.xml

computer的bean是一定要写的,loggingAspect的bean也要写,才能识别。

之后配置AOP

<?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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<bean id="computerImpl" class="com.spring.day5.ComputerImpl"></bean>
	
	<bean id="loggingAspect" class="com.spring.day5.LoggingAspect"></bean>
	
	<aop:config>
        配置切点表达式
		<aop:pointcut expression="execution(public int com.spring.day5.Computer.*(..))" 
		id="pointcut"/>
		
        配置切面以及通知
		<aop:aspect ref="loggingAspect">
			<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
		</aop:aspect>
		
	</aop:config>


</beans>

还需要配置后置通知,只需要在aop:aspect里添加<aop:after method="....." pointcut-ref="..",配置的方式基本是一样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值