spring和mybatis整合 测试报错 求大神路过解决一下!!!!!!!!
鸡蛋坑 2018-01-10 02:50:26 以下是报错内容
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mybatisTest.test2': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'stuService' is defined
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:319)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
以下是配置文件
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:component-scan base-package="com.service"/>
<!-- 1配置数据库相关参数 -->
<context:property-placeholder location="classpath:Jdbc.properties" />
<!-- 2配置连接池 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcurl" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 3配置SqlSessionFactory工厂 -->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 3.1首先加载数据源 -->
<property name="dataSource" value="dataSource"/>
<!-- 3.2加载mybatis的配置文件 -->
<property name="configLocation" value="classpath:MybatisConfig.xml"/>
<property name="mapperLocation" value="classpath:stuMapper.xml"/>
</bean>
<!-- 4配置Mapper扫描器,进行映射 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入SqlSessionFactory -->
<!-- 4.1加载SqlSessionFactory工厂 -->
<property name="SqlSessionFactoryBeanName" value="SqlSessionFactory"/>
<!-- 4.2扫描mapper接口 -->
<property name="basepackage" value="com.mapper"/>
</bean>
<!-- 5配置事物管理 -->
<bean id="txManger"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<!-- 5.1加载数据源 -->
<property name="dataSource" value="dataSource"/>
</bean>
<!-- 6声明式事物 -->
<tx:annotation-driven transaction-manager="txManger"/>
</beans>
<?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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:default-servlet-handler/>
<!-- 注解的扫描包 -->
<context:component-scan base-package="com.controller"/>
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 给实体类创建一个别名 -->
<!-- <typeAliases>
<typeAlias alias="" type=""/>
</typeAliases> -->
<!-- 扫描映射mapper.xml文件 -->
<!-- 在 sqlSessionFactory中进行扫描mapper文件 -->
<!-- <mappers>
<mapper resource="resources/Mapper/*.xml"/>
</mappers> -->
</configuration>
dao层
package com.mapper;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.entity.student;
@Repository
public interface StuDao {
// 按学号查询学生信息
student findStuBySno(int sno) throws Exception;
// 查询全部学生
List<student> findStuAll() throws Exception;
}
service层
package com.service;
import java.util.List;
import com.entity.student;
public interface StuService {
public student findStuBySno (int sno) throws Exception;
public List<student> findStuAll() throws Exception;
}
实现类
package com.serviceImp;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.entity.student;
import com.mapper.StuDao;
import com.service.StuService;
@Service("stuService")
public class StuServiceImp implements StuService {
@Autowired
private StuDao studao;
public student findStuBySno(int sno) throws Exception {
return this.studao.findStuBySno(sno);
}
public List<student> findStuAll() throws Exception {
return this.studao.findStuAll();
}
}
测试类
package mybatisTest;
import static org.junit.Assert.fail;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.entity.student;
import com.service.StuService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:Spring-*.xml"})
public class test2 {
private static Logger logger =Logger.getLogger(test2.class);
@Resource(name="stuService")
StuService stuService;
@Test
public void testFindStuBySno() throws Exception {
student student=stuService.findStuBySno(123);
System.out.println(student);
logger.info(student);
}
@Test
public void testFindStuAll() {
fail("Not yet implemented");
}
}