Я создал интерфейс и класс:Почему весна не может найти мою фасоль?
public interface UserService {
List<User> listAll();
}
@Transactional
public class DefaultUserService implements UserService {
private String tableName;
public List<User> listAll() { someDao.listAllFromTable(tableName); }
public void setTableName(String tableName) { this.tableName = tableName; }
}
Кроме того, в моем контекстном XML файла context.xml
, я определил:
<bean id="userService" class="mypackage.DefaultUserService">
<property name="tableName" value="myusers" />
</bean>
Тогда я хочу, чтобы проверить DefaultUserService
:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:context-test.xml"})
@TransactionConfiguration(transactionManager = "testTransactionManager")
@Transactional
public class UserServiceTest {
@Autowired
private DefaultUserService userService;
@Before
public void setup() {
userService.setTableName("mytesttable");
}
@Test
public void test() {
// test with userService;
userService.listAll();
}
}
Обратите внимание, что он использует context-test.xml
, который импортировал оригинал context.xml
:
<import resource="classpath:context.xml"/>
К сожалению, когда начинается тест, весна бросает исключение:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'mypackage.UserServiceTest':
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field:
private mypackage.DefaultUserService mypackage.userService
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [mypackage.DefaultUserService] 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)}
Я не уверен, где это неправильно, почему весна не может найти компонент DefaultUserService
я определил?
Параметра '' декларации у вас есть в 'context-test.xml'? –
Вы имели в виду 'public class DefaultUserService' в этом первом кодеблоке? – Dan
@Sotirios, см. Мой обновленный вопрос, у меня есть эта декларация. – Freewind