我写了一个spring mvc web应用程序。应用程序正常运行。 我正在尝试使用spring测试套件编写DAO测试。我使用的是与web应用程序使用的相同的配置文件servlet-context.xml。当我运行测试时,我得到没有合格的豆类型错误“没有符合条件的Bean类型”,而弹簧mvc应用程序的DAO测试
我看了看周围的计算器,但它“出现”我的配置是正确的。请建议: - 找到下面的代码: -
package net.codejava.spring.dao;
import static org.junit.Assert.*;
import java.util.Date;
import java.util.List;
import junit.framework.Assert;
import net.codejava.spring.model.User;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:*/servlet-context.xml")
public class UserDAOImplTests {
@Autowired
public UserDAO userDao;//=new UserDAOImpl();
@Test
public void test() {
User user=new User();
user.setAddedBy(1);
user.setCreated(new Date());
user.setEmail("[email protected]");
user.setFirstName("Toshi");
user.setId(98);
user.setIsSoftDeleted('N');
user.setLastName("Singhal");
user.setLicenseId(1);
user.setMobile("9030137901");
user.setPassword("password");
user.setUpdated(new Date());
userDao.addUser(user);
List<User> users= userDao.findAllUsers();
String actual=users.get(0).getFirstName();
Assert.assertEquals("Toshi", actual);
}
}
我的servlet-context.xml中有豆初始化。 Servlet的context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="net.codejava.spring" />
<context:component-scan base-package="net.codejava.spring.dao" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="jdbc:sqlserver://url;" />
<property name="username" value="admin_hs" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<!-- <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop>
</props> </property> -->
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<context:annotation-config />
<bean id="userDao" class="net.codejava.spring.dao.UserDAOImpl">
</bean>
</beans>
错误日志: -
产生的原因: org.springframework.beans.factory.NoSuchBeanDefinitionException:无型的 预选赛豆[net.codejava.spring。 dao.UserDAOImpl]找到 的依赖关系:预计至少有1个bean符合自动连线 候选此依赖关系。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(必需=真)}
你确定'classpath:*/servlet-context.xml'是对的吗?你的servlet-context.xml在哪里? – alexbt
组件扫描 - - 是多余的。你可以摆脱它。 –
asg
你可以发布'UserDAOImpl'类吗?另外,还有其他正在实现/扩展'UserDAO'的类吗? – AntJavaDev