spring aop 记要
spring aop 记录
参考:
aop概念和例子(详细):
http://blog.****.net/Matol/article/details/6096058
http://hi.baidu.com/rainfling/blog/item/d289e26fc2f2fbd381cb4af9.html
http://chenjumin.iteye.com/blog/364973
AOP例子:
http://pandonix.iteye.com/blog/336873
advisor 策略 是通过切入点 找到符合条件的方法 再根据策略 进行相应方法的事务控制
而aop:aspect 是根据切入点找到符合条件的方法 然后再根据切面的类型来适时的执行引入bean的方法
配置文件:
参考:
aop概念和例子(详细):
http://blog.****.net/Matol/article/details/6096058
http://hi.baidu.com/rainfling/blog/item/d289e26fc2f2fbd381cb4af9.html
http://chenjumin.iteye.com/blog/364973
AOP例子:
http://pandonix.iteye.com/blog/336873
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 --自动代理方式"); } }