[英]Spring doesn't instantiate bean at start
I'm stuck with the following problem. 我陷入了以下问题。 My application doesn't create my Services at startup and I end up with a
NullPointerException
when I try to use them in my Maven Tests after Inject them with @Autowired
. 我的应用程序在启动时不会创建我的服务,并且当我在使用
@Autowired
注入Maven测试中尝试使用它们时,我最终遇到了NullPointerException
@Autowired
。
I don't understand where it comes from. 我不知道它从哪里来。 I have done some research but I still don't understand why it doesn't work.
我已经做了一些研究,但我仍然不明白为什么它不起作用。
Here is the class where my Autowired administrationActionService
is null: 这是我的自动装配
administrationActionService
为null的类:
public class AdministrationActionTests extends EntityTests {
@Autowired
AdministrationActionService administrationActionService;
@Test
public void equalsTests() {
administrationActionService.getExample();
[...]
The class : 班级 :
package com.bloombooking.services;
@org.springframework.stereotype.Service
public class AdministrationActionService extends ServiceEntity{
@Autowired private AdministrationActionDao administrationActionDao;
[...]
And my ApplicationContext.xml
. 还有我的
ApplicationContext.xml
。 I've placed it in src/ApplicationContext.xml
我已经将它放在
src/ApplicationContext.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:component-scan base-package="com.bloombooking.services, com.bloombooking.dao"/>
</beans>
I really don't know what I could have done wrong. 我真的不知道我做错了什么。 Can someone help me?
有人能帮我吗? Thanks!
谢谢!
To make it work you have to make the following changes: 要使其正常工作,您必须进行以下更改:
@RunWith(SpringJUnit4ClassRunner.class)
to your class. @RunWith(SpringJUnit4ClassRunner.class)
添加到您的类中。 @ContextConfiguration("path_to_you_spring_beans.xml")
@ContextConfiguration("path_to_you_spring_beans.xml")
So your class becomes: 因此,您的课程变为:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("path_to_you_spring_beans.xml")
AdministrationActionTests {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.