zoukankan      html  css  js  c++  java
  • set注入与构造器注入

    spring

    set注入与构造器注入

    1、配置依赖

        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.3.9</version>
            </dependency>
        </dependencies>
    

    2、编写dao.UserDao类

    package com.xiang.dao;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/9/22 10:55
     */
    public interface UserDao {
        String show();
    }
    

    3、编写dao.impl.UserDaoimpl1/UserDaoimpl2类

    package com.xiang.dao.impl;
    
    import com.xiang.dao.UserDao;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/9/22 10:56
     */
    public class UserDaoImpl1 implements UserDao {
        @Override
        public String show() {
            return "查询用户信息1";
        }
    }
    
    package com.xiang.dao.impl;
    
    import com.xiang.dao.UserDao;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/9/22 10:56
     */
    public class UserDaoImpl2 implements UserDao {
        @Override
        public String show() {
            return "查询用户信息2";
        }
    }
    

    4、编写service.UserService类

    package com.xiang.service;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/9/22 10:57
     */
    public interface UserService {
    String show();
    }
    

    5、编写service.UserServiceImpl类

    package com.xiang.service;
    
    import com.xiang.dao.UserDao;
    import com.xiang.dao.impl.UserDaoImpl1;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/9/22 10:57
     */
    public class UserServiceImpl implements  UserService{
    //    private UserDaoImpl1 userDaoImpl1 = new UserDaoImpl1();
        private UserDao userDao;
    
        /**
         * set 注入
         * @param userDao
         */
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        /**
         * 构造方法
         * 构造器 注入
         * @param userDao
         */
       public  UserServiceImpl(UserDao userDao){
            this.userDao = userDao;
       }
    
        /**
         * 调方法
         * @return
         */
        @Override
        public String show() {
            return userDao.show();
        }
    }
    

    6、编写测试类

    package com.xiang.service;
    
    import com.xiang.dao.impl.UserDaoImpl1;
    import com.xiang.dao.impl.UserDaoImpl2;
    import org.junit.Test;
    
    /**
     * Created by IntelliJ IDEA.
     * User: xiang
     * Date: 2021/9/22 11:00
     */
    public class UserServiceImplTest {
    
        /**
         * 普通方式查询
         */
    //    @Test
    //    public void Test01() {
    //        UserServiceImpl service = new UserServiceImpl();
    //        System.out.println(service.show());
    //    }
    
        /**
         * set 注入
         */
    //    @Test
    //    public void Test02() {
    //        UserServiceImpl service = new UserServiceImpl();
    //        service.setUserDao(new UserDaoImpl2());
    //        System.out.println(service);
    //        System.out.println(service.show());
    //    }
    
        /**
         * set 注入
         */
    //    @Test
    //    public void Test03() {
    //        UserServiceImpl service = new UserServiceImpl();
    //        service.setUserDao(new UserDaoImpl1());
    //        System.out.println(service);
    //        System.out.println(service.show());
    //    }
    
        /**
         * 构造器 注入
         */
        @Test
        public void Test04() {
            UserServiceImpl service = new UserServiceImpl(new UserDaoImpl2());
            System.out.println(service);
            System.out.println(service.show());
        }
    }
    

    7、运行结果

  • 相关阅读:
    我的.emacs配置文件
    C语言夜未眠5——变量前缀代表的含义
    c语言夜未眠4——对某一位或几位进行反转
    指针也可这么玩:返回局部指针变量,局部噢
    lua学习之table类型
    10个最“优秀”的代码注释
    为什么我希望用C而不是C++来实现ZeroMQ
    c语言夜未眠2——实现撤销和重做
    emacs学习笔记(基本概念)
    flutter json_serializable
  • 原文地址:https://www.cnblogs.com/d534/p/15319450.html
Copyright © 2011-2022 走看看