Spring Autowire无法正常工作返回null [英] spring autowire is not working returning null

查看:64
本文介绍了Spring Autowire无法正常工作返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习春天.这是我的示例代码.我将jersey,spring,hibernate和mysql用于我的REST服务.

CustomerServiceImpl.java是REST端点(部分代码)

package com.samples.service.impl;

@Path("customers")
public class CustomerServiceImpl implements CustomerService {

    private static Logger logger = Logger.getLogger(CustomerServiceImpl.class);

    @Autowired
    CustomerBO customerBO;

这是CustomerBOImpl.java(部分代码)

package com.samples.BO.impl;

@Component
public class CustomerBOImpl implements CustomerBO {

    @Autowired
    CustomerDAO customerDAO;

    @Autowired
    CustomerAdapter customerAdapter;

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>    

这是我遇到的例外情况的前几行.

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

这是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>

因此,如果我了解这是如何正常工作的,则configuring xml通过提供要在其下运行自动扫描的软件包对我的组件进行自动扫描.对于我想要自动连线的对象.在CustomerServiceImpl类中,我将@autowired用于customerBO对象,该对象应该已经被CustomerBOImpl.class定义上的@Component注释扫描过.您能帮我为什么我的自动扫描没有拾取自动连接的customerBO对象吗? 谢谢.

我怀疑问题是您的CustomerServiceImpl类是在Servlet容器之外还是在您的代码中显式地在Spring外部实例化的.您需要让Spring实例化它(通过使其成为bean等)或使用<context:spring-configured/>.我不确定如果servlet容器实例化该对象,后者是否会起作用,因为它很可能取决于事物发生的顺序...

i'm learning spring now. here's my sample code. i'm using jersey, spring, hibernate, and mysql for my REST service.

CustomerServiceImpl.java which is REST endpoint (partial code)

package com.samples.service.impl;

@Path("customers")
public class CustomerServiceImpl implements CustomerService {

    private static Logger logger = Logger.getLogger(CustomerServiceImpl.class);

    @Autowired
    CustomerBO customerBO;

here is CustomerBOImpl.java (partial code)

package com.samples.BO.impl;

@Component
public class CustomerBOImpl implements CustomerBO {

    @Autowired
    CustomerDAO customerDAO;

    @Autowired
    CustomerAdapter customerAdapter;

CustomerDAOImpl.class package com.samples.DAO.impl;

@Repository
public class CustomerDAOImpl implements CustomerDAO {

this is 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>    

this is first few lines of exception i'm getting.

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

this is 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>

so if i'm understanding how this works correctly, i'm configuring xml to have my components autoscanned by providing the package under which i want to run autoscan. and for objects that i want to autowire. in CustomerServiceImpl class, i use @autowired for customerBO object which should have been scanned by @Component annotation on CustomerBOImpl.class definition. can you please help why my auto-scan is not picking up autowired customerBO object? thanks.

解决方案

I suspect the problem is your CustomerServiceImpl class is being instantiated outside of Spring, either by the servlet container or explicitly in your code. You need to either have Spring instantiate it (by making it a bean, etc) or use <context:spring-configured/>. I'm not sure if the latter will work if the servlet container instantiates the object as it will likely depend on the order in which things occur...

这篇关于Spring Autowire无法正常工作返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆