spring aop 学习

1.做了一个简单的spring aop例子 用 aspectjrt.jar, aspectjweaver.jar 两包 通过配置文件实现, 核心配置如下
<bean id="login" class="com.Login" />
	<bean id="printLog" class="com.PrintLog" />
	<aop:config>

		<aop:aspect id="myAop" ref="printLog">
			<aop:pointcut id="taget" expression="execution(* com.Login.login(..))" />
			<aop:before method="beforeLog" pointcut-ref="taget" />
			<aop:after method="afterLog" pointcut-ref="taget" />
			<!--<aop:around method="around" pointcut-ref="taget" /> -->
		</aop:aspect>
	</aop:config>

在配置execution 表达式的时候要注意* com.Login.login(..) *号后面要空格后在接com 被代理的对象Login类如果没有实现任何接口要引入cglib.jar包。要注意的是引入的cglib.jar包是否与spring2.5.jar包中的类是否有冲突。
附件是完整代码。

2.spring aop 拦截器通过xml文件配置 不引用其它jar包 只通过spring2.5.jar实现。核心配置如下:
	<bean id="printLogService" class="com.log.PrintLogService">
		<property name="login" ref="interceptor"></property>
	</bean>

<bean id="login" class="com.Login" />
	<bean id="logInterceptor" class="com.LogInterceptor"></bean>
	<!-- 代理类, interceptor就是ILogin 接口的代理了 -->
	<bean id="interceptor" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<value>com.ILogin</value>
		</property>
		<property name="target">
			<ref bean="login" />
		</property>

		<property name="interceptorNames">
			<list>
				<value>logAdvice</value>
			</list>
		</property>

	</bean>
	<bean id="logAdvice"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice">
			<ref bean="logInterceptor" />
		</property>
		<!-- 此处的表达式是正则表达式 -->
		<property name="pattern">
			<value>.*verify.*</value>
		</property>

	</bean>

被代理的接口ILogin成功代理后,其它类中如果要注入这个类,就要注入他的代理类,interceptor如:
<bean id="printLogService" class="com.log.PrintLogService">
		<property name="login" ref="interceptor"></property>
	</bean>

附件2是例子全部代码。

你可能感兴趣的:(spring aop)