gpt4 book ai didi

java - Spring & Maven & JUnit - BeanCreationException : Could not autowire field NoSuchBeanDefinitionException: No qualifying bean of type

转载 作者:太空宇宙 更新时间:2023-11-04 07:03:58 25 4
gpt4 key购买 nike

当我尝试构建项目时,我在测试阶段遇到了异常。

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'com.dozortsev.adviceexchange.service.test.UserServiceTest':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: public com.dozortsev.adviceexchange.service.UserService
com.dozortsev.adviceexchange.service.test.TestContext.userService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.dozortsev.adviceexchange.service.UserService]
found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

项目由多个模块组成:

AdviceExchange
├── domain
├── dao
├── service
├── web

我不明白为什么会发生这种情况,服务层上的所有 DAO 类都映射为 @Repository,所有类都映射为 @Service

它看起来像我的DAO应用程序上下文层:

<beans xmlns="http://www.springframework.org/schema/beans"....>

<import resource="classpath:/spring/queries.xml"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/adviceexchange"/>
<property name="username" value="root"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource"/>

<property name="packagesToScan" value="com.dozortsev.adviceexchange.domain"/>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>

用于测试的应用程序上下文:

<beans xmlns="http://www.springframework.org/schema/beans"...>

<import resource="classpath:/spring/dao-application-context.xml"/>

<context:annotation-config/>

<context:component-scan base-package="com.dozortsev.adviceexchange"/>

<!--
Define configuration for embedded database
Build HSQL database for unit testing in-memory
-->

<jdbc:embedded-database id="embeddedDatabase" type="HSQL">

<jdbc:script location="classpath:database/test-ddl.sql"/>
<jdbc:script location="classpath:database/test-dml.sql"/>
</jdbc:embedded-database>
</beans>

还有我的测试类(class):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:/spring/test-application-context.xml")
public abstract class TestContext {

static final Logger log = Logger.getLogger(TestContext.class);

@Autowired public UserService userService;

private static EmbeddedDatabase db;

@BeforeClass public static void setUp() {
log.info("creates an HSQL in-memory database");
db = new EmbeddedDatabaseBuilder().setName("embeddedDatabase").build();
}

@AfterClass public static void tearDown() {
log.info("shutdown database");
db.shutdown();
}
}

UserServiceTest是构建项目时抛出异常的类

public class UserServiceTest extends TestContext {

@Test public void testFindByLogin() {

final String login = "sed.dui.Fusce@torquentperconubia.net";

Assert.assertNotNull(userService);
User user = userService.findByLogin(login);

Assert.assertNotNull(user);
}

@Test public void testCreate() {

// prepare data for service
User user = new User(
"Mario", 25, null, "Germany, Dortmund", null, "gotze@gmail.com", "gotze_mario", 1
);
Assert.assertNull(user.getId());
userService.create(user);
Assert.assertNotNull(user.getId());
}
}

请帮我解决这个问题。我非常感谢所有的建议!

最佳答案

此文件夹错误

https://github.com/dozortsev/AdviceExchange/tree/master/service/src/test/resource

maven 约定是“资源” - 末尾带有 s,因此测试应用程序上下文不在类路径中。

因为你有 classpath*:/spring/test-app-context... 其中 * 丢失的文件被忽略,然后它找不到任何 bean,因为上下文是空的。

只需修复文件夹名称即可。

关于java - Spring & Maven & JUnit - BeanCreationException : Could not autowire field NoSuchBeanDefinitionException: No qualifying bean of type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21678542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com