【问题标题】:Spring autowired dao is nullSpring autowired dao 为空
【发布时间】:2014-11-26 01:34:00
【问题描述】:

我正在尝试创建 REST Web 服务来处理 Android 应用程序的 CRUD 操作。我正在使用带有 Hibernate 的 Spring 4.0。我正在尝试自动连接一个 dao,它始终为空,我无法找出问题的原因。我在网上搜索,一切对我来说都是正确的,所以我完全迷路了。正如我在许多教程中看到的那样,我在 applicationContext 中定义了 bean,但我无法让 dao 自动装配。任何帮助,将不胜感激。

dispatch-servlet.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <context:annotation-config />
    <mvc:annotation-driven />
    <context:component-scan base-package="com.bike.party.services, com.bike.party.daos" />
  <tx:annotation-driven transaction-manager="transactionManager"  />

</beans>

applicationContext.xml

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

   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="propertyConfigurer" p:location="/WEB-INF/jdbc.properties"/>

<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource" p:driverClassName="${jdbc.driverClassName}" p:password="${jdbc.password}" p:url="${jdbc.url}" p:username="${jdbc.username}"/>

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>

            </props>
        </property>
        <property name="packagesToScan" value="com.bike.party.models"/>
    </bean>

    <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager" p:sessionFactory-ref="sessionFactory" />    

    <tx:annotation-driven/>
</beans>

用户道

package com.bike.party.daos;

import com.bike.party.models.User;

public interface UserDao extends Dao<User> {

    public User findByUserName(String username);
}

UserDaoImpl

package com.bike.party.daos;

import com.bike.party.models.User;
import java.util.List;
import javax.transaction.Transactional;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

@Repository
@Transactional
@Qualifier("userDaoImpl")
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public User findByUserName(String username) {
        List<User> userList = sessionFactory.getCurrentSession().createCriteria(User.class).add(Restrictions.eq("username", username)).setMaxResults(1).list();
        return userList.isEmpty() ? null : userList.get(0);
    }
}

用户网络服务

package com.bike.party.services;

import com.bike.party.daos.UserDao;
import com.bike.party.models.User;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Provider
@Component
@Path("userservice")
public class UserWebService {

    @Autowired
    private UserDao userDao;

    @GET
    @Path("/user/{username}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getUser(String username) {
        if (userDao == null) {
            return "the dao is null";
        }

        User user = userDao.findByUserName(username);
        if (user == null) {
            return "No user was found.";
        } else {
            return user.getUsername();
        }
    }
}

更新:

我用

修改了 UserWebService
private UserDao userDao;

    @Autowired
    public void setUserDao(UserDao userDao) {
        if (functionDao != null) {
            System.out.println(String.valueOf(userDao.findByUserName("chris")));
        } else {
            System.out.println("setting the dao is null :(");
        }
        this.userDao= userDao;
    }

在部署 Web 应用程序时,它会被调用并找到用户,但是当我从客户端调用它时,它始终为空。这可能是因为我直接调用服务而不是通过 spring 吗?如果是这样,我可以指出从客户端调用服务的创建方式。谢谢

【问题讨论】:

  • 您是否将 Spring 与您的 JAX RS 实现正确集成?
  • 考虑将该代码减少到最小的可重现问题。
  • 数据源在哪里声明?
  • 将它们添加到上面的代码中。
  • 尝试在autowired setUserDao 方法上方添加@Qualifier("userDaoImpl")。

标签: java spring web-services hibernate spring-mvc


【解决方案1】:

我相信 spring 找不到你的 UserDao 类。

尝试将 dispatch-servlet.xml 中的 component-scan 指令更改为此:

<context:component-scan base-package="com.bike.party.*" />

如果您在项目中使用Spring Tool Suite,所有@Component 都应标记为组件(带有S的图标):

编辑: 从 xml 中删除 bean 定义,不要混合 xml 和带注释的配置。 还要从您的注释中删除 "userDaoImpl"。将@Repository("userDaoImpl") 更改为@Repository()

【讨论】:

  • 此更改后仍为空。
  • 在上面的代码中进行了更改,但仍然没有更改。我发现另一个有类似问题的here。我对提到的 web.xml 进行了更改,但仍然遇到同样的问题。
【解决方案2】:

从 applicationContext.xml 文件中删除 &lt;bean id="userDao" class="com.bike.party.daos.UserDaoImpl" /&gt; bean。

我认为您正在注入此实例,而不是通过 @Repository 注释创建的实例,这就是您的 SessionFactory 为空的原因

【讨论】:

  • 我删除了applicationContext中的bean,它仍然是null。
  • 这里是 github 上示例 jersey + spring hello world 项目的修改版本。我基本上已经将 DAO 和 ORM 层添加到现有的 webapp 中,它们都被正确注入了:github.com/stringy05/jersey/tree/master/examples/…
【解决方案3】:

尝试从 SpringBeanAutowiringSupport 派生您的 UserWebService。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多