D层接口/ 实现类:PersonDaoImpl/PersonDaol
Service层接口/实现类:PersonServiceImpl/PersonService
切面类:ExceptionAspect
客户端测试类:ExceptionTest
配置文件:applicationContext.xml
PersonDao:
package com.tgb.spring.aop.xml.exception.dao;
public interface PersonDao {
public void savePerson();
public void updatePerson();
}
PersonDaoImpl:
package com.tgb.spring.aop.xml.exception.dao.impl;
import com.tgb.spring.aop.xml.exception.dao.PersonDao;
public class PersonDaoImpl implements PersonDao {
public void savePerson() {
int a = 1/0;
}
public void updatePerson() {
Long.parseLong("aaa");
}
}
PersonService:
package com.tgb.spring.aop.xml.exception.service;
public interface PersonService {
public void savePerson();
public void updatePerson();
}
PersonServiceImpl:
package com.tgb.spring.aop.xml.exception.serviceimpl;
import com.tgb.spring.aop.xml.exception.dao.PersonDao;
import com.tgb.spring.aop.xml.exception.service.PersonService;
public class PersonServiceImpl implements PersonService {
private PersonDao persaonDao;
public PersonDao getPersaonDao() {
return persaonDao;
}
public void setPersaonDao(PersonDao persaonDao) {
this.persaonDao = persaonDao;
}
public void savePerson() {
// TODO Auto-generated method stub
this.persaonDao.savePerson();
}
public void updatePerson() {
// TODO Auto-generated method stub
this.persaonDao.updatePerson();
}
}
ExceptionAspect:
package com.tgb.spring.aop.xml.exception.aspect;
import org.aspectj.lang.JoinPoint;
public class ExceptionAspect {
//throwingException
public void throwingException(JoinPoint joinPoint,Throwable ex){
System.out.println(ex.getMessage());
};
}
ExceptionTest:
package com.tgb.spring.aop.xml.exception.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tgb.spring.aop.xml.exception.service.PersonService;
public class ExceptionTest {
@Test
public void testException (){ //applicationContext.xml
ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService personService =(PersonService)context.getBean("personService");
personService.savePerson();
};
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans 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">
<bean id="personDao" class="com.tgb.spring.aop.xml.exception.dao.impl.PersonDaoImpl"></bean>
<bean id="personService" class="com.tgb.spring.aop.xml.exception.serviceimpl.PersonServiceImpl">
<property name="persaonDao">
<ref bean="personDao"/>
</property>
</bean>
<bean id ="exceptionAspect" class="com.tgb.spring.aop.xml.exception.aspect.ExceptionAspect"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.tgb.spring.aop.xml.exception.serviceimpl.*.*(..))" id="perform"/>
<aop:aspect ref="exceptionAspect">
<aop:after-throwing method="throwingException" pointcut-ref="perform" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>