Spring MVC 中的依赖注入抛出 NullPointerException
这是我的问题:我正在使用 SpringMVC,并且在调用 @Autowired 依赖项时出现 NullPointerException 。
这是@Service:
package x.y.z.service
@Repository("myService")
public class MyServiceImpl implements MyService {
/* ... */
}
该服务在应用程序中的任何地方都会自动装配,没有任何问题,除了这里:
package x.y.z.utils
@Component
public class TestClass {
@Autowired
private MyService myService;
public TestClass() {
super();
}
public void tester() {
myService.find(1);
}
}
方法 tester()
在 myService
上抛出 NP 异常。当跟踪日志时,我发现该 bean 由 Spring 管理。
应用程序上下文文件非常简单:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<annotation-driven />
<interceptors>
<!-- ... -->
</interceptors>
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Imports user-defined @Controller beans that process client requests -->
<beans:import resource="controllers.xml" />
<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</beans:beans>
和引用的controllers.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Scans within the base package of the application for @Components to
configure as beans -->
<context:component-scan base-package="x.y.z" />
<context:annotation-config />
</beans>
经过两天的搜索,我找不到任何帮助。我错过了什么吗?
谢谢
here is my problem: I'm working with SpringMVC and I get a NullPointerException on calling an @Autowired dependency.
Here is the @Service:
package x.y.z.service
@Repository("myService")
public class MyServiceImpl implements MyService {
/* ... */
}
The service is autowired everywhere in the application without problems, except here:
package x.y.z.utils
@Component
public class TestClass {
@Autowired
private MyService myService;
public TestClass() {
super();
}
public void tester() {
myService.find(1);
}
}
where the method tester()
throws the NP Exception on myService
. When tracing the logs I see that the bean is managed by Spring.
The application context file is pretty simple:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<annotation-driven />
<interceptors>
<!-- ... -->
</interceptors>
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Imports user-defined @Controller beans that process client requests -->
<beans:import resource="controllers.xml" />
<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</beans:beans>
And the referenced controllers.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Scans within the base package of the application for @Components to
configure as beans -->
<context:component-scan base-package="x.y.z" />
<context:annotation-config />
</beans>
After a two days search I can't find anything helping. Am I missing something?
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请确保您有
此外,请添加:
更新:
很抱歉,您的 xml 在架构方面不正确,并且缺少我告诉您添加的内容:
这就是您的文件的方式应该看起来像:
Please make sure that you have
In addition, please add:
Update:
I'm sorry but your xml is incorrect in terms of schema and it is missing what I told you to add:
This is how your file should look like: