只要配置了上下文組件掃描,只要使用@Component
註釋就可以創建spring bean是否正確?沒有xml bean定義的Spring組件檢測
使用Spring 3.0.5與Java 6
我的測試情況是:
@ContextConfiguration(locations={"classpath:spring-bean.xml"})
public class ServerServiceUnitTest extends AbstractJUnit4SpringContextTests {
@Autowired
private ServerService serverService;
@Test
public void test_server_service() throws Exception {
serverService.doSomething();
//additional test code here
}
}
的spring-bean.xml
文件包含:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>
我的班級我想成爲一個bean是:
@Component("ServerService")
public class ServerServiceImpl implements ServerService {
private static final String SERVER_NAME = "test.nowhere.com";
//method definitions.....'
}
這應該不足以讓Spring實例化ServerService
bean並執行自動裝配嗎?
我得到的錯誤是:
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:[serversystem.ServerService]找到依賴性否類型的匹配豆:預期至少1豆,其有資格作爲此依賴項的自動導向候選項。依賴註釋:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
我確定我錯過了一些簡單的東西。
完美。謝謝! – wadesworld