スプリングはbeanを得られません.
3152 ワード
今日はspringユニットのテストをしていますが、beanがなかなかロードできませんでした.
エラー:
<context:component-scan base-package=「comp.springit」/>
user Serviceというbeanにロードできなくなりました.
テストコードは以下の通りです.
user Service:
エラー:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:694)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1168)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
at com.spring_test.UserManagerTest.before(UserManagerTest.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
丁未の過程で、springの構成に欠落していることが分かった.<context:component-scan base-package=「comp.springit」/>
user Serviceというbeanにロードできなくなりました.
テストコードは以下の通りです.
user Service:
public interface UserService {
public User getUserById(String userId);
}
UserServiceImplimport org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService{
public User getUserById(String userId) {
return null;
}
}
テストクラス:public class UserManagerTest {
private UserService userService;
@Before
public void before()
{
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"/spring/spring-config.xml"});
userService = (UserService) ac.getBean("userService");
}
@Test
public void testUser()
{
String userId ="001";
userService.getUserById(userId);
}
}
spring-config.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/>
<context:component-scan base-package="com.spring_test"/>
<!-- dbconfig.properties -->
<context:property-placeholder location="classpath:dbconfig.properties" />
<!-- ( ), me.gacl.service @Service -->
<context:component-scan base-package="me.gacl.service" />
</beans>