spring中的aop初步认识

9 篇文章 0 订阅

本文重要介绍spring的AOP编程的主要配置.

1.定义接口文件TestServiceInter.java和TestServiceInter1.java文件

package com.aoptest;

public interface TestServiceInter {
		 public void sayHello();
}


package com.aoptest;

public interface TestServiceInter1 {
		 public void sayBye();
}

2.创建接口首先类TestService.java

package com.aoptest;

public class TestService implements TestServiceInter,TestServiceInter1 {
	private String name;
	
	@Override
	public void sayHello() {
		System.out.println(name+",Hello!!");
	}
	@Override
	public void sayBye() {
		System.out.println(name+",Bye!!");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}


3.编写前置通知类MyMethodBeforeAdvice.java实现spring的MethodBeforeAdvice.java接口

package com.aoptest;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MyMethodBeforeAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method method, Object[] arg1, Object arg2)throws Throwable {
			System.out.println("logging……………"+method.getName());
	}
}

4.配置beans.xml文件

<?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:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置被代理的对象 -->				
<bean id="testService" class="com.aoptest.TestService" >
		<property name="name" >
				<value>siege</value>
		</property>
</bean>
<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice"  class="com.aoptest.MyMethodBeforeAdvice">
</bean>
<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 配置代理接口集 -->	
		<property name="proxyInterfaces">
				<list>
						<value>com.aoptest.TestServiceInter</value>
						<value>com.aoptest.TestServiceInter1</value>
				</list>
		</property>
<!--把通知织入代理对象  -->		
		<property name="interceptorNames"  value="myMethodBeforeAdvice"></property>
<!-- 配置被代理对象 -->		
		<property name="target"  ref="testService"></property>
</bean>
</beans>

5.编写测试类App.java

package com.aoptest;

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


public class App {
	
	public static void main(String[] args) {
		ApplicationContext ac=(ApplicationContext) new ClassPathXmlApplicationContext("com/aoptest/beans.xml");
		TestServiceInter test=(TestServiceInter) ac.getBean("proxyFactoryBean");//直接获取代理对象
		test.sayHello();
		((TestServiceInter1)test).sayBye();
	}

}

测试结果如下:

logging……………sayHello
siege,Hello!!
logging……………sayBye
siege,Bye!!

其详细步骤如下:

1.定义接口

2.编写被代理对象(即目标对象)

3.编写通知对象(前置通知目标方法)

4.配置beans.xml

  • 配置被代理对象
  • 配置通知
  • 配置代理对象:

   ① 配置代理接口集

②把通知织入代理对象

③配置被代理对象

  • 编写测试类

AOP相关的术语概念:

1.切面:要实现的交叉功能,由通知实现

2.通知:切面的实际实现

3.连接点:插入切面的地点,指方法调用

4.目标对象:被代理的对象

5.代理:即代理对象

6.织入(过程):调用代理对象变发生织入

7.切入点:当织入发生时,连接点变成切入点





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值