我一开始的文件名是:spring-config.xml
根据官方文档的指导一步步配置 sqlsessionfactory 之类的配置后,在 service 层自动装配的时候就提示找不到 bean,我一开始以为是哪里错了,检查了半天没问题啊,结果后来我改了个名字,spring-mybatis.xml 就神奇的好了,这是啥缘故?
必须要指定叫做这个文件名么?哪个参数能设置这个加载路径的?
1
sweetcode OP 更正一下,refresh 一下以后还是找不到...
|
2
hand515 2017-05-07 10:45:58 +08:00
你配置问题
|
3
sweetcode OP @hand515
``` <?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"> <context:component-scan base-package="com.sweetcode" /> <context:annotation-config /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/sweetcode?characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>5201314666</value> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/sweetcode/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.sweetcode.mapper"/> </bean> </beans> ``` |
4
nutting 2017-05-07 10:50:56 +08:00 via Android
web.xml 里配置 spring.xml 路径,然后 spring 里包含各个子配置文件,名字随便吧,只是为了清晰。你这是配置内容的问题
|
7
sweetcode OP @nutting
@hand515 Junit 里面我加载了 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:config/spring-mybatis.xml") @Autowired 但是我在 Main 类中用 getBean 的方式获取就获取不到了 |
8
Lonely 2017-05-07 10:56:56 +08:00 via iPhone
web.xml 里没配好
|
10
Ouyangan 2017-05-07 12:17:52 +08:00
不要问这么傻的问题
|
11
cs4814751 2017-05-07 12:42:15 +08:00
mappersLocation 你用了通配符* 所以必须是 classpath*: 不然 mapper 找不到
|