采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
本来在读写分离那张章节时候都没有问题,刚才写控制器的更新店铺的时候想登陆进去看看页面的时候就报错了,我实在是没找到哪里有问题(http://localhost:8080/o2o/shopadmin/shopoperation),报错信息为(org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.imooc.o2o.dao.split.DynamicDataSource] for bean with name ‘dynamicDataSource’ defined in file [D:\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\o2o\WEB-INF\classes\spring\spring-dao.xml]; nested exception is java.lang.ClassNotFoundException: com.imooc.o2o.dao.split.DynamicDataSource
)
附上配置文件的图片:这个类(com.imooc.o2o.dao.split.DynamicDataSource)也没写错!!!翔仔 HELP
翔仔你看看这是我的截图,,,你说的方式我都试过,都是不好使,我现在随便定义一个路径,比如{
@RequestMapping("/string")
@ResponseBody
public String get() {
return "string";
}
}它都报错(org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.imooc.o2o.dao.split.DynamicDataSource] for bean with name 'dynamicDataSource' defined in file [D:\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\o2o\WEB-INF\classes\spring\spring-dao.xml]; nested exception is java.lang.ClassNotFoundException: com.imooc.o2o.dao.split.DynamicDataSource
),,,,确实没招了
报错信息还是老样子
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置整合mybatis过程 --> <!-- 1.配置数据库相关参数properties的属性:${url} --> <bean class="com.imooc.o2o.util.EncryptPropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:redis.properties</value> </list> </property> <property name="fileEncoding" value="UTF-8" /> </bean> <!-- 2.数据库连接池 --> <bean id="abstractDataSource" abstract="true" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30" /> <property name="minPoolSize" value="10" /> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false" /> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="10000" /> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2" /> </bean> <bean id="master" parent="abstractDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.master.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="slave" parent="abstractDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.slave.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置动态数据源,这儿targetDataSources就是路由数据源所对应的名称 --> <bean id="dynamicDataSource" class="com.imooc.o2o.dao.split.DynamicDataSource"> <property name="targetDataSources"> <map> <entry value-ref="master" key="master"></entry> <entry value-ref="slave" key="slave"></entry> </map> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"> <property name="targetDataSource"> <ref bean="dynamicDataSource" /> </property> </bean> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- 扫描entity包 使用别名 --> <property name="typeAliasesPackage" value="com.imooc.entity" /> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.imooc.o2o.dao" /> </bean> </beans>
package com.imooc.o2o.web.shopadmin; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping(value = "shopadmin", method = { RequestMethod.GET }) /** * 主要用来解析路由并转发到相应的html中 * * @author xiangze * */ public class ShopAdminController { @RequestMapping(value = "/shopoperation") public String shopOperation() { // 转发至店铺注册/编辑页面 return "shop/shopoperation"; } @RequestMapping(value = "/shoplist") public String shopList() { // 转发至店铺列表页面 return "shop/shoplist"; } @RequestMapping(value = "/shopmanagement") public String shopManagement() { // 转发至店铺管理页面 return "shop/shopmanagement"; } @RequestMapping(value = "/productcategorymanagement", method = RequestMethod.GET) private String productCategoryManage() { // 转发至商品类别管理页面 return "shop/productcategorymanagement"; } @RequestMapping(value = "/productoperation") public String productOperation() { // 转发至商品添加/编辑页面 return "shop/productoperation"; } @RequestMapping(value = "/productmanagement") public String productManagement() { // 转发至商品管理页面 return "shop/productmanagement"; } }
然后再在spring-web.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 配置SpringMVC --> <!-- 1.开启SpringMVC注解模式 --> <mvc:annotation-driven /> <!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 --> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-handler /> <!-- 3.定义视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/html/"></property> <property name="suffix" value=".html"></property> </bean> <!-- 文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <!-- 1024 * 1024 * 20 = 20M --> <property name="maxUploadSize" value="20971520"></property> <property name="maxInMemorySize" value="20971520"></property> </bean> <!-- 4.扫描web相关的bean --> <context:component-scan base-package="com.imooc.o2o.web" /> </beans>
就能访问对应的页面
登录后可查看更多问答,登录/注册
SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需
了解课程