【发布时间】:2013-11-02 18:18:32
【问题描述】:
我开始使用 Spring DI,但我正在努力解决依赖注入问题,更糟糕的是,我什至不知道为什么,因为这对我来说似乎没问题。希望你们能帮助我!
问题是注释为@Autowired的属性总是null
我有几个具有 Maven 结构的项目:
- com.diegotutor.lessondeliver
- com.diegotutor.utility
我在 Tomcat 7 上运行示例
我在 pom.xml 中使用了以下依赖项:
- spring-context 3.2.4
- 弹簧网 3.2.4
- 球衣服务器 1.17.1
- jersey-core 1.17.1
- jersey-servlet 1.17.1
简单的想法是让 RESTful 服务通过依赖注入能够打印出位于以下配置文件中的属性值:D:\configuracion.conf.
在 com.diegotutor.utility 我有以下界面:
package com.diegotutor.utility;
public interface ConfigService {
public String getProperty(final String propertyName);
}
实施者:
package com.diegotutor.utility.impl;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Properties;
import com.diegotutor.utility.ConfigService;
public class PropertyFileConfigService implements ConfigService{
Properties prop;
public PropertyFileConfigService (final InputStream input) throws IOException {
if(input == null) {
throw new IllegalArgumentException("Input stream can't be null");
}
prop = new Properties();
prop.load(input);
}
public PropertyFileConfigService (final String fileName) throws IOException {
final FileInputStream input = new FileInputStream(fileName);
prop = new Properties();
prop.load(input);
}
public PropertyFileConfigService(final Reader input) throws IOException {
prop = new Properties();
prop.load(input);
}
public String getProperty(final String propertyName) {
return prop.getProperty(propertyName);
}
}
在 com.diegotutor.lessondeliver 我有 RESTful 服务,我想在其中使用 ConfigService 的注入实例:
package com.diegotutor.lessondeliver;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.diegotutor.utility.ConfigService;
@Path("/")
@Component
public class HelloWorld {
private static final Log log = LogFactory.getLog(HelloWorld.class);
@Autowired
private ConfigService configService;
@Path("/helloworld")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello() {
String host = configService.getProperty("host");
return "Hello World! HOST" + host;
// configService IS NULL!!
//SO IT THROWS A NULLPOINTER EXCEPTION WHEN INVOKING getProperty ON IT
}
}
最后在 /com.diegotutor.lessondeliver/src/main/webapp/WEB-INF/service-beans.xml 我有以下 XML 应用程序上下文文件,我使用 ConfigService (PropertyFileConfigService) 的实现在其上注入配置文件读取的路径:
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="configService" class="com.diegotutor.utility.impl.PropertyFileConfigService">
<constructor-arg type="java.lang.String"
value="D:\configuracion.conf" />
</bean>
<context:component-scan base-package="com.diegotutor" />
</beans>
显然我在这个com.diegotutor.lessondeliver web 应用程序的web.xml 中指定了我想要service-beans.xml 作为ConfigLocation 和一个监听器ContextLoaderListener,并且RESTful 服务依赖于ServletContainer p>
如果我指定 context:component-scan 以按照建议 here 查找 com.diegotutor 中的组件,并且我通过不使用建议 here 的任何新语句来强制通过 Spring 创建对象,为什么我将带注释的 configService 设为空?为什么 Spring 无法注入 com.diegotutor.utility.impl.PropertyFileConfigService 的实例?
任何帮助将不胜感激!
谢谢
编辑:
按照要求,我的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>com.diegotutor.lessondeliver</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/service-beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
【问题讨论】:
-
请发布您的 web.xml。
-
Sotirios,感谢您的快速回复。我现在已经添加了 web.xml
-
我不相信您看到的
HelloWorld对象是 Spring 生成的对象,因此不会注入该字段。您可能想查看 this 以了解一些 Spring-Jersey 集成。 -
太好了,我不知道 Jersey 和 Spring 可能会发生冲突,需要集成,但是如果 Jersey 正在实例化另一个完全不知道 Spring @Component 的 HelloWorld,这确实可以解释这个问题。我会试试你说的。非常感谢!
标签: spring dependency-injection nullpointerexception jersey autowired