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