2020-11-09 依赖注入

.Ioc创建对象的方式

没有无参构造器的话就可以使用的方式来给对象初始化,写了有参构造器就不会再默认创建无参构造器了。

1.使用无参方法创建对象,默认。

2.如果我们想使用有参构造方法。

我们无法使用有参构造器构造,当然要在没有无参构造器的情况下

  • 使用 下标赋值

    <constructor-arg index="0" value="???">
    
  • 使用 类型赋值

    <constructor-arg type="java.lang.string" value="???">
    

通过类型创建,假如有两个String就会出问题。

<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg type="java.lang.String" value="7500000"/>
    <constructor-arg type="java.lang.String" value="42"/>
</bean>
  • Javabean的方式创建 直接通过参数名来设置
<bean id="user" class="com.kyz.pojo.User">
        <constructor-arg name="name" value=""></constructor-arg>
    </bean>

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

spring容器类似婚介网站,这里的全部都会在getbean的时候创建完毕。只要挑选即可。

.Spring配置说明

1.alisa 多了一个名字,原来的还是可以用的

如果添加了别名,可以通过别名来获取对象

<alias name="user" alias="user2"></alias>
    <bean id="user" class="com.kyz.pojo.User">
        <constructor-arg name="name" value="秦疆bean nb"></constructor-arg>
    </bean>

2.bean的配置
在这里插入图片描述

id:变量名 class:全类名 name:也是别名,而且name比alisa高级,可以同时取多个别名。,;空格都可以用别名。

3.import的配置

一般用于团队合作使用,可以将多个xml合称为一个
在这里插入图片描述

内容相同,也会被合并。使用的时候,使用总的配置就可以了。ApplicationContext

7.DI(Dependency Injection)

null的注入

<bean class="ExampleBean">
    <property name="email" value=""/>
</bean>
-------
<bean class="ExampleBean">
    <property name="email">
        <null/>
    </property>
</bean>
-------
exampleBean.setEmail("");

创建对象方式

  • 通过构造器的方式注入

5.Ioc创建对象的方式

  • 通过set的方式注入【重点】

    实体类pojo

    package com.kyz.pojo;
    
    import java.util.*;
    
    public class Student {
        private String name;
        private Address address;
        private String[] books;
        private List<String> hobby;
        private Map<String,String> card;
        private Set<String> game;
        private Properties info;
        private String wife;
        public String getWife() {
            return wife;
        }
        public void setWife(String wife) {
            this.wife = wife;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Address getAddress() {
            return address;
        }
        public void setAddress(Address address) {
            this.address = address;
        }
        public String[] getBooks() {
            return books;
        }
        public void setBooks(String[] books) {
            this.books = books;
        }
        public List<String> getHobby() {
            return hobby;
        }
        public void setHobby(List<String> hobby) {
            this.hobby = hobby;
        }
        public Map<String, String> getCard() {
            return card;
        }
        public void setCard(HashMap<String, String> card) {
            this.card = card;
        }
        public Set<String> getGame() {
            return game;
        }
        public void setGame(Set<String> game) {
            this.game = game;
        }
        public Properties getInfo() {
            return info;
        }
        public void setInfo(Properties info) {
            this.info = info;
        }
        @Override
        public String toString() {
            return "Student{" + '\n'+
                    "  "+"name='" + name + '\'' +  '\n'+
                    ", address=" + address +'\n'+
                    ", books=" + Arrays.toString(books) +'\n'+
                    ", hobby=" + hobby +'\n'+
                    ", card=" + card +'\n'+
                    ", game=" + game +'\n'+
                    ", info=" + info +'\n'+
                    ", wife=" + wife +'\n'+
                    '}';
        }
    }
    
    

    ApplicationContext

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
       
        <bean id="address" class="com.kyz.pojo.Address"></bean>
        <bean id="student" class="com.kyz.pojo.Student">
    <!--        普通值注入 直接使用value-->
            <property  name="name" value="秦疆"></property>
    <!--        bean注入-->
            <property name="address" ref="address"></property>
    <!--        数组注入-->
            <property name="books">
                <array>
                    <value>array1</value>
                    <value>array2</value>
                    <value>array3</value>
                    <value>array4</value>
                </array>
            </property>
    <!--        List注入-->
            <property name="hobby">
                <list>
                    <value>list1</value>
                    <value>list2</value>
                </list>
            </property>
    <!--        Map注入-->
            <property name="card">
                <map>
                    <entry key="key1" value="value1"></entry>
                    <entry key="key2" value="value2"></entry>
                </map>
            </property>
    <!--        Ser注入-->
            <property name="game">
                <set>
                    <value>set1</value>
                    <value>set2</value>
                </set>
            </property>
    <!--        Properties注入-->
            <property name="info">
                <props>
                    <prop key="pro1">property1</prop>
                    <prop key="pro2">property2</prop>
                    <prop key="pro3">property3</prop>
                </props>
            </property>
    <!--       null值注入-->
            <property name="wife">
                <null/>
            </property>
    
        </bean>
    </beans>
    

    junit

    import com.kyz.pojo.Student;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class DiTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
            Student student = (Student) context.getBean("student");
            System.out.println(student.toString());
        }
    }
    

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

    成员属性和类的注入是常用的、而map和proterties的注入是特殊的。

    <bean id="address" class="com.kyz.pojo.Address">
            <property name="address" value="Address Class DI"></property>
    </bean>
    
            <bean id="student" class="com.kyz.pojo.Student">
    <!--        普通值注入 直接使用value-->
            <property  name="name" value="SpringDependencyInject"></property>
    <!--        bean注入-->
            <property name="address" ref="address"></property>
            <!--        Map注入-->
            <property name="card">
                <map>
                    <entry key="key1" value="value1"></entry>
                    <entry key="key2" value="value2"></entry>
                </map>
            </property>
            <!--        Properties注入-->
            <property name="info">
                <props>
                    <prop key="pro1">property1</prop>
                    <prop key="pro2">property2</prop>
                    <prop key="pro3">property3</prop>
                </props>
            </property>
    <!--       null值注入-->
            <property name="wife">
                <null/>
            </property>
            </bean>
    
    • 什么是依赖

      指Bean对象的创建依赖于容器

    • 什么是注入

      指Bean对象的属性,由容器来注入

  • 拓展的方式注入,引入一些非原生的约束

        <!--        Properties注入-->
        <property name="info">
            <props>
                <prop key="pro1">property1</prop>
                <prop key="pro2">property2</prop>
                <prop key="pro3">property3</prop>
            </props>
        </property>
    
        <property name="wife">
            <null/>
        </property>
        </bean>
    
    
    
    
    - 什么是依赖 
    
      指Bean对象的创建依赖于容器 . Bean对象的(依赖)资源 
    
    - 什么是注入
    
    ​       指Bean对象所依赖的资源 , 由容器来设置和装配 (注入)
    
    
  • 拓展的方式注入,引入一些非原生的约束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值