3、Hello Spring

3.1、 XML配置文件实现

实体类:

package com.pojo;

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 + '\'' +
                '}';
    }
}

XML配置文件:
_ 使用Spring来创建对象, 在Spring中这些对象都称为Bean

	类型 变量名 = new Hello();<br />    	bean = 对象   new Hello();

	id=变量名<br />    	class= new 的对象<br />    	property= 给对象中的属性设置了一个值_<br />_​_

为class中的对象赋值 该属性必须要有Set方法!!

<?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();
        bean = 对象   new Hello();

        id=变量名
        class= new 的对象
        property= 给对象中的属性设置了一个值
    -->
    <bean id="hello" class="com.pojo.Hello">
        <!--class中的对象赋值 该属性必须要有Set方法-->
        <property name="str" value="SpringHello啊!"/>
    </bean>
</beans>

测试类:

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

3.2、Spring-01-ioc01 的改进

1、接口与实现类
![image.png](https://img-blog.csdnimg.cn/img_convert/2dad2f6a375abddcedc51dd96e9a8453.png#clientId=uae4bda25-57bf-4&from=paste&height=382&id=u3545c520&margin=[object Object]&name=image.png&originHeight=458&originWidth=413&originalType=binary&ratio=1&size=21569&status=done&style=none&taskId=u54aab1f7-5277-4c7e-a4b8-93ca383be97&width=344.5)
2、bean配置

<?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();
        bean = 对象   new Hello();

        id=变量名
        class= new 的对象
        property= 给对象中的属性设置了一个值
    -->
    <bean id="mysqlImpl" class="com.dao.UserDaoMysqlImpl"/>
    <bean id="orcaImpl" class="com.dao.UserDaoOrcaImpl"/>
    <!--ref: 引用Spring容器中创建好的对象
        value: 具体的值, 基本数据类型!
    -->
    <bean id="UserServiceImpl" class="com.service.UserServiceImpl">
        <property name="userDao" ref="orcaImpl"/>
    </bean>
    <!--class中的对象赋值 该属性必须要有Set方法-->

</beans>

3、测试

import com.service.UserServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
//        //用户调用的实际上是Service层,不会直接接触Dao层
//        UserServiceImpl userService = new UserServiceImpl();
//
//        //这里能够让用户获得控制权
//        userService.setUserDao(new UserDaoOrcaImpl());
//
//        userService.getUser();


        //获取ApplicationContext: 拿到Spring的容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        //需要什么就get什么
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");

        userServiceImpl.getUser();
    }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值