您的位置:首页 > 编程语言 > Java开发

Spring-AOP-Exception

2014-03-12 20:54 260 查看
公共接口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 {@Testpublic 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;}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐