有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java一些疑问与这个Spring MVC项目架构的架构有关。它不是在使用Spring功能吗?

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

情况如下:

有两个XML配置文件,第一个是SpringServlet。描述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>

第二个是spring安全性。包含Spring安全设置的xml。没有类似于根上下文的内容。xml(或applicationContext.xml)文件,其中包含webapp中所有servlet之间共享的bean

顺便说一下,正如您所看到的,在我的XML配置中没有Hibernate配置或DAOBeans声明

要通过以下方式配置\使用DAOs类,这是一种控制方法(处理注册fomr,用户可以在系统中注册新帐户):

@RequestMapping(value="/signup" , method=RequestMethod.POST)
public ModelAndView doSignUpProcess(HttpServletRequest request)
{
    ModelAndView mav = new ModelAndView("signup");
    String message = "";

    if(ServletFileUpload.isMultipartContent(request))
    {
        try
        {
        List<FileItem> data = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

        String username = data.get(0).getString();
        String password = data.get(1).getString();
        String repassword = data.get(2).getString();
        String gender = data.get(3).getString();
        String vehicle = data.get(4).getString();
        String country = data.get(5).getString();

        String image = new File(data.get(6).getName()).getName();

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setGender(gender);
        user.setCountry(country);
        user.setVehicle(vehicle);
        user.setImage(image);

        if(password.equals(repassword))
        {
            /*Signup_Model sm = new Signup_Model();

            //message = sm.doSignUp(username, repassword, gender, vehicle, country, image);
            message = sm.doHibernateSignUp(user);*/
            message = RegisteryDAO.getUserDAO().doHibernateSignUp(user);
            String path = request.getSession().getServletContext().getRealPath("/") + "//WEB-INF//images//";

            data.get(6).write(new File(path + File.separator + image));

        }
        else
        {
            message = "Password does not match..please try again";
        }

        }
        catch(Exception e)
        {
            System.out.println(e);
            message = "Please try again....";
        }
    }
    mav.addObject("message", message);
    return mav;
}

正如您在前面的方法中所看到的,要在数据库上存储新的User对象,它可以这样做:

message = RegisteryDAO.getUserDAO().doHibernateSignUp(user);

其中,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;
        }
    }

}

正如您在这个UserDAO类中所看到的,Hibernate SessionFactory(用于检索Hibernate会话以将对象存储在DB上)是这样检索的:

SessionFactory sessionFactory = HibernateConnection.doHibernateConnection();
Session session = sessionFactory.openSession();

在配置了数据库连接的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特性

是我的推理正确还是我遗漏了什么?(也许这很平常)


共 (0) 个答案