场景描述 : 在mvc中,异常处理十分重要,在这里使用aop实现service层异常的全局捕获;
分析 : 目标类是 :serviceImpl
切面 :自定义MyException 捕获类
public interface UserDao {
void addUser() throws Exception;
}
接口实现 : 在这里我们模拟抛出异常;
package cn.labelnet.dao.impl;
import cn.labelnet.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public void addUser() throws Exception{
System.out.println("添加用户");
//模拟出错
throw new RuntimeException("我是异常");
}
}
public interface UserService {
void userAdd() throws Exception;
}
public class UserServiceImp implements UserService{
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void userAdd() throws Exception {
userDao.addUser();
}
}
用来进行测试使用;
public class UserAction {
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void userAction() throws Exception{
userService.userAdd();
}
}
切面;
public class MvcException {
//异常拦截
public void exceptionInterception(Throwable xe){
System.out.println("异常拦截: "+xe.getMessage());
}
}
方法名对应切面中的方法,throwing对应方法中的参数名一样,pointcut-ref:目标类的切入点
@Test
public void test() throws Exception {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserAction userAction= (UserAction) context.getBean("userAction");
userAction.userAction();
}
http://download.csdn.net/detail/lablenet/9383289
ICO和DI的实现也是采用注解的方式实现;
public interface UserDao {
void addUser() throws Exception;
}
接口实现 : 模拟异常抛出
import org.springframework.stereotype.Repository;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Override
public void addUser() throws Exception{
System.out.println("添加用户");
// //模拟出错
throw new RuntimeException("我是异常");
}
}
public interface UserService {
void userAdd() throws Exception;
}
接口实现 :目标类】
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImp implements UserService{
@Resource(name="userDao")
private UserDao userDao;
@Override
public void userAdd() throws Exception {
userDao.addUser();
}
}
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
@Controller("userAction")
public class UserAction {
@Resource(name="userService")
private UserService userService;
public void userAction() throws Exception{
userService.userAdd();
}
}
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component("myexception")
@Aspect //配置aop
public class MvcException {
//配置切入点
@Pointcut("execution(* cn.labelnet.action.UserServiceImp.*(..))")
private void expoint(){} //方法签名,最好是私有的,返回值为void
//异常拦截
@AfterThrowing(pointcut="expoint()",throwing="xe")
public void exceptionInterception(Throwable xe){
System.out.println("异常拦截: "+xe.getMessage());
}
}
1)导入context命名空间 ,导入aop命名空间
2)实现配置
@Test
public void test() throws Exception {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserAction userAction= (UserAction) context.getBean("userAction");
userAction.userAction();
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut expoint
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at cn.labelnet.action.TestMvc.test(TestMvc.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut expoint
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:316)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:205)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:192)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:173)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:198)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:253)
at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:287)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:113)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:85)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:66)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:360)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:324)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 34 more
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut expoint
错误,后通过查询,说aop的jar要对应jdk的版本,才可以,也就是我的aop包是1.6的对应1.6的jdk才可以,哎,再一次发现这么纠结的问题;
jdk 1.7 的 aop 包 下载 :http://download.csdn.net/detail/lablenet/9383279
http://download.csdn.net/detail/lablenet/9383292
学到这里spring基础知识应该就完了,下面开始讲spring和mybatis进行整合使用;
spring配置总包下载 :http://download.csdn.net/detail/lablenet/9383285