3、HelloSpring

3、HelloSpring

新建HelloSpring项目

操作过程

  • 新建一个module:spring-02-hellospring

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9Tm6jOKJ-1621779113654)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210523214304267.png)]

  • 新建pojo类

    • 设置set和get方法

    • 重写toString方法

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zrHgcPZ3-1621779113660)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210523214427121.png)]

  • 新建beans.xml

    去官网找模板:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eYTvZXKY-1621779113662)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210523214145241.png)]

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="..." class="...">  
            <!-- collaborators and configuration for this bean go here -->
        </bean>
    
        <bean id="..." class="...">
            <!-- collaborators and configuration for this bean go here -->
        </bean>
    
        <!-- more bean definitions go here -->
    
    </beans>
    
  • 测试

    public class MyTest {
        public static void main(String[] args) {
            //获取Spring的上下文对象
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            // 我们的对象现在都在Spring中管理了,我们要使用,直接去里面取出来就可以!
            Hello hello = (Hello) context.getBean("hello");
            System.out.println(hello.toString());
        }
    }
    
  • 代码地址:

    https://gitee.com/zengqiang_wang/spring-study-gongyi

  • 思考问题:

    • Hello对象是谁创建的?

      hello对象是由Spring创建的

    • Hello对象的属性是怎么设置的?

      hello对象的属性是由Spring容器设置的

    这个过程就叫控制反转。

    控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的

    反转:程序本身不创建对象,而变成被动的接收对象

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

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

    可以通过new ClassPathXmlApplicationContext去浏览一下底层源码

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

ClassPathXmlApplicationContext的类结构图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ak36k7ug-1621779113666)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210523213812033.png)]

改进Spring-01-ioc1项目

  • 核心代码:

    配置文件:

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="userDaoImpl" class="com.gy.dao.UserDaoImpl"/>
        <bean id="userDaoMysqlImpl" class="com.gy.dao.UserDaoMysqlImpl"/>
        <bean id="userDaoOracleImpl" class="com.gy.dao.UserDaoOracleImpl"/>
        <bean id="userDaoSQLServerImpl" class="com.gy.dao.UserDaoSQLServerImpl"/>
    
        <bean id="userServiceGeneral" class="com.gy.service.UserServiceGeneralImpl">
            <property name="userDao" ref="userDaoImpl"/>
        </bean>
        <!--
        ref: 引用Spring容器中出创建好的对象
        value: 具体的值,基本数据类型!
        -->
    </beans>
    

    测试类:

    public class MyTest3 {
        public static void main(String[] args) {
            //获取ApplicationContext;拿到Spring的容器
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            //容器在手,天下我有,需要什么,就直接get什么!
            UserServiceGeneralImpl userServiceGeneral = (UserServiceGeneralImpl) context.getBean("userServiceGeneral");
            userServiceGeneral.getUser();
        }
    }
    
  • 代码地址:

https://gitee.com/zengqiang_wang/spring-study-gongyi

代码show

代码结构图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-asLp8qQf-1641197564012)(D:\study\学习笔记\spring学习\3、HelloSpring.assets\image-20220103160957623.png)]

1.新建spring-02-hellospring module

2.新建pojo包及类

Hello.java

public class Hello {


    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}

3.新建资源文件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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--
    使用Spring来创建对象,在Spring这些都称为Bean

    类型 变量名 =  new 类型();
    Hello hello = new Hello();

    id = 变量名
    class = new的对象
    property 相当于给对象中的属性设置一个值!
    -->
    <bean id="hello" class="com.gy.pojo.Hello">
        <property name="str" value="Hello Spring!"/>
    </bean>

</beans>

4.新建测试类

public class MyTest {
    public static void main(String[] args) {
        //获取Spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        // 我们的对象现在都在Spring中管理了,我们要使用,直接去里面取出来就可以!
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}

彩蛋

1.一旦class被spring托管,那么class的左侧有个logo

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vqoU2ThG-1641197964737)(D:\study\学习笔记\spring学习\3、HelloSpring.assets\image-20220103161905686.png)]

2.被class托管的类无logo,bean.xml也无logo:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gI9eUkvi-1641198589973)(D:\study\学习笔记\spring学习\3、HelloSpring.assets\image-20220103162751086.png)]

解决:

在这里插入图片描述

有提示后,观察project structure

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wgXalYVg-1641198589977)(D:\study\学习笔记\spring学习\3、HelloSpring.assets\image-20220103162927027.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值