Spring @autowired没有加载bean - 抛出NULL指针

时间:2015-01-17 23:15:40

标签: spring web

我有一个非常简单的项目设置。 春季4.14 泽西岛2.15

Web.xml中

<servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.talentera</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

出于某种原因,@ Autowired无法正常工作或者没有扫描bean。 以下是我的项目设置: enter image description here

以下是弹簧配置:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd    
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task-3.2.xsd">


<context:annotation-config />
<context:component-scan base-package="com.talentera">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Component" />
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Service" />
</context:component-scan>

以下是用法:

 @Component
    @Path("/user")
    @Scope("request")
    public class UserRestService extends BaseAPIService{

        @Autowired
        UserManagementService userManagementService;
        protected static final Log logger = LogFactory.getLog( UserRestService.class.getName() );

        EmailFormatValidator emailValidator = new EmailFormatValidator();



        public UserManagementService getUserManagementService() {
            return userManagementService;
        }

        public void setUserManagementService(UserManagementService userManagementService) {
            this.userManagementService = userManagementService;
        }

     @GET
        @Path("/get/{userId}")
        @Produces(MediaType.APPLICATION_JSON)
        public String getUser(@PathParam("userId") Integer userId)
        {
            System.out.println(getSerRequest().getSession().getId());
            User u = userManagementService.getUserById(userId);
            response.setData(u);
            return jsonStringifyResponse();
        }

package com.talentera.common.service.impl;


imports,,,,

    @Service("userManagementService")
    @Transactional(rollbackFor = Exception.class)
    @SuppressWarnings("unchecked")
    public class UserManagementServiceImpl extends AbstractService<Identifiable> implements UserManagementService
    {

这会引发NULL指针异常,因为userManagementService为NULL。

请帮助我了解你的发现。在此先感谢。

1 个答案:

答案 0 :(得分:0)

似乎将此添加到您的xml可能会解决问题。如果您从特定的注释列表中进行选择,则还需要在其中添加自动连接的注释。

<context:include-filter type="annotation" expression="org.springframework.stereotype.Autowired" />