Spring-AOP-Exception

Source: Internet
Author: User

Public interface PersonDao {public invalid savePerson () throws an exception; Public invalid updatePerson () throws an exception; Public invalid deletePerson () throws an exception ;}
Public class PersonDaoImp ?? L implement PersonDao {@ override public invalid savePerson () throw an exception {// TODO automatically generate the method stub System. out. println ("Releaser"); int a = 1/0;} @ overwrite public invalid updatePerson () throw an exception {// TODO automatically generate method stub System. out. println ("updated by"); int a = 1/0;} @ overwrite () of deletePerson, which is public and invalid. An exception is thrown. {// TODO automatically generates the method stub System. out. println ("deleted by"); int a = 1/0 ;}}

Public interface PersonService {public invalid savePerson () throws an exception; Public invalid updatePerson () throws an exception; Public invalid deletePerson () throws an exception ;}

Public class PersonServiceImpl implements PersonService {private PersonDao personDao; Public PersonDao getPersonDao () {returns personDao;} public invalid setPersonDao (PersonDao personDao) {this. personDao = personDao;} @ overwrite public invalid savePerson () throw an exception {this. personDao. savePerson () ;}@ override public invalid updatePerson () throw an exception {this. personDao. updatePerson () ;}@ overwrite deletePerson's () thrown by the Public invalid {this. personDao. deletePerson ();}}
 
 




Public PersonAction {private PersonService personService; Public PersonService getPersonService () {returns personService;} public invalid setPersonService (PersonService personService) {this. personService = personService;} public invalid savePerson () throw an exception {this. personService. savePerson ();} public invalid updatePerson () throw an exception {this. personService. updatePerson ();} The deletePerson () Public invalid throw an exception {this. personService. deletePerson ();}}

/*** Aspect ** @ author administrator **/public class MyException {/*** write an exception notification */Public invalid getExcpetionMessage (Throwable EX) {System. out. println (ex. getMessage ());}}

<? XML version = 1.0 encoding = UTF-8?>
 <豆的xmlns =“htt??p: www.springframework.org schema beans”的xmlns:aop="“htt??p://www.springframework.org/schema/aop”的xmlns:XSI" www.w3.org 2001 xmlschema-instance”xsi:的schemalocation="“htt??p://www.springframework.org/schema/beans" http: beans spring-beans-2.5.xsd aop spring-aop-2.5.xsd“>
  
<! -1. Import the class of the action, service, and DAO layer to 2, declare aspect 3, and configure AOP.->
     
    
     
         
      
       
          
        Aspect ref = "myException">
         
        
       
      
     
    
   
  
 





Exception Handling


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;">/*** total interface of business logic ** @ author administrator **/public interface service {public object service (ServiceMapping serviceMapping) throw an exception ;}

Public class ServiceMapping {private string service class; // Method for encapsulating the full name of the service private string; // method of a Service Public String getServiceClass () {return service class ;} public invalid setServiceClass (string service class) {this. serviceClass = service class;} The Public String implements getMethod () {return method;} the public is invalid and setMethod (string method) {this. method = method ;}}


Public class PersonServiceImpl implementation service {@ override public object service (ServiceMapping serviceMapping) throw an exception {/*** serviceMapping * service class cn. itcast. exception. service. studentServiceImpl * methodName's savePerson */string method name = serviceMapping. getMethod (); Class of object obj =. forName (serviceMapping. getServiceClass () newInstance (); method = Class. forName (serviceMapping. getServiceClass () implements getMethod (method name); Return 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 {/*** this method is the total call interface of the service, so you can handle exceptions in a unified manner here * @ param serviceMapping * @ return */public static Object execution (ServiceMapping serviceMapping) {/*** serviceMapping * service class cn. itcast. exception. service. studentServiceImpl * methodName's savePerson */try {Service service = (Service) Class. forName (serviceMapping. getServiceClass ()). newInstance (); service. service (serviceMapping);} catch (Exception e) {e. printStackTrace ();} return null ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.