Spring笔记_AOP简单例子
来源:互联网 发布:mac 网络测试工具 编辑:程序博客网 时间:2024/05/21 09:59
接口TestServiceInter
package com.xk.aop;
public interface TestServiceInter{
public void sayHello();
}
接口TestServiceInter2
package com.xk.aop;
public interface TestServiceInter2
{
public void sayBye();
}
前置通知
package com.xk.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice
{
/**
* method 被调用的方法名
* args 方法需要的参数
* target 目标对象
*/
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable
{
System.out.println("日志记录。。。"+method.getName());
}
}
被代理的对象TestService1实现两个接口TestServiceInter,TestServiceInter2
package com.xk.aop;
public class TestService1 implements TestServiceInter,TestServiceInter2
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void sayHello()
{
System.out.println("hello: "+name);
}
@Override
public void sayBye()
{
System.out.println("hello: "+name);
}
}
beans.xml文件放在com.xk.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: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="testService1" class="com.xk.aop.TestService1">
<property name="name" value="Sawliet"/>
</bean>
<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.xk.aop.MyMethodBeforeAdvice" />
<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean" >
<!-- 代理接口集 -->
<property name="proxyInterfaces">
<list>
<value>com.xk.aop.TestServiceInter</value>
<value>com.xk.aop.TestServiceInter2</value>
</list>
</property>
<!-- 把通知织入到代理对象 -->
<property name="interceptorNames">
<value>myMethodBeforeAdvice</value>
</property>
<!-- 配置被代理对象,指定哪些对象被代理 -->
<property name="target" ref="testService1" />
</bean>
</beans>
测试:
package com.xk.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Test
{
public static void main(String[] args)
{
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/xk/aop/beans.xml"));
TestServiceInter inter = (TestServiceInter)factory.getBean("proxyFactoryBean");
inter.sayHello();
((TestServiceInter2)inter).sayBye();
}
运行结果:
日志记录。。。sayHello
hello: Sawliet
日志记录。。。sayBye
hello: Sawliet
}
- Spring笔记_AOP简单例子
- Spring学习文档_AOP
- 15.Spring学习笔记_AOP基础(by尚硅谷_佟刚)
- Mybatis笔记之四 -- mybatis与spring整合简单例子
- 一个Spring简单例子
- Spring IOC简单例子
- 简单Spring IOC例子
- spring eclipselink 简单例子
- Spring remoting简单例子
- spring-xfire简单例子
- Spring Aop 简单例子
- spring aop 简单例子
- spring:quartz 简单例子
- Spring+Hibernate 简单例子
- Spring AOP 简单例子
- spring boot 简单例子
- Spring+RabbitMq简单例子
- Spring简单例子
- 字符串处理函数详细使用例子
- jQuery取得select选择的文本与值
- 面向对象的一个重要理念————重载
- java
- 字符串与整数、浮点数、无符号整数之间的转换常用函数
- Spring笔记_AOP简单例子
- 用clojure解决 euler problem 4
- sql基本原理以及运用场景
- 【动态规划+二分】奥运大包围
- c++ string 简单应用一
- poj 4048 计算几何(线段相交)金华邀请赛 E题
- 我伟大的爷爷
- CentOS GCC升级
- hello wxpython