关于java:面试官你给我讲一下什么是spring依赖注入网友就这啊太简单了

7次阅读

共计 4718 个字符,预计需要花费 12 分钟才能阅读完成。

什么是依赖注入

  • 依赖 : 指 Bean 对象的创立依赖于容器 .
  • 注入 : 指 Bean 对象所依赖的资源 , 由容器来设置和拆卸 .

依赖注入的类型有三类

  1. 根本数据类型和 String 类型
  2. 其余 bean 类型(在配置文件中或者注解配置过的 bean)
  3. 简单类型 / 汇合类型

注入的形式有三种

  1. 应用构造函数注入
    构造函数形式注入:应用标签 constructor-arg
  • type: 用于指定注入的数据类型
  • index:用于指定注入的数据给构造函数中指定索引地位的参数赋值,索引地位是从 0 开始
  • name:用于指定给构造函数中指定名称的参数赋值(举荐应用)
  • value: 用于提供根本类型和 string 类型数据赋值
  • ref: 用于指定其余的 bean 数据类型,是指在 spring 容器中呈现过的 bean 对象,援用 id 进来

测试:

实体类

public class User {
    private String name;
    private Integer age;
    private Date date;

    public User(){}

    public User(String name, Integer age, Date date) {
        this.name = name;
        this.age = age;
        this.date = date;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", date=" + date +
                '}';
    }

}

Spring 外围配置文件 beans.xml

   <bean id="user" class="com.Dao.User">
        <constructor-arg name="name" value="chenhui"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
        <constructor-arg name="date" ref="date"></constructor-arg>
    </bean>
    <bean id="date" class="java.util.Date"></bean>

测试类:

 @Test
    public void test(){
        // 通过 ClassPathXmlApplicationContext 对象加载配置文件形式将 javabean 对象交给 spring 来治理
        ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml");
       // 获取 Spring 容器中的 bean 对象,通过 id 和类字节码来获取
        User user = Context.getBean("user", User.class);
        System.out.println(user);
    }

后果:

  1. 应用 set 办法注入(罕用)
  • 应用标签 property
  • name: 用于指定注入时所调用的 set 办法名称
  • value: 用于提供根本类型和 string 类型数据赋值
  • ref: 用于指定其余的 bean 数据类型,是指在 spring 容器中呈现过的 bean 对象,援用 id 进来

要求被注入的属性 , 必须有 set 办法 , set 办法的办法名由 set + 属性首字母大写 , 如果属性是 boolean 类型 , 没有 set 办法 , 是 is .

测试:

实体类

public class User {
    private String name;
    private Integer age;
    private Date date;

    public User(){}

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", date=" + date +
                '}';
    }

    public void setName(String name) {this.name = name;}

    public void setAge(Integer age) {this.age = age;}

    public void setDate(Date date) {this.date = date;}

}

Spring 外围配置文件 beans.xml

<!--    应用的是默认构造函数创建对象 -->
    <bean id="user" class="com.Dao.User">
        <property name="name" value="chenhui"></property>
        <property name="age" value="22"></property>
        <property name="date" ref="date"></property>
     </bean>
    <bean id="date" class="java.util.Date"></bean>

测试类:

  @Test
    public void test(){
        // 通过 ClassPathXmlApplicationContext 对象加载配置文件形式将 javabean 对象交给 spring 来治理
        ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml");
       // 获取 Spring 容器中的 bean 对象,通过 id 和类字节码来获取
        User user = Context.getBean("user", User.class);
        System.out.println(user);
    }

后果:

简单类型 / 汇合类型的注入

  • 用于给 list 构造汇合注入标签:
  • list array set
  • 用于给 map 构造汇合注入的标签:
  • map property
  • 构造雷同,标签能够调换,即标签体能够调换应用,不会报错

测试:

实体类:

public class User {private String[] strings;
    private List<String> list;
    private Map map;
    private Properties properties;
    private Set set;

    public void setStrings(String[] strings) {this.strings = strings;}

    public void setList(List<String> list) {this.list = list;}

    public void setMap(Map map) {this.map = map;}

    public void setProperties(Properties properties) {this.properties = properties;}

    public void setSet(Set set) {this.set = set;}

    public User(){}

    public void print(){System.out.println(Arrays.toString(strings));
        System.out.println(list);
        System.out.println(map);
        System.out.println(set);
        System.out.println(properties);
    }
}

Spring 外围配置文件 beans.xml

<bean id="user" class="com.Dao.User">
        <property name="strings">
            <array>
                <value>dasd</value>
                <value>aaaa</value>
                <value>dfff</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>dff</value>
                <value>hjjgj</value>
                <value>dgfhfgh</value>
            </list>
        </property>
        <property name="set">
            <set>
                <value>1213</value>
                <value>1fsdf</value>
                <value>trg</value>
            </set>
        </property>
        <property name="map">
            <map>
<!--                应用第一种形式 -->
                <entry key="chenhui" value="123"></entry>
                <entry key="xie" value="123"></entry>
<!--                也可应用第二种形式 -->
                <entry key="chen">
                    <value>333</value>
                </entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="cc">1</prop>
                <prop key="ww">2</prop>
                <prop key="dsa">3</prop>
            </props>
        </property>
    </bean>

测试类:

 @Test
    public void test(){
        // 通过 ClassPathXmlApplicationContext 对象加载配置文件形式将 javabean 对象交给 spring 来治理
        ApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml");
       // 获取 Spring 容器中的 bean 对象,通过 id 和类字节码来获取
        User user = Context.getBean("user", User.class);
        // 调用办法使汇合类型注入执行
        user.print();}

后果:

当构造雷同,标签能够调换

<bean id="user" class="com.Dao.User">
        <property name="strings">
            <set>
                <value>1213</value>
                <value>1fsdf</value>
                <value>trg</value>
            </set>

        </property>
        <property name="list">
            <array>
                <value>dasd</value>
                <value>aaaa</value>
                <value>dfff</value>
            </array>

        </property>
        <property name="set">
            <list>
                <value>dff</value>
                <value>hjjgj</value>
                <value>dgfhfgh</value>
            </list>
        </property>
        <property name="map">
            <props>
                <prop key="cc">1</prop>
                <prop key="ww">2</prop>
                <prop key="dsa">3</prop>
            </props>

        </property>
        <property name="properties">
            <map>
                <!--                应用第一种形式 -->
                <entry key="chenhui" value="123"></entry>
                <entry key="xie" value="123"></entry>
                <!--                也可应用第二种形式 -->
                <entry key="chen">
                    <value>333</value>
                </entry>
            </map>
        </property>
    </bean>

后果:
[图片上传中 …(image-85fd26-1613912046687-0)]

最初

欢送关注公众号:前程有光,支付一线大厂 Java 面试题总结 + 各知识点学习思维导 + 一份 300 页 pdf 文档的 Java 外围知识点总结!这些材料的内容都是面试时面试官必问的知识点,篇章包含了很多知识点,其中包含了有基础知识、Java 汇合、JVM、多线程并发、spring 原理、微服务、Netty 与 RPC、Kafka、日记、设计模式、Java 算法、数据库、Zookeeper、分布式缓存、数据结构等等。

正文完
 0