Spring-AOP-Exception

公共接口PersonDao {
	公共无效savePerson()抛出异常;
	
	公共无效的updatePerson()抛出异常;
	
	公共无效deletePerson的()抛出异常;

}
公共类PersonDaoImp​​l实现PersonDao {

	@覆盖
	公共无效savePerson()抛出异常{
		/ / TODO自动生成方法存根
		System.out.println(“拯救者”);
		INT A = 1/0;
	}

	@覆盖
	公共无效的updatePerson()抛出异常{
		/ / TODO自动生成方法存根
		System.out.println(“更新的人”);
		INT A = 1/0;
	}

	@覆盖
	公共无效deletePerson的()抛出异常{
		/ / TODO自动生成方法存根
		System.out.println(“删除的人”);
		INT A = 1/0;
	}
	
}

公共接口PersonService {
	公共无效savePerson()抛出异常;

	公共无效的updatePerson()抛出异常;

	公共无效deletePerson的()抛出异常;

}

公共类PersonServiceImpl实现PersonService {

	私人PersonDao personDao;
	
	公共PersonDao getPersonDao(){
		返回personDao;
	}

	公共无效setPersonDao(PersonDao personDao){
		this.personDao = personDao;
	}

	@覆盖
	公共无效savePerson()抛出异常{
		this.personDao.savePerson();
	}

	@覆盖
	公共无效的updatePerson()抛出异常{
		this.personDao.updatePerson();
	}

	@覆盖
	公共无效deletePerson的()抛出异常{
		this.personDao.deletePerson();
	}

}





公共类PersonAction {
	私人PersonService personService;

	公共PersonService getPersonService(){
		返回personService;
	}

	公共无效setPersonService(PersonService personService){
		this.personService = personService;
	}
	
	公共无效savePerson()抛出异常{
		this.personService.savePerson();
	}
	
	公共无效的updatePerson()抛出异常{
		this.personService.updatePerson();
	}
	
	公共无效deletePerson的()抛出异常{
		this.personService.deletePerson();
	}
}

/ **
 *切面
 * @作者管理员
 *
 * /
公共类MyException {
	/ **
	 *写一个通知为异常通知
	 * /
	公共无效getExcpetionMessage(Throwable的EX){
		System.out.println(ex.getMessage());
	}
}

<?XML版本=“1.0”编码=“UTF-8”?>
<豆的xmlns =“htt​​p://www.springframework.org/schema/beans”
	的xmlns:AOP =“htt​​p://www.springframework.org/schema/aop”
	的xmlns:XSI =“htt​​p://www.w3.org/2001/XMLSchema-instance”
	XSI:的schemaLocation =“htt​​p://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“>
	<! -  
		1,把行动,服务,DAO层的类导入进来
		2,声明切面
		3,进行AOP的配置
	 - >
	 <bean id="personDao" class="cn.itcast.spring0909.aop.exception.dao.PersonDaoImp​​l"> </豆>
	 
	 <bean id="personService" class="cn.itcast.spring0909.aop.exception.service.PersonServiceImpl">
	 	<property name="personDao">
	 		<ref bean="personDao"/>
	 	</财产>
	 </豆>
	 
	 <bean id="personAction" class="cn.itcast.spring0909.aop.exception.action.PersonAction">
	 	<property name="personService">
	 		<ref bean="personService"/>
	 	</财产>
	 </豆>
	 
	 <bean id="myException" class="cn.itcast.spring0909.aop.exception.MyException"> </豆>
	 
	 <aop:config>
	 	<aop:pointcut expression="execution(* cn.itcast.spring0909.aop.exception.service.*.*(..))" id="perform"/>
		<aop:aspect ref="myException">
			<aop:after-throwing method="getExcpetionMessage" throwing="ex" pointcut-ref="perform"/>
		</ AOP:纵横>
	 </ AOP:配置>
</豆>





异常处理


/ **
 *业务逻辑的总的接口
 * @作者管理员
 *
 * /
公共接口服务{
	公共对象服务(ServiceMapping serviceMapping)抛出异常;
}
公共类ServiceMapping {
	私人字符串服务类;/ /封装服务的全名
	
	私人字符串的方法;/ /某一个服务的方法

	公共字符串getServiceClass(){
		返回服务类;
	}

	公共无效setServiceClass(弦乐服务类){
		this.serviceClass =服务类;
	}

	公共字符串实现getMethod(){
		返回的方法;
	}

	公共无效使用setMethod(字符串方法){
		this.method =方法;
	}
}	


公共类PersonServiceImpl实现服务{

	@覆盖
	公共对象服务(ServiceMapping serviceMapping)抛出异常{
		/ **
		 * serviceMapping
		 *服务类cn.itcast.exception.service.StudentServiceImpl
		 * methodName的savePerson
		 * /
		字符串方法名= serviceMapping.getMethod();
		对象obj =的Class.forName(serviceMapping.getServiceClass())的newInstance();
		方法方法=的Class.forName(serviceMapping.getServiceClass())实现getMethod(方法名);
		返回method.invoke(OBJ);
	}
}

public class ExceptionTest {
	@Test
	public void test(){
		ServiceMapping serviceMapping = new ServiceMapping();
		serviceMapping.setServiceClass("cn.itcast.exception.service.StudentServiceImpl");
		serviceMapping.setMethod("savePerson");
		ServiceInvocation.execution(serviceMapping);
	}
}


public class ServiceInvocation {
	/ **
	 * 该方法是service总的调用接口,所以能在这里统一处理异常
	 * @param serviceMapping
	 * @return
	 * /
	public static Object execution(ServiceMapping serviceMapping){
		/ **
		 * serviceMapping
		 *服务类cn.itcast.exception.service.StudentServiceImpl
		 * methodName的savePerson
		 * /
		try{
			Service service = (Service)Class.forName(serviceMapping.getServiceClass()).newInstance();
			service.service(serviceMapping);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值