Spring之AOP学习

 AOP是Spring中最重要的两个特性之一(另外一个是IOC),下面介绍下AOP相关的一些操作

一:什么是切入点?什么是通知?什么是切面?

       切入点:功能需要被扩展的方法(spring只支持方法类型的切入点)。

          通知:即为需要扩展的功能部分

           切面:切面确切的讲是一个过程,是将扩展功能应用到切入点的过程。

二:什么是aop?

     aop是Aspect Oriented Programming的缩写,翻译成中文的意思是面向切面编程,讲的通俗一点就是在不修改源代码的基础上对原有代码的功能进行扩展,由于spring支持只支持方法类型的切入点,所以通常所讲的aop是在不修改原有方法的代码基础上对该方法进行功能扩展。

三:示例

    Target类中的show方法需要进行功能扩展,扩展的功能为在show方法的输出后面打印“over!!!”,功能扩展代码编写在Patch类的enhance方法中

package com.maty.aop; 
/** 
* @author maty  e-mail:512181558@qq.com
* @version 创建时间:2018年5月21日 下午12:05:20 
* 类说明 该类中的show方法需要被增强
*/
public class Target
{
	public void show()
	{
		System.out.println("正常功能");
	}
}
package com.maty.aop; 
/** 
* @author maty  e-mail:512181558@qq.com
* @version 创建时间:2018年5月21日 下午12:30:42 
* 类说明 该类中包含了扩展功能部分,即spring中所谓的通知
*/
public class Patch
{
	public void enhance()
	{
		System.out.println("over!!!");
	}
}

xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
	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.xsd    
            http://www.springframework.org/schema/context     
            http://www.springframework.org/schema/context/spring-context.xsd    
            http://www.springframework.org/schema/mvc    
            http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx.xsd  
            http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop.xsd ">

	<!-- 将类注入到spring中 -->
	<bean id = "target" class = "com.maty.aop.Target"></bean>
	<bean id = "patch" class = "com.maty.aop.Patch"></bean>
	
	<!-- 配置aop -->
	<aop:config>
		<!-- 第一步:配置切入点(需要增强的方法,所以配置内容一定体现出了需要被增强的方法的位置) -->
		<aop:pointcut expression="execution(* com.maty.aop.Target.show(..))" id="Targetshow"/>
		
		<!-- 第二步:配置切面(切面的意思为将通知应用到切入点的过程,所以配置内容包括通知和切入点) -->
		<aop:aspect id="firstaop" ref="patch">
			<aop:after method="enhance" pointcut-ref="Targetshow"/>
		</aop:aspect>
	</aop:config>		
</beans>

测试类

package com.maty.test;

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

import com.maty.annotation.SpringAnnotation;
import com.maty.aop.Target;
import com.maty.xmlanno.MyService;

/** 
* @author maty  e-mail:512181558@qq.com
* @version 创建时间:2018年5月16日 下午5:59:24 
* 类说明 
*/
public class MyTest
{
	public static void main(String[] args)
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");
		Target target = (Target) context.getBean("target");
		target.show();
	}
}
 

运行结果

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
正常功能
over!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值