其他分享
首页 > 其他分享> > Spring 05: 用DI(依赖注入)优化Spring接管下的三层项目架构

Spring 05: 用DI(依赖注入)优化Spring接管下的三层项目架构

作者:互联网

背景

实现类

数据访问层

package com.example.dao;

import com.example.pojo.User;
import org.springframework.stereotype.Repository;

/**
 * 数据访问层的实现类
 */
@Repository
public class UserMapperImpl implements UserMapper{

    //模拟用户信息导入
    @Override
    public int insertUser(User user) {
        System.out.println("用户: " + user.getName() + ", 导入成功!");
        return 1;
    }
}

业务逻辑层

package com.example.Service.impl;

import com.example.Service.UserService;
import com.example.dao.UserMapper;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务逻辑层实现类
 */
@Service
public class UserServiceImpl implements UserService {

    //数据访问层接口指向数据访问层实现类
    @Autowired
    UserMapper userMapper;
    
    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }
}

界面层

package com.example.controller;

import com.example.Service.UserService;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * 界面层
 */
@Controller
public class UserController {
    
    //业务逻辑层接口指向业务逻辑层实现类
    @Autowired
    UserService userService;
    
    public int insertUser(User user){
        return userService.insertUser(user);
    }
}

applicationContext.xml

image

<?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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 添加包扫描 -->
    <context:component-scan base-package="com.example"/>
    
</beans>

包扫描的方式

    <context:component-scan base-package="com.example.controller"/>
    <context:component-scan base-package="com.example.Service"/>
    <context:component-scan base-package="com.example.dao"/>
    <context:component-scan base-package="com.example.dao com.example.Service com.example.controller"/>
    <context:component-scan base-package="com"/>

测试

package com.example.test;

import com.example.controller.UserController;
import com.example.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestInsert {
    
    //测试:注解改造后的Spring接管下的简单三层架构
    @Test
    public void testInsertUser(){
        //创建Spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器中获取UserController对象
        UserController uc = (UserController) ac.getBean("userController");
        //完成用户数据的导入
        uc.insertUser(new User("荷包蛋", 20, "黑河"));
    }
}

测试输出

用户: 荷包蛋, 导入成功!

Process finished with exit code 0

标签:05,Spring,DI,public,org,import,com,example
来源: https://www.cnblogs.com/nefu-wangxun/p/16609882.html