spring的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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据库连接池 采用阿里巴巴的druid连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.pwd}" />
</bean>
<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis.xml" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
<property name="typeAliasesPackage" value="com.seckill.vo" />
</bean>
<!-- 配置扫描Dao包接口,动态实现Dao包中的接口,并且注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 为什么用sqlSessionFactoryBeanName不用sqlSessionFactory属性,
防止在使用sessionFactory时,从外部db。properties获取的属性还没有注入进来
导致dataSource错误,导致sessionFactory错误
-->
<property name="sqlSessionFactoryBeanName" value="sessionFactory" />
<property name="basePackage" value="com.seckill.dao" />
</bean>
</beans>
但是一getBean的时候就会报No bean named 'xxx' is defined 错误,后面才发现我把资源文件都放在了src/main/resouces 这个 source Folder下,而这个source Folder是我右键项目new出来的一个source Folder,所以资源文件永远不会加载到项目中去,一直报No bean named 'xxx' is defined 错误,解决办法就是右键项目 在builder path下 New Souce Folder 这样资源文件才能加载到项目里去.