1

我对春天有以下问题。我有一个 webapp 和一个域项目。域项目包含一个 studentService,它应该通过 webapp 的类中的自动装配注入。我已将 和 添加到 appcontext.xml。

这是来自 webapp 的类:

@Component
public class JSONToDomainObjects
{

@Autowired
private StudentService studentService;

private void bindSubmissionValuesToDomainObjects(Integer userKey) throws Exception
{
 Student student =  studentService.getStudentBySlNumber(userKey);
}
}

然后是学生服务:

@Service
public class StudentService
{
  ..
}

因此,一旦我启动我的应用程序,我就会看到 studentService 为空,但是当我获取 appcontext 并调用 getBean("studentService") 方法时,就会返回一个 studentservice 实例。我使用弹簧 3.0.5。有人知道自动装配失败的原因吗?

干杯,

迈克尔

4

2 回答 2

2

你为什么不在你的测试类中使用依赖注入呢?像这样的东西:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"appcontext.xml"})
public final class JSONToDomainObjectsTests {
    private StudentService service;

    @Autowired
    public void setService(StudentService service) {
        this.service= service;
    }

    @Test
    public void testJSONToDomain() {
        service.foo();
    }
}
于 2011-04-11T09:44:29.277 回答
1

您是否<context:annotation-config/>在 appcontext.xml 中使用?

于 2011-04-07T09:44:12.623 回答