我正在尝试创建休息Web服务来处理Android应用程序的CRUD操作。我正在使用Spring 4.0和Hibernate。我正在尝试autoire dao,它总是空的,我无法找出问题的原因。我在网上搜索,一切看起来都对我,所以我完全失去了。我在applicationContext中定义了bean,正如我在许多教程中看到的那样但是我无法将dao用于autowire。任何帮助,将不胜感激。
调度-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>
userDAO的
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);
}
}
UserWebService
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();
}
}
}
更新:
我用
修改了UserWebServiceprivate 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应用程序时,会调用此用户并查找用户,但是当我从客户端调用它时,它始终为null。这可能是因为我直接打电话给服务而不是经历春天?如果是这样,我可以指出从客户端调用服务的创建方式。感谢
答案 0 :(得分:0)
从applicationContext.xml文件中删除<bean id="userDao" class="com.bike.party.daos.UserDaoImpl" />
bean。
我认为您正在注入此实例而不是通过@Repository
注释创建的实例,这就是您的SessionFactory
为空的原因
答案 1 :(得分:0)
我相信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()
答案 2 :(得分:0)
尝试从SpringBeanAutowiringSupport派生您的UserWebService。