Spring-AOP-Exception
2014-03-12 20:54
260 查看
公共接口PersonDao { 公共无效savePerson()抛出异常; 公共无效的updatePerson()抛出异常; 公共无效deletePerson的()抛出异常; }
公共类PersonDaoImpl实现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 =“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“> <! -1,把行动,服务,DAO层的类导入进来2,声明切面3,进行AOP的配置- ><bean id="personDao" class="cn.itcast.spring0909.aop.exception.dao.PersonDaoImpl"> </豆><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;}}
相关文章推荐
- java.lang.ClassNotFoundException: org.springframework.aop.TargetSource
- Spring、Mybatis和Logback集成,用面向切面AOP方式捕获并打印异常Exception信息
- aspectj 使用spring AOP切面编程的时候报错:ReflectionWorld$ReflectionWorldException NoClassDefFoundError 的处理
- spring的aop拦截action中的方法会报java.lang.NoSuchMethodException: $Proxy97.test()异常
- 编写aop:around时出现的异常org.springframework.aop.AopInvocationException:
- java spring 3.2 java.lang.ClassNotFoundException: org.aopalliance.aop.Advice
- Spring Aop包版本不对应IllegalArgumentException: error at ::0 can't find referenced pointcut point_cut
- Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigExcep
- org.springframework.aop.framework.AopConfigException: Cannot proxy target class
- 通过spring的aop统一拦截Exception抛出自定义异常
- Spring、Mybatis和Logback集成,用面向切面AOP方式捕获并打印异常Exception信息
- spring-AOP编程出现org.springframework.beans.factory.BeanCreationException
- Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误)
- org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [c
- Spring AOP 报错 Exception encountered during context initialization - cancelling refresh attempt[已解决]
- 在使用Spring AOP 时,启动服务抛出异常: org.springframework.beans.factory.BeanNotOfRequiredTypeException
- Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误)
- 【spring】IllegalArgumentException Can not set field to $Proxy 在spring中使用事物或AOP遇到的错误
- Spring AOP UndeclaredThrowableException
- spring aop Caused by: java.lang.IllegalArgumentException: Pointcut is not well-formed: 报错