sssscarlet 2019-05-18 01:36 采纳率: 0%
浏览 270
已结题

关于spring 注入为null的一些问题

这是一个基于SSH框架的网站系统其中的登录模块,结果在登录的时候userserviceimpl始终是null
这个是DAO实现

package dao;

import entity.User;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
    //增加用户
    @Override
    public void addUser(User user){
        getHibernateTemplate().save(user);
    }

    //查询验证用户是否存在
    @Override
    public User findUser(User user){
        User user1 = new User();
        String hql = "from User user where user.username='"
                + user.getUsername() + "' and user.password= '"
                + user.getPassword() + "'";
        //将查询出的结果放到List
        List<User> userlist = getHibernateTemplate().findByExample(user);
        //判断是否有查询结果,换句话说就是判断用户是否存在
        if(userlist.size()>0){
            //取出查询结果的第一个值
            user1 = userlist.get(0);
        }
        return user1;
    }
}

userService

package service;

import dao.UserDAOImpl;
import entity.User;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    //注入对象UserDAO
    private UserDAOImpl userdaoimpl;
    public void setUserDAOImpl(UserDAOImpl userdaoimpl) {
        this.userdaoimpl = userdaoimpl;
    }
    //保存用户信息
    @Override
    public void addUser(User user){
        this.userdaoimpl.addUser(user);
    }
    //查找验证用户信息
    @Override
    public boolean findUser(User user){
        User user1 = this.userdaoimpl.findUser(user);
        //DAO查询中已经判断当用户名密码都存在时才返回user1,这里只用判断用户名是否存在就行
        if(user1.getUsername()!=null){
            return true;
        }else{
            return false;
        }
    }
}

LoginAction

package action;

import com.opensymphony.xwork2.ActionSupport;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import service.UserService;
import service.UserServiceImpl;

public class LoginAction extends ActionSupport{
    private User user;
    public void setUser(){
        this.user=user;
}
    private UserServiceImpl userserviceimpl;
    public void setUserServiceImpl(UserServiceImpl userserviceimpl) {
        this.userserviceimpl = userserviceimpl;
    }
    @Override
    public String execute() throws Exception{
        boolean flag = userserviceimpl.findUser(user);
        if (flag) {
            return SUCCESS;
        } else {
            return INPUT;
        }
    }
}

配置文件


<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd ">

    <!-- dbcp连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" >
        <property name="driverClassName" value="com.mysql.jdbc.Driver">
        </property>
        <property name="url" value="jdbc:mysql://localhost:3306/user_db">
        </property>
        <property name="username" value="root">
        </property>
        <property name="password" value="1234">
        </property>
    </bean>

    <!--hibernate-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource">
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</prop>
                <prop key="hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autocommit">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>entity/User.hbm.xml</value>
            </list>
        </property>
    </bean>


    <!--使用属性注入-->
    <bean id="User" class="entity.User"></bean>
    <bean id="UserDAO" class="dao.UserDAOImpl" scope="singleton">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

    <bean id="UserService" class="service.UserServiceImpl" scope="prototype">
        <property name="userDAOImpl" ref="UserDAO"></property>
    </bean>

    <bean id="action.RegistAction" class="action.RegistAction" scope="prototype">
        <property name="UserServiceImpl" ref="UserService"></property>
    </bean>

    <bean id="action.LoginAction" class="action.LoginAction" scope="prototype">
    <property name="UserServiceImpl" ref="UserService"></property>
    </bean>


</beans>


在LoginAction中 userserviceimpl总是null

  • 写回答

3条回答 默认 最新

  • threenewbee 2019-05-18 09:14
    关注
    List<User> userlist = getHibernateTemplate().findByExample(user);
    ->
    List<User> userlist = getHibernateTemplate().find(hql);
    
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站