spring5学习-控制反转及依赖注入

spring框架是由于软件开发的复杂性而创建的,spring是一个轻量级的控制反转(LoC)和面向切面(AoP)的容器框架。

使用时先导入此包,这样会连带着导入其他包。
 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>

使用方式,将实体类托管于spring容器,实体类对象的实例化交给容器来做,可动态注入对应属性的值。如:

实体类:

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文件框架

<?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">
</beans>

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">
<!--使用spring来创建对象,在spring这些都是beans-->


<!--    id = 变量名
   class = new 的对象
   property 相当于给对象的中的属性设置值
    name="str" value="spring" 属性名叫做str 值为spring
    proper-->

    <!--相当于创建对象-->
    <bean id="hello" class="com.lijian.pojo.Hello">
        <property name="str" value="spring"/>

    </bean>
</beans>
通过beans.xml中的id获取对象并进行输出测试:

在这里插入图片描述

//context spring 容器的上下文对象,总管对象
 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 //context.getBean("hello");拿到hello 这个对象
Hello hello = (Hello) context.getBean("hello");

依赖注入,注入对象对应的属性值

<?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="address" class="com.lijian.pojo.Address">
        <property name="address"  value="成都"/>
    </bean>



<!--使用set方式注入-->
    <bean id="student" class="com.lijian.pojo.Student">

<!--第一种,使用普通值value注入 -->
        <property name="name" value=""/>
<!--第二种,使用bean注入,ref 注入-->
        <property name="address" ref="address"/>
<!--第三种,数组注入-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>
<!--列表list注入-->
        <property name="hobbys">
            <list>
                <value>打球</value>
                <value>写代码</value>
                <value>看电影</value>
            </list>
        </property>
<!--键值对map注入-->
        <property name="card">
            <map>
                <entry key="学号" value="192.168.03565"/>
                <entry key="id" value="1641646464"/>
            </map>
        </property>
<!--set集合注入-->
        <property name="games">
            <set>
                <value>王者</value>
                <value>lol</value>
            </set>
        </property>
<!--null空值注入-->
        <property name="wife">
            <null/>
        </property>
<!--Properties注入,map写在value尖括号内,而Properties写在标签外面-->
        <property name="info">
            <props>
                <prop key="学号">111111</prop>
                <prop key="id">2222222</prop>
            </props>
        </property>

    </bean>

    </beans>

测试

import com.lijian.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        //获得对象,通过id获取对象
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
        //各种注入方式 的结果
        /*
        * Student{name='李'
        * address=Address{address='成都'}
        * books=[红楼梦, 西游记, 水浒传, 三国演义] 列表
        * hobbys=[打球, 写代码, 看电影]  集合
        *card={学号=192.168.03565} 键值对
        * id=1641646464}
        * games=[王者, lol] set 集合
        * info={学号=111111,id=2222222} Properties对象
        * wife='null'  普通value设置值
        * */

    }
}

总结


1、 控制反转的思想是:以往的对象的创建由程序本身来控制,而反转之后,对象的创建权交给用户。
2、spring实现对象的创建及属性注入的原理。spring通过构造器创建对象,配置xml文件时,对象就已经存在且唯一。属性值的注入方式有多种,主要通过set方法注入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值