Java spring autowire不工作,返回null
我现在在学春天。这是我的示例代码。我正在使用jersey、spring、hibernate和mysql来提供REST服务Java spring autowire不工作,返回null,java,spring,autowired,Java,Spring,Autowired,我现在在学春天。这是我的示例代码。我正在使用jersey、spring、hibernate和mysql来提供REST服务 CustomerServiceImpl.java这是REST端点(部分代码) 下面是CustomerBOImpl.java(部分代码) CustomerDAOImpl.class 包com.samples.DAO.impl @Repository public class CustomerDAOImpl implements CustomerDAO { 这是applicat
CustomerServiceImpl.java
这是REST端点(部分代码)
下面是CustomerBOImpl.java
(部分代码)
CustomerDAOImpl.class
包com.samples.DAO.impl
@Repository
public class CustomerDAOImpl implements CustomerDAO {
这是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"
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/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-3.1.xsd">
<context:property-placeholder location="classpath*:database.properties"/>
<context:component-scan base-package="com.samples"/>
<context:annotation-config />
</beans>
这是web.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Employee Service</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml
</param-value>
</context-param>
<context-param>
<param-name>initializeContextOnStartup</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.samples.service</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
员工服务
上下文配置位置
classpath*:applicationContext.xml
初始化EconTextOnStartup
真的
org.springframework.web.context.ContextLoaderListener
泽西塞尔维特酒店
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
com.samples.service
com.sun.jersey.api.json.POJOMappingFeature
真的
1.
泽西塞尔维特酒店
/*
因此,如果我理解了这是如何正确工作的,那么我就是配置xml
,通过提供要运行autoscan的包来自动屏蔽我的组件。对于我想要自动连线的对象。在CustomerServiceImpl
类中,我为customerBO
对象使用@autowired,该对象应该由CustomerBOImpl.class定义上的@Component annotation扫描。你能帮我解释一下为什么我的自动扫描没有拾取自动连线的customerBO对象吗?
谢谢。Spring只知道自动关联已标记为bean的字段。从您的代码
CustomerDAO
不是一个bean。例如,要在xml配置文件中将其定义为bean,需要添加
在xml文件中。
或者使用@Component
注释类,或者在@Configuration
类中使用@Bean
定义类。我怀疑问题在于您的CustomerServiceImpl
类在Spring之外被servlet容器实例化,或者显式地在您的代码中实例化。您需要让Spring实例化它(通过使它成为bean
等),或者使用
。如果servlet容器实例化对象,我不确定后者是否有效,因为它可能取决于事情发生的顺序…Jersey 2,现在支持Spring集成
泽西岛文件:
博客:
示例应用程序:
然而,在撰写本文时,不能通过使用SpringXML配置将Springbean直接注入JAX-RS类。必须使用注释(
@Autowired
等)。我也有同样的问题。我在资源类中注入的bean总是以NULL
的形式出现
我花了两天时间试图弄明白为什么不能将Springbean注入JAX-RS资源类。然后,我在中找到以下内容:
“限制:
Springbean不能通过使用
SpringXML配置“
注意:我在项目中使用了Jersey 2和Spring 3.x。我使用的是Spring和Jersey。所以我认为泽西岛应该寻找@Path来寻找资源?是的,我是。完整的实现使用maven、spring、jersey、hibernate、c3p0、mysql。但对我来说,这仍然是一种简单的举重运动。这是一个简单的REST服务。还记得必须这样做吗?看看这个,你是如何实例化
CustomerServiceImpl
?如果Spring无法自动关联依赖项,它应该抛出一个异常…chris,我正在使用jersey,所以我正在使用jersey的@Path annotation来获取资源。我在日志中没有看到任何autowire依赖项错误。只是自动连线bean实际上是空的,这是我的问题。谢谢你的评论。我没有发布CustomerDAOImpl代码,但它有@Component注释。问题实际上发生在更早的时候,CustomerBOImpl的自动连接在CustomerServiceImpl上失败。(刚刚用CustomerDAOImpl代码更新了主要问题)如果可能的话,我也尽量避免在xml中定义bean。谢谢你的评论。看看reimeus在上面提供的链接,我想你可能是对的。我用的是spring和jersey。我已经按照上述解决方案的建议在web.xml中使用了SpringServlet,并且已经正确地排除了jersey spring依赖项。但autowire仍将为空。将继续围绕此解决方案进行操作。
<?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"
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/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-3.1.xsd">
<context:property-placeholder location="classpath*:database.properties"/>
<context:component-scan base-package="com.samples"/>
<context:annotation-config />
</beans>
http-bio-8090-exec-1] [class: CustomerServiceImpl] INFO - list all customers
[http-bio-8090-exec-1] [class: CustomerServiceImpl] INFO - customerBO is null
May 08, 2014 10:55:29 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at com.samples.service.impl.CustomerServiceImpl.getAllCustomers(CustomerServiceImpl.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvo
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Employee Service</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml
</param-value>
</context-param>
<context-param>
<param-name>initializeContextOnStartup</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.samples.service</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>