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

Spring与AOP-异常通知-捕获自定义异常

2020-07-14 05:24 295 查看

使不同异常执行不同的方法

目录

代码

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: