ClassNotFoundException

就是我通过JUnit测试代码是没有任何问题的,但是当请求到达之后就报classNotFound,
贴代码,结和代码说明:

ApplicationContext ac;
UserService us;
@Before
public void init() {
    ac=new ClassPathXmlApplicationContext("spring/spring-mvc.xml","spring/spring-mybatis.xml");
    us=ac.getBean("userService",UserService.class);
}
@Test
public void testLogin() {
    User user=us.login("18888888888", "1234");
    System.out.println(user);
}

这样一个JUnit的方法测试是可以得到User对象的,先说明我这里采用的是ssm框架,
但是如下:

@Resource
private UserService us;
@ResponseBody
@RequestMapping("/login.shop")
/**
 * 登录,成功是败都返回数据
 * @param username 可以是邮箱,昵称,手机号
 * @param password 登录密码
 * @return
 */
public JSONResult login(String username,String password) {
    User user=null;
    try {
        user=us.login(username, password);
    }catch(UserNameException e) {
        return new JSONResult("001",e);
    }catch(PasswordException e) {
        return new JSONResult("002",e);
    }
    return new JSONResult(user);

}

这里执行的时候那个login方法会抛出异常,已验证us,username,password是有被赋值的。异常信息如下:

严重: Servlet.service() for servlet [shop] in context with path [/MobileShop] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'oracle.jdbc.OracleDriver'] with root cause
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:203)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:372)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:417)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:255)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy18.registByTel(Unknown Source)
at com.chanct.shop.personal.controller.Regist.registByTel(Regist.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)

感觉我都是用的同一个方法,但是关系导spring就报错了,测试就蛮好
搞了半天了,实在弄不懂是哪里错了,跪求指点

大家都说驱动。。。。附图
图片说明图片说明

求能真正解决问题的

6个回答

总算找到问题了,之前我导ojdbc的时候是在版本下加system,然后声明在电脑中的路径,大家也能看到我的ojdbc是在e盘的,这是之前别人教我的导本地jar包的方法,
我也不知道为什么我能看到jar包里面的所有内容,测试也没有问题,但是用spring走不了,,,我开始也觉得时驱动的问题,我都将.m2给删了重新下了还是没有解决。
最后。。。用cmd命令的方式将我放在e盘的ojdbc驱动导到本地的maven仓库,再重新导下这个包就解决了问题。

检查pom.xml文件

你导了Springjdbc 又有Ojdbc 两个jar冲突啦, 就ClassNotFound了。。

xiaolin_wxl
xiaolin_wxl 谢谢你的回答,不过不是这里的问题
大约 3 年之前 回复

你看看数据库驱动的jar包导进来了吗

xiaolin_wxl
xiaolin_wxl 我那个JUnit也是练剑的数据库访问的啊 ,没有导包应该会有问题的吧,可是没报错
大约 3 年之前 回复
xiaolin_wxl
xiaolin_wxl 导了ojdbc,dbcp,pool,mybatis,mybatis-spring'
大约 3 年之前 回复

缺少包了吧?你看看你有没有那个驱动包

xiaolin_wxl
xiaolin_wxl 导了ojdbc,dbcp,pool,mybatis,mybatis-spring',springjdbc,我那个JUnit也是练剑的数据库访问的啊 ,没有导包应该会有问题的吧,可是没报错
大约 3 年之前 回复

一看就是驱动没加进来 驱动

xiaolin_wxl
xiaolin_wxl 你看还差哪个驱动?
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐