理解spring AOP 详细工作方式

SPRING AOP 实际是将原对象引用为一个代理,此代理由proxyfactorybean或spring的自动代理类BeanNameAutoProxyCreator创建,代理实例包含被代理的对象,同时包含切入点和通知。当调用原对象是时,实际上调用的是代理,同时调用代理的通知方法,来达到AOP的目的。

下面是一个例子先看applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myInterceptor" class="com.aop.interceptors.MyInterceptor"></bean>
<bean id="myService" class="com.aop.service.impl.MyServiceImpl"></bean>
<bean id="myPointCut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut">
<property name="expression" value="execution(* com.aop.service.*Service.*(..))"></property>
</bean>
<bean id="expressAdvice" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="myInterceptor"></property>
<property name="pointcut" ref="myPointCut"></property>
</bean>
<bean id="login" class="com.aop.managebeans.LoginBean">
<property name="myService" ref="myService"></property>
</bean>
<bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames" value="expressAdvice">
</property>
<property name="beanNames"><list><value>myService</value></list></property>
</bean>
</beans>

 其次是环绕通知的实现MyInterceptor.java

package com.aop.interceptors;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MyInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation arg0) throws Throwable {
		System.out.println("before invoke");
		Object obj = arg0.proceed();
		System.out.println("after invoke");
		return obj;
	}

}

 以下是服务类,即被代理的对象MyServiceImpl.java

package com.aop.service.impl;

import com.aop.service.MyService;

public class MyServiceImpl implements MyService {

	@Override
	public String sayHello(String name) {
		// TODO Auto-generated method stub
		return "hello"+name;
	}

}

 当MyServiceImpl被调用时。执行通知MyInterceptor的代码

package com.aop.managebeans;

import javax.faces.bean.ManagedBean;

import com.aop.service.MyService;


public class LoginBean {

	private String name;
	private String passWord;
	private MyService myService;
	/**
	 * @param myService the myService to set
	 */
	public void setMyService(MyService myService) {
		this.myService = myService;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the passWord
	 */
	public String getPassWord() {
		return passWord;
	}
	/**
	 * @param passWord the passWord to set
	 */
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	public String loginUser(){
		myService.sayHello(name);
		if(name.equals(passWord)){
			return "success";
		}

		return "error";
	}
}
 

附件为源代码。为一个JSF2的WEB工程。

你可能感兴趣的:(spring,AOP,工作,xml,bean)