ProxyFactoryBean: spring 管理,配置interceptorNames,proxyInterfaces,target
DefaultAdvisorAutoProxyCreator:spring 自动发现PointcutAdvisor,根据PointcutAdvisor的 Pointcut 判断是否进行代理
例子:
xml 配置:
<bean id="loginServiceImpl" class="com.gym.LoginServiceImpl"/> <bean id="ba1" class="com.gym.BeforeAdvice1"/> <bean id="dpa1" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice" ref="ba1"/> </bean> <bean id="af1" class="com.gym.AfterAdvice1"/> <bean id="dpa2" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice" ref="af1"/> </bean> <bean id="user" class="com.gym.User" > <property name="username" value="xinchun.wang"/> <property name="password" value="123456"/> </bean> <bean id="WelcomeAdvice" class="com.gym.WelcomeBeforeAdvice"/> <bean id="adp3" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="pattern"> <value>com.*login</value> <!-- 业务实现方法名匹配 --> </property> <property name="advice"> <ref bean="WelcomeAdvice"/> </property> </bean> <bean id="adp4" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="mappedNames"> <value>login</value> <!-- 业务实现方法名匹配 --> </property> <property name="advice"> <ref bean="WelcomeAdvice"/> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
test:
public class LoginServiceImplTest { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/applicationContext.xml"); LoginService userService = applicationContext.getBean("loginServiceImpl",LoginService.class); User user = new User(); user.setUsername("xinchun.wang"); user.setPassword("123456"); userService.login(user); System.out.println(userService.getClass()); }