• Spring之IOC入门


    ​# Spring从入门到精通–(2)Spring之IOC入门

    作者:进击攻城狮
    个人主页:欢迎访问我的主页
    首发时间:2022年8月2日星期二
    订阅专栏:Spring入门到精通
    个人信条:星光不问赶路人,岁月不负有心人。
    如果文章有错误,欢迎在评论区指正。
    🎉 支持我:点赞👍+收藏⭐️+留言📝​

    活动地址:CSDN21天学习挑战赛

    1.HelloSpring

    导入Jar包

      <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.2.0.RELEASEversion>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1、编写一个Hello实体类

    public class Hello {
        private String str;
    
        public Hello(String str) {
            this.str = str;
        }
    
        public Hello() {
    
        }
    
        public String getStr() {
            return str;
        }
    //set方法是核心
        public void setStr(String str) {
            this.str = str;
        }
    
        @Override
        public String toString() {
            return "Hello{" +
                    "str='" + str + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    2、编写我们的spring文件 , 这里我们命名为beans.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="hello" class="com.hc.pojo.Hello">
    
           <property name="str" value="Spring"/>
        </bean>
    <!--    使用spring来创建对象,在spring这些都称为Bean-->
    <!--    类型   变量名 =new 类型();-->
    <!--    Hello hello=new Hello();-->
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3、我们可以去进行测试了 .

    public class MyTest {
        public static void main(String[] args) {
    
           ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Object hello = context.getBean("hello");
            System.out.println(hello.toString());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    思考

    • Hello 对象是谁创建的 ? hello 对象是由Spring创建的
    • Hello 对象的属性是怎么设置的 ? hello 对象的属性是由Spring容器设置的

    这个过程就叫控制反转 :

    • 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
    • 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .

    依赖注入 : 就是利用set方法来进行注入的.

    IOC是一种编程思想,由主动的编程变成被动的接收

    可以通过newClassPathXmlApplicationContext去浏览一下底层源码 .

    修改案例一

    我们在案例一中, 新增一个Spring配置文件beans.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="mysqlImpl" class="com.hc.dao.UserDaoMysqlImpl"/>
        <bean id="OracleImpl" class="com.hc.dao.UserDaoOracleImpl"/>
    
        <!--    使用spring来创建对象,在spring这些都称为Bean-->
        <!--    类型   变量名 =new 类型();-->
        <!--    Hello hello=new Hello();-->
        <bean id="UserServiceImpl" class="com.hc.service.UserServiceImpl">
    <property name="userDao" ref="OracleImpl"/>
        </bean>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    测试!

    import com.hc.dao.UserDaoMysqlImpl;
    import com.hc.dao.UserDaoOracleImpl;
    import com.hc.dao.UserDaoSqlserverImpl;
    import com.hc.service.UserService;
    import com.hc.service.UserServiceImpl;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import sun.security.mscapi.CPublicKey;
    
    /**
     * @author HC
     * @version 1.0
     */
    public class MyTest {
        public static void main(String[] args) {
    
            //用户调用的是业务层,dao层他们不需要接触!
    //        UserServiceImpl userService = new UserServiceImpl();
    //        userService.setUserDao(new UserDaoSqlserverImpl());
    //        userService.setUserDao(new UserDaoOracleImpl());
    //        userService.getUser();
    
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
    
            userServiceImpl.getUser();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    OK , 到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !

    2.IOC创建对象方式

    2.1.通过无参构造方法来创建

    通过无参构造方法来创建

    1、User.java

    public class User {
        private String name;
    
        public User(String name) {
            this.name = name;
        }
    
    //    public User() {
    //    }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void show()
        {
            System.out.println("name="+getName());
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    2、beans.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--一、无参-->
    <!--   <bean id="user" class="com.kuang.pojo.User">-->
    <!--       <property name="name" value="hc"/>-->
    <!--   </bean>-->
    
    <!--    //二、有参-->
    <!--    <bean id="user" class="com.kuang.pojo.User">-->
    <!--       <constructor-arg index="0" value="狂神说java"/>-->
    <!--    </bean>-->
    
    
    <!--    三、有参-->
    <!--    <bean id="user" class="com.kuang.pojo.User">-->
    <!--        <constructor-arg name="name" value="hc"/>-->
    <!--    </bean>-->
    
        <bean id="user" class="com.kuang.pojo.UserT" name="u1 u2,u3">
            <property name="name" value="西部开源"/>
        </bean>
    
    <!--    alias取别名-->
    <!--    <alias name="user" alias="ccccccccccccccccc"/>-->
    
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    3、测试类

    public class MyTest {
        public static void main(String[] args) {
           // User user = new User();
           ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
           //User user1 = (User) context.getBean("user");
           // User user1 = (User) context.getBean("ccccccccccccccccc");
    
            UserT user4 = (UserT) context.getBean("u2");
            // User user2 = (User) context.getBean("user");
           user4.show();
            //System.out.println(user1==user2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    结果可以发现,在调用show方法之前,User对象已经通过无参构造初始化了!

    2.2.通过有参构造方法来创建

    通过有参构造方法来创建

    1、UserT . java

    public class UserT {
        private String name;
    
        public UserT() {
            System.out.println("UserT被创建了");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void show()
        {
            System.out.println("name="+getName());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2、beans.xml 有三种方式编写

    <!--一、无参-->
    <!--   <bean id="user" class="com.kuang.pojo.User">-->
    <!--       <property name="name" value="hc"/>-->
    <!--   </bean>-->
    
    <!--    //二、有参-->
    <!--    <bean id="user" class="com.kuang.pojo.User">-->
    <!--       <constructor-arg index="0" value="狂神说java"/>-->
    <!--    </bean>-->
    
    
    <!--    三、有参-->
    <!--    <bean id="user" class="com.kuang.pojo.User">-->
    <!--        <constructor-arg name="name" value="hc"/>-->
    <!--    </bean>-->
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3、测试

    public class MyTest {
        public static void main(String[] args) {
           // User user = new User();
           ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
           //User user1 = (User) context.getBean("user");
           // User user1 = (User) context.getBean("ccccccccccccccccc");
    
            UserT user4 = (UserT) context.getBean("u2");
            // User user2 = (User) context.getBean("user");
           user4.show();
            //System.out.println(user1==user2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    结论:在配置文件加载的时候。其中管理的对象都已经初始化了!

    1                      学如逆水行舟,不进则退
    
    • 1
  • 相关阅读:
    7个设计师必备的Figma汉化插件,高效设计超简单!
    SpringBoot+Vue 的留守儿童系统的研究与实现,2.0 版本,附数据库、教程
    查找算法【平衡二叉树】 - 平衡二叉树的删除
    c#优雅高效的读取字节数组——不安全代码(1)
    思科拟推出PuzzleFS驱动,采用Rust语言开发
    IP设置教程
    2312. 卖木头块
    电脑Win11安装Autocad出现错误要如何处理
    231n-图像线性分类
    ubuntu安装后配置
  • 原文地址:https://blog.csdn.net/yi_chen_c/article/details/126148460