Spring与AOP-异常通知-捕获自定义异常
2020-07-14 05:24
295 查看
使不同异常执行不同的方法
- 目录
- 代码
- ISomeService.java(接口)
- MyTest.java(测试类)
- MyThrowsAdvice.java(切面)
- PasswordException.java(密码异常)
- SomeServiceImpl.java(实现类)
- UserException.java(用户异常.基类)
- UserNameException.java(用户名异常)
- applicationContext.xml(配置文件)
目录
代码
ISomeService.java(接口)
package com.study.aop05; // 主业务接口 public interface ISomeService { // 目标方法 void login(String username, String password) throws UserException; }
MyTest.java(测试类)
package com.study.aop05; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URL; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class MyTest { /** * 通过junit中注解的方式@Test运行函数 * @throws Exception */ @Test public void test01() throws Exception { // 创建容器对象, 加载Spring配置文件 // 会从类路径下查找配置文件 String resource = "com/study/aop05/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource);//ClassPathXmlApplicationContext(); ISomeService service = (ISomeService)ac.getBean("serviceProxy"); service.login("beijing", "beijing"); } }
MyThrowsAdvice.java(切面)
package com.study.aop05; import org.springframework.aop.ThrowsAdvice; public class MyThrowsAdvice implements ThrowsAdvice { // 当目标方法抛出UserNameException异常时,执行当前方法 public void afterThrowing(UserNameException ex) { System.out.println("发生用户名异常 ex = " + ex.getMessage()); } // 当目标方法抛出与指定类型的异常具有is-a关系的异常时,执行当前方法 public void afterThrowing(PasswordException ex) { System.out.println("发生密码异常 ex = " + ex.getMessage()); } // 当目标方法发生其他异常时,执行此方法 public void afterThrowing(Exception ex) { System.out.println("发生其他类型异常 ex = " + ex.getMessage()); } }
PasswordException.java(密码异常)
package com.study.aop05; public class PasswordException extends UserException { public PasswordException() { super(); // TODO Auto-generated constructor stub } public PasswordException(String message) { super(message); // TODO Auto-generated constructor stub } }
SomeServiceImpl.java(实现类)
package com.study.aop05; // 目标类 public class SomeServiceImpl implements ISomeService { @Override public void login(String username, String password) throws UserException { if(!"beijing".equals(username)) { throw new UserNameException("异常测试:发生用户名异常"); } if(!"beijing".equals(password)) { throw new PasswordException("异常测试:发生密码异常"); } throw new RuntimeException("异常测试: 运行时异常"); } }
UserException.java(用户异常.基类)
package com.study.aop05; // 异常分两种: // 1) 运行时异常,不进行处理也可进行编译。 // 若一个类继承自RunTimeException(UncheckException),则该异常就是运行时异常 // 2) 编译时异常,受查异常,Checked Exception。不进行处理,则无法通过编译。 // 若一个类继承自Exception 则是Checked Exception public class UserException extends Exception { public UserException() { super(); // TODO Auto-generated constructor stub } public UserException(String message) { super(message); // TODO Auto-generated constructor stub } }
UserNameException.java(用户名异常)
package com.study.aop05; public class UserNameException extends UserException { public UserNameException() { super(); // TODO Auto-generated constructor stub } public UserNameException(String message) { super(message); // TODO Auto-generated constructor stub } }
applicationContext.xml(配置文件)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <!-- bean definitions here --> <!-- 注册目标对象service --> <bean id="someService" class="com.study.aop05.SomeServiceImpl"></bean> <!-- 注册切面:通知(advice) --> <bean id="myAdvice" class="com.study.aop05.MyThrowsAdvice"></bean> <!-- 生成代理对象 --> <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="someService"></property> <property name="interceptorNames" value="myAdvice"></property> </bean> </beans>
相关文章推荐
- Spring AOP--返回通知,异常通知和环绕通知
- Spring 通过AOP 来实现前置,环绕,异常通知,注解
- Spring AOP学习笔记(3):AOP返回通知&异常通知&环绕通知
- .net mvc中AOP 异常捕获后返回自定义的Json
- Spring的AOP机制---- AOP异常通知制---- AOP异常通知
- Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。
- Spring AOP--返回通知,异常通知和环绕通知(顶)
- 3-Spring Boot ( 全局捕获异常 与 自定义异常 )
- Spring 通过来AOP 实现前置,环绕,异常通知,注解
- aop环绕通知下全局异常捕获失效
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
- 抛出自定义异常,spring AOP事务不回滚的解决方案
- Spring AOP 面向切面编程 常见通知实现(前置,后置,环绕,异常)
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
- AOP 捕获自定义异常
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
- 抛出自定义异常,spring AOP事务不回滚的解决方案
- Spring-AOP-通知-异常通知
- 关于配置spring AOP中的前置通知MethodBeforeAdvice出现的异常
- Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等