當我嘗試構建項目時遇到了測試階段中的異常。Spring&Maven&JUnit - BeanCreationException:無法自動裝入字段NoSuchBeanDefinitionException:沒有符合條件的類型
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
映射爲@Service
所有類所有的DAO類。
它看起來像我的應用程序上下文的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 = "[email protected]";
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, "[email protected]", "gotze_mario", 1
);
Assert.assertNull(user.getId());
userService.create(user);
Assert.assertNotNull(user.getId());
}
}
請幫我解決這個問題的過程中拋出的異常的類。我很感激所有的建議!
什麼是例外發布全部細節 – Dileep
你的問題是什麼? – Rembo