0
这里是我的问题:我正在使用SpringMVC,并且在调用@Autowired依赖时遇到了NullPointerException。依赖注入在Spring MVC中抛出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例外。当跟踪日志时,我看到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>
为期两天的搜索,我不能找到任何帮助后。我错过了什么吗?
THX
两者都已经存在。事实上,这个bean是由Spring管理的。 – Piccolomomo
请发布您的applicationContext xml文件。 –
问题更新,但该文件是非常基本的。 – Piccolomomo