Spring5(3) —— 第一个Spring程序


1.创建POJO

package com.thhh.pojo;

public class Hello {
    private String str;

    public Hello() {
    }

    public Hello(String str) {
        this.str = str;
    }

    public String getStr() {
        return str;
    }

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

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

2.编写配置文件

<?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-->
    <bean id="hello" class="com.thhh.pojo.Hello">
        <property name="str" value="HELLO SPRING"/>
    </bean>
</beans>

3.测试

package com.thhh.pojo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}

在这里插入图片描述


4.使用Spring改造上一个例子

1.配置文件

<?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-->

    <!--创建3个DAO层实现类的对象-->
    <bean id="UserDaoImpl" class="com.thhh.dao.UserDaoImpl"/>
    <bean id="UserDaoMySqlImpl" class="com.thhh.dao.UserDaoMySqlImpl"/>
    <bean id="UserDaoOracleImpl" class="com.thhh.dao.UserDaoOracleImpl"/>

    <!--创建供客户端调用的service层的对象-->
    <bean id="UserServiceImpl" class="com.thhh.service.UserServiceImpl">
        <property name="userDao" ref="UserDaoMySqlImpl"/>
    </bean>
</beans>

2.测试

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
        userServiceImpl.getUser();
    }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


5.小结

  • 上面的两个例子中,我们没有是一个new关键字,但是通过获取 配置文件对象/容器,我们成功的获取了对象的对象实例,并且调用了这个对象的非静态方法,这就说明我们获取到的是一个真真正正存在的实例化了的对象,否则它的方法是不会别我们调用的
  • 但是对象是谁创建的?对象的属性又是谁进行的赋值?
    • 答案只有一个,都是【spring容器】
    • 在整个使用过程中,为了和我们调用的类有关联的就是我们创建的配置文件beans.xml,对象的属性也是在这个文件中进行的配置
  • 在使用spring实现IOC的时候,我们同样是将对象选择的主动权给到了用户/客户端,用户要使用哪个对象只需要去spring文件中进行修改即可,对象的创建过程被分配到了spring的配置文件/容器 中,客户端通过spring配置文件/容器获取对象,再将对象注入到service层,这样就通过Spring实现了IOC
  • 到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !

6.解析配置文件中的一些节点的意义

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值