AOP切面拦截-收集异常

        

        D层接口/ 实现类:PersonDaoImpl/PersonDaol

  

        Service层接口/实现类:PersonServiceImpl/PersonService


       切面类:ExceptionAspect


        客户端测试类:ExceptionTest

        

         配置文件:applicationContext.xml




PersonDao:

package com.tgb.spring.aop.xml.exception.dao;

public interface PersonDao {
   public void savePerson();
   public void updatePerson();
}


PersonDaoImpl:

package com.tgb.spring.aop.xml.exception.dao.impl;

import com.tgb.spring.aop.xml.exception.dao.PersonDao;

public class PersonDaoImpl implements PersonDao {

	public void savePerson() {
		int a = 1/0;
		
	}

	public void updatePerson() {

		Long.parseLong("aaa");
	}
  
}


PersonService:

package com.tgb.spring.aop.xml.exception.service;

public interface PersonService {
   public void savePerson();
   public void updatePerson();
}


PersonServiceImpl:


package com.tgb.spring.aop.xml.exception.serviceimpl;

import com.tgb.spring.aop.xml.exception.dao.PersonDao;
import com.tgb.spring.aop.xml.exception.service.PersonService;

public class PersonServiceImpl implements PersonService {

	private PersonDao persaonDao;
	
	
	
	
	public PersonDao getPersaonDao() {
		return persaonDao;
	}

	public void setPersaonDao(PersonDao persaonDao) {
		this.persaonDao = persaonDao;
	}

	public void savePerson() {
		// TODO Auto-generated method stub
      this.persaonDao.savePerson();	
	}

	public void updatePerson() {
		// TODO Auto-generated method stub
		this.persaonDao.updatePerson();
	}

}



ExceptionAspect:

package com.tgb.spring.aop.xml.exception.aspect;

import org.aspectj.lang.JoinPoint;


public class ExceptionAspect {
              //throwingException
	public void throwingException(JoinPoint joinPoint,Throwable ex){
		
		System.out.println(ex.getMessage());
	};
}


ExceptionTest:

package com.tgb.spring.aop.xml.exception.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tgb.spring.aop.xml.exception.service.PersonService;

public class ExceptionTest  {
	@Test
	public void testException (){                                  //applicationContext.xml
	ApplicationContext	context= new ClassPathXmlApplicationContext("applicationContext.xml");
	
	PersonService personService =(PersonService)context.getBean("personService");
	personService.savePerson();
	
	};
}


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:aop="http://www.springframework.org/schema/aop"
       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-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  	
  	<bean id="personDao" class="com.tgb.spring.aop.xml.exception.dao.impl.PersonDaoImpl"></bean>
  	<bean id="personService" class="com.tgb.spring.aop.xml.exception.serviceimpl.PersonServiceImpl">
<property name="persaonDao">
  			<ref bean="personDao"/>
  		</property>
  	</bean>
  	<bean id ="exceptionAspect" class="com.tgb.spring.aop.xml.exception.aspect.ExceptionAspect"></bean>
  	
  	
  	<aop:config>
  	<aop:pointcut expression="execution(* com.tgb.spring.aop.xml.exception.serviceimpl.*.*(..))" id="perform"/>
  	<aop:aspect ref="exceptionAspect">
  	        <aop:after-throwing method="throwingException" pointcut-ref="perform" throwing="ex"/>
  	</aop:aspect>
  	</aop:config>
</beans>


    

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值