spring中的通知

一  、Before通知 

使用Spring中的Before通知需要实现 MethodBeforeAdvice 接口



public class DaoBeforeAdvice implements MethodBeforeAdvice{
	/**
	 * @param  target 代理目标类
	 * @param method 代理目标类的的方法
	 * @param args 代理目标类的的方法的参数
	 * 如果需要对被代理的目标类操作  可以将target强转成目标类 
	 */
	public void before(Method method, Object[] args, Object target)
			throws Throwable {
		System.out.println((target instanceof PersonDao) + "\t" + method.getName() + "\t" + args.length);
	}




然后在applicationContex.xml中配置如下
 	<bean id="daoBefore" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="targetName" value="personDao"></property>
		<property name="interceptorNames">
			<array>
				<value>beforeDao</value>
			</array>
		</property>
	</bean>
	<bean name = "personDao" class="com.zf.dao.PersonDao" ></bean>
	<bean name="beforeDao" class="com.zf.aspect.DaoBeforeAdvice"></bean>




获取目标对象时用下面的方法
  
 ApplicationContext contex = new ClassPathXmlApplicationContext("applicationContext.xml");
   PersonDao personDao = (PersonDao)contex.getBean("daoBefore"); 
   personDao.savePerson();



输出结果
    true	savePerson	1
    savePerson()...



二、AfterReturning 通知
使用AfterReturning 通知需要实现AfterReturningAdvice接口
public class DaoAfter implements AfterReturningAdvice{


	/**
	 * @param returnValue 返回对象
	 */
	public void afterReturning(Object returnValue, Method method,
			Object[] args, Object target) throws Throwable {
		System.out.println("after...");
	}



然后在applicationContex.xml 中添加bean就行了
	<bean name = "daoAfter" class="com.zf.aspect.DaoAfter"></bean>
	<bean id="daoBefore" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="targetName" value="personDao"></property>
		<property name="interceptorNames">
			<array>
				<value>beforeDao</value>
				<value>daoAfter</value> 	<-- 这里也要加上 -->
			</array>
		</property>
	</bean>




三、Throw通知
使用Throw通知 需要实现ThrowsAdvice接口
并且提供afterThrowing方法 , 该方法定义如下:
	//[Method], [args], [target], 该方法还可拥有这些参数 ,不过是可选的
	public void afterThrowing(Exception subclassOfThrowable){
		System.out.println(subclassOfThrowable.getMessage());
	} 



然后在applicationContex.xml 中添加相应的配置就行了


四、Intereptor通知
需要继承DelegatingIntroductionInterceptor类
重写invok方法
	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		System.out.println("intereptor...");
		if(true)			//这里可以添加一些业务逻辑的判断 , 是否执行invoke方法
			return null;
		return super.invoke(mi);
	}

然后在applicationContex.xml中添加相应的配置就行了。



代理事务

<bean id="petStore" 
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="target"><ref local="targetService"/></property>
    <property name="transactionAttributes">
        <props>
            <prop key="insert*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>
</bean>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值