spring aop 记录

本文介绍 Spring AOP 的基本配置与实现方法,包括使用 BeanNameAutoProxyCreator 和 aop:aspect 进行切面配置的例子。通过具体代码展示了如何在 Spring 中应用 AOP 实现方法拦截。
参考:
[color=red]aop概念和例子(详细):[/color]
[url]http://blog.youkuaiyun.com/Matol/article/details/6096058[/url]


[url]http://hi.baidu.com/rainfling/blog/item/d289e26fc2f2fbd381cb4af9.html[/url]
[url]http://chenjumin.iteye.com/blog/364973[/url]
[color=red]AOP例子:[/color]
[url]http://pandonix.iteye.com/blog/336873[/url]
advisor 策略 是通过切入点 找到符合条件的方法 再根据策略 进行相应方法的事务控制


而aop:aspect 是根据切入点找到符合条件的方法 然后再根据切面的类型来适时的执行引入bean的方法

配置文件:
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="helloWorld" class="sytcun.HelloWorldServiceImp">

</bean>
<bean id="interceptorClass" class="sytcun.InterceptorClass"></bean>
<!--
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="helloWorld"/>
</property>
<property name="interceptorNames">
<list>
<value>interceptorClass</value>
</list>
</property>
</bean>
-->
<bean id="beanNameProxyCreater" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*World</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>interceptorClass</value>
</list>
</property>
</bean>

<aop:config>
<!-- <aop:advisor pointcut="" advice-ref=""/>-->
<!-- 声明切面 order:加载顺序-->
<aop:aspect ref="interceptorClass" order="0" id="interceptor">
<!-- 声明切入点 -->
<aop:pointcut expression="execution(* sytcun..*(..))" id="pointcut"/>
<!-- 声明通知 -->
<aop:before pointcut-ref="pointcut" method="beforeAdvice"/>
</aop:aspect>
</aop:config>


</beans>

package sytcun;

public interface HelloWorldService {
public void sayHello();
}

package sytcun;

public class HelloWorldServiceImp implements HelloWorldService{
public void sayHello() {
System.out.println("hello world-sytcun");
}
}

package sytcun;

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

public class HelloWorldMain {
public static void main(String args[]) {
ApplicationContext con=new FileSystemXmlApplicationContext("src/applicationContext.xml");
HelloWorldService helloWorld=(HelloWorldService)con.getBean("helloWorld");
//HelloWorldService helloWorld=(HelloWorldService)con.getBean("proxy");
helloWorld.sayHello();

HelloWorldService helloWorld2=(HelloWorldService)con.getBean("helloWorld");
helloWorld2.sayHello();
}
}

package sytcun;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class InterceptorClass implements MethodBeforeAdvice{
public void before() {
System.out.println("hello world execute");
}
public void beforeAdvice() {
System.out.println("hello world execute --aop:config方式");
}
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("hello world execute --自动代理方式");

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值