![](/img/trans.png)
[英]Could not autowire field jdbcTemplate - No qualifying bean of type
[英]Spring & Maven & JUnit - BeanCreationException: Could not autowire field NoSuchBeanDefinitionException: No qualifying bean of type
當我嘗試構建項目時,在測試階段遇到一個例外。
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
我不明白為什么會這樣,服務層上所有映射為@Repository
DAO類都映射為@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>
還有我的測試課 :
@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。
只需修復文件夾名稱即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.