ProxyFactory:手工编程实现AOP,编程式 添加advice 和 target 以及proxyInterfaces
ProxyFactoryBean: spring 管理,配置interceptorNames,proxyInterfaces,target
DefaultAdvisorAutoProxyCreator:spring 自动发现PointcutAdvisor,根据PointcutAdvisor的 Pointcut 判断是否进行代理
例子:
xml 配置:
test:
[code]
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());
}
[/code]
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:
[code]
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());
}
[/code]
Spring AOP 实现解析
本文介绍了Spring中AOP的几种实现方式,包括手工编程实现ProxyFactory、通过配置管理ProxyFactoryBean以及使用DefaultAdvisorAutoProxyCreator自动代理。通过具体XML配置示例展示了如何配置拦截器、切入点和增强方法。
854

被折叠的 条评论
为什么被折叠?



