Java 一些质疑与这个Spring MVC项目架构相关。它不是在使用Spring功能吗?

Java 一些质疑与这个Spring MVC项目架构相关。它不是在使用Spring功能吗?,java,spring,spring-mvc,architecture,spring-data,Java,Spring,Spring Mvc,Architecture,Spring Data,我对SpringMVC还很陌生,我发现一个项目(在一个教程中找到)以一种奇怪的方式处理HibernateDAO,这与它有关 情况如下: 有两个XML配置文件,第一个是描述servlet映射的spring servlet.XML: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3

我对SpringMVC还很陌生,我发现一个项目(在一个教程中找到)以一种奇怪的方式处理HibernateDAO,这与它有关

情况如下:

有两个XML配置文件,第一个是描述servlet映射的spring 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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<mvc:annotation-driven/>

<context:component-scan base-package="com.demo.controllers"></context:component-scan>

    <bean id="viewresolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>

    <mvc:resources location="/WEB-INF/images/" mapping="/img/**"></mvc:resources>

    <mvc:resources location="/WEB-INF/css/" mapping="/css/**"></mvc:resources>

    <mvc:resources location="/WEB-INF/js/" mapping="/js/**"></mvc:resources>

</beans>
其中,RegisteryDAO类似于DAO工厂类:

public class RegisteryDAO {

    public static com.demo.dao.layer.ProductsDAO productsDAO;
    public static com.demo.dao.layer.UserDAO userDAO;

    static{
        productsDAO = new ProductsDAO();
        userDAO = new UserDAO();
    }

    public static com.demo.dao.layer.ProductsDAO getProductsDAO() {
        return productsDAO;
    }

    public static void setProductsDAO(com.demo.dao.layer.ProductsDAO productsDAO) {
        RegisteryDAO.productsDAO = productsDAO;
    }

    public static com.demo.dao.layer.UserDAO getUserDAO() {
        return userDAO;
    }

    public static void setUserDAO(com.demo.dao.layer.UserDAO userDAO) {
        RegisteryDAO.userDAO = userDAO;
    }

}
其中声明并创建了一个实现my DAO的新静态UserDAO对象,该对象:

public class UserDAO implements com.demo.dao.layer.UserDAO{

    public String doHibernateLogin(String username, String password){
        try{
            SessionFactory sessionFactory = HibernateConnection.doHibernateConnection();
            Session session = sessionFactory.openSession();

            session.beginTransaction();

            List<User> user = session.createQuery("From User where username='"+username+"' and password='"+password+"'").list();

            session.close();

            if(user.size() == 1) return "login success" ;
            else return "Please try again...";
        }
        catch(Exception e){
            return "Please try again...";
        }
    }

    public String doHibernateSignUp(User user){
        try{
            Session session = HibernateConnection.doHibernateConnection().openSession();
            session.beginTransaction();

            session.save(user);

            session.getTransaction().commit();
            session.close();
            return "Sign Up Successfully...";
        }
        catch(Exception e){
            e.printStackTrace();
            return "User is already there with this username";
        }
    }

    public User getUserByUsername(String username){
        try{
            Session session = HibernateConnection.doHibernateConnection().openSession();
            List<User> users = session.createQuery("From User where username = :username")
                                .setParameter("username", username).list();
            session.close();
            if(users != null && users.size() == 1){
                return users.get(0);
            }else{
                return null;
            }

        }
        catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
在配置了数据库连接的HibernateConnection类上调用doHibernateConnection()

public class HibernateConnection {

    public static SessionFactory sessionFactory;

    public static SessionFactory doHibernateConnection(){
        Properties database = new Properties();
        database.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        database.setProperty("hibernate.connection.username", "root");
        database.setProperty("hibernate.connection.password", "");
        database.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/spring");
        database.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

        Configuration cfg = new Configuration()
                            .setProperties(database)
                            .addPackage("com.demo.pojo")
                            .addAnnotatedClass(User.class)
                            .addAnnotatedClass(Products.class);

        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());

        sessionFactory = cfg.buildSessionFactory(ssrb.build());

        return sessionFactory;

    }

}
因此,我对Spring非常陌生,在架构方面没有太多经验,但在我看来,这个示例非常糟糕,因为它手动处理数据库配置和DAOs对象创建(它创建了一个工厂,但Spring依赖项注入是一个工厂)

所以在我看来,这是非常可怕的,因为这是一个Spring项目,但它没有使用最重要的Spring特性


是我的推理正确还是我遗漏了什么?(可能这很常见)

是的,你是对的,它没有利用Spring依赖注入。只是想澄清一下,
扫描bean创建。这整件事非常糟糕。让DAO捕捉异常和返回字符串是很糟糕的。我建议对daos使用Spring数据JPA,你是对的,它没有利用Spring依赖注入。只是想澄清一下,
扫描bean创建。这整件事非常糟糕。让DAO捕捉异常和返回字符串是很糟糕的。我建议对DAO使用Spring数据JPA
public class UserDAO implements com.demo.dao.layer.UserDAO{

    public String doHibernateLogin(String username, String password){
        try{
            SessionFactory sessionFactory = HibernateConnection.doHibernateConnection();
            Session session = sessionFactory.openSession();

            session.beginTransaction();

            List<User> user = session.createQuery("From User where username='"+username+"' and password='"+password+"'").list();

            session.close();

            if(user.size() == 1) return "login success" ;
            else return "Please try again...";
        }
        catch(Exception e){
            return "Please try again...";
        }
    }

    public String doHibernateSignUp(User user){
        try{
            Session session = HibernateConnection.doHibernateConnection().openSession();
            session.beginTransaction();

            session.save(user);

            session.getTransaction().commit();
            session.close();
            return "Sign Up Successfully...";
        }
        catch(Exception e){
            e.printStackTrace();
            return "User is already there with this username";
        }
    }

    public User getUserByUsername(String username){
        try{
            Session session = HibernateConnection.doHibernateConnection().openSession();
            List<User> users = session.createQuery("From User where username = :username")
                                .setParameter("username", username).list();
            session.close();
            if(users != null && users.size() == 1){
                return users.get(0);
            }else{
                return null;
            }

        }
        catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
SessionFactory sessionFactory = HibernateConnection.doHibernateConnection();
Session session = sessionFactory.openSession();
public class HibernateConnection {

    public static SessionFactory sessionFactory;

    public static SessionFactory doHibernateConnection(){
        Properties database = new Properties();
        database.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        database.setProperty("hibernate.connection.username", "root");
        database.setProperty("hibernate.connection.password", "");
        database.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/spring");
        database.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

        Configuration cfg = new Configuration()
                            .setProperties(database)
                            .addPackage("com.demo.pojo")
                            .addAnnotatedClass(User.class)
                            .addAnnotatedClass(Products.class);

        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());

        sessionFactory = cfg.buildSessionFactory(ssrb.build());

        return sessionFactory;

    }

}