栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC:父子工厂划分(springmvc的耦合问题)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC:父子工厂划分(springmvc的耦合问题)

从功能角度没有存在问题,但是在设计角度存在缺陷,就是MVC层与其他层次存在耦合,

如果日后,替换了MVC的实现,整体的代码都将受到影响,如果日后把SpringMVC换成Strus2,Webflat的话,换了MVC不仅仅影响MVC的内容,也影响到其他层次

通过两个工厂,解决MVC层与非MVC层之间的耦合

 

父容器:

子工厂:

diapatcher.xml:




    
    
    

    

    
    
        
        
    

在web.xml中添加父容器ContextLoadListener创建工厂 :

web.xml:






    
        org.springframework.web.context.ContextLoaderListener
    

    
        contextConfigLocation
        classpath:applicationContext.xml
    

    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        encodingFilter
        /*
    





    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
    
        contextConfigLocation
        classpath:dispatcher.xml
    
    
    1



    dispatcherServlet
    /

    

父工厂:

applicationContext.xml:




    


    
    
        
        
        
        
    

    
    
        
        
        
        
        
        
            
            
                classpath:com.baizhiedu.mapper/*Mapper.xml
            
        
    

    
    
        
        
    

    
    
        
    

    
    

User:

package com.baizhiedu.entity;

public class User {
    private Integer id;
    private String name;
    private String password;

    public User() {
    }

    public User(Integer id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

UserDAO:

package com.baizhiedu.dao;

import com.baizhiedu.entity.User;

public interface UserDAO {
    public void save(User user);
}

UserDAOMapper.xml:




    
    
    
        insert into tb_users(name,password) values (#{name},#{password})
    

UserService:

package com.baizhiedu.service;

import com.baizhiedu.entity.User;

public interface UserService {
    public void register(User user);
}

UserServiceImpl:

package com.baizhiedu.service;

import com.baizhiedu.dao.UserDAO;
import com.baizhiedu.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service //使用@Service注解创建这个类的对象
@Transactional //使用注解,添加事务属性
public class UserServiceImpl implements UserService {

    @Autowired //注入userdao
    private UserDAO userDAO;

    @Override
    public void register(User user) {
        userDAO.save(user);
    }
}

控制器:

UserController:

package com.baizhiedu.controller;

import com.baizhiedu.entity.User;
import com.baizhiedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public String register(User user){
        userService.register(user);
        return "regOk";
    }
}

reg.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/4
  Time: 18:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    


  
UserName:
Password:

regOk.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/4
  Time: 18:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    


   RegOK


现在的ssm整合开发父子容器处理过程当中是没有事务的,下面打印没有输出create new Transction开启事务

 

解决:在包扫描上子容器不扫描com.baizhiedu这个包,只让他扫描com.baizhiedu.Controller,这样他就不会创建Service对象,他就会往父容器里面找Service

 

 让zi工厂只创建Controller:

diapatcher.xml:




    

    
    

    

    
    
        
        
    

让父容器排除创建Controller:




    
        
        
    


    
    
        
        
        
        
    

    
    
        
        
        
        
        
        
            
            
                classpath:com.baizhiedu.mapper/*Mapper.xml
            
        
    

    
    
        
        
    

    
    
        
    

    
    

从新运行,添加上了事务:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/952828.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号