1
由於某些原因,當我單獨使用@ComponentScan時,我的DAO未加載。彈簧集成測試加載帶註釋的豆
吾道:
@Repository
public class MyDAO{
@Autowire
private Datasource ds;
}
我的配置類(只覆蓋了嵌入式DB數據源豆):
@Configuration
public class TestConfig extends AppConfig {
@Bean
public Datasource getDatasource() throws Exception{...}
我的測試類:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
@ComponentScan
public class MyDAOTest {
@Autowired
private MyDAO target;
我的理解是, @ComponentScan應該能夠找到MyDAO並加載它(TestConfig類中的數據源加載得很好)。但是,MyDAO未被加載。我得到了「org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合條件的[com.acme.dao.MyDAO]類型的合格bean」。我也嘗試在@ComponentScan中指定包,例如com.acme或com.acme.dao。
當我在@ContextConfiguration中包含MyDAO.class作爲其中一個類時,它工作正常。但是,我不想列出集成測試的所有類...
'@ ComponentScan'屬於'@ Configuration'類,而不是'@ ContextConfiguration'類。參考http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html –
@DonBottstein好吧,那麼如何加載所有註釋的bean,只需重寫數據源爲我的集成測試? – user2793390
@DonBottstein好吧,我想我明白了。我將ComponentScan添加到我的TestConfig.class中,並且它現在似乎正在工作。謝謝。 – user2793390