木头人
2017-11-06 21:58:14
spring mvc+spring+mybatis环境搭建遇到的问题
今天在公司搭建环境,由于公司不能连接使用存储设备,没办法,环境只能一个人慢慢的搭建。当环境搭建成功的时候,我运行了一下,发现有个错误,但是很快错误消失了,因为打印的日志太多了,控制台已经找不到错误日志了。我没办法,就只能刚开始运行就强制关闭Tomcat。功夫不负有心人,报的错误居然是无法加载${jdbc.driver}。我郁闷了半天,我建立了一个测试类。
ApplicationContext ct=new ClassPathXmlApplicationContext("spring-beans.xml"); ct.getBean("dataSource");
还是这样的错误。具体定位是properties文件加载不进去,在网上找了一下properties的加载方法:
<!--方法1:--> <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/> <!--方法2(与第一种等价):--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!--方法3(这种是可以通过注解获取配置信息):--> <bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <!-- 这里是PropertiesFactoryBean类,它也有个locations属性,也是接收一个数组,跟上面一样 --> <property name="locations"> <array> <value>classpath:jdbc.properties</value> </array> </property> </bean> <!--方法4(通过util标签):--> <util:properties id="propertiesReader" location="classpath:jdbc.properties"/> <!--方法5 省略-->
我用的第二种,按说应该不是这个问题。然后继续找资料,发现我的mybatis和spring集成的时候,spring创建的sqlsessionFactory加载的问题。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--set注入--> <property name="basePackage" value="com.chinasoft.ssm.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
我开始配置的是:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--set注入--> <property name="basePackage" value="com.chinasoft.ssm.dao"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
这样配置默认不会先解析properties文件中的配置,所以一直初始化dataSource失败。
评论

YJH123123 LV2
2019年6月11日
suxiansheng LV3
2019年6月11日
sxdlzl LV3
2018年10月12日
付修立 LV4
2018年8月31日
ssmtest LV5
2018年8月7日
zyl LV34
2018年6月29日
wsh564494062 LV8
2018年4月16日
onewaymail LV6
2018年4月8日
203778513 LV9
2018年3月14日
lihaoweiyi LV7
2018年3月8日