微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring5学习二:Spring 配置、依赖注入

Spring5学习(二)

5.Spring 配置

5.1 别名

    <alias name="user" alias="usernew"/>

5.2 Bean的配置

     <!--
      id:bean的唯一标识符,也就是相当于我门学的对象名
      class:bean对象所对应的全限定名:包名 + 类型
      name:也是别名,且可以取多个别名,逗号或者空格分割都行
     -->
    <bean id="user" class="com.hedachun.pojo.User" name="user2,user3,hedachun">
        <constructor-arg name="name" value="有参构造方式之参数名构造成功了"/>
        <constructor-arg name="age"  value="21"/>
    </bean>

5.3 import

import,一般用于团队开发使用,可以将多个配置文件,导入合并为一个

    <import resource="beans1.xml"/>
    <import resource="beans2.xml"/>
    <import resource="beans3.xml"/>

一般总配置名称为applicationContext.xml

6.依赖注入

6.1 构造器注入

在第四个点IOC创建对象中已经实现

6.2 Set注入

  • 依赖注入:Set注入

    • 依赖:bean对象的创建依赖于容器

    • 注入:bean对象中的所有属性,由容器来注入

1.复杂类型

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

2.真实测试对象

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> game;
    private String wife;
    private Properties info;
}

3.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="student" class="com.hedachun.pojo.Student">
        <!--普通值注入-->
        <property name="name" value="何大春"/>
    </bean>
</beans>

4.测试类

public class Mytest {
    public static void main(String[] args) {
        ApplicationContext context =  new ClasspathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());
    }
}

5.完善xml配置,主要是引用类型、数组、List、Map、Set、Null、Properties等的注入方式。具体可参考官方文档

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

    <bean id="address" class="com.hedachun.pojo.Address">
        <property name="address" value="南昌"/>
    </bean>
    <bean id="student" class="com.hedachun.pojo.Student">
        <!--普通值注入-->
        <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="1111111111"/>
                <entry key="电话卡" value="2222222222"/>
                <entry key="银行卡" value="3333333333"/>
            </map>
        </property>
        <!--Set注入-->
        <property name="game">
            <set>
                <value>LOL</value>
                <value>NBA2K</value>
            </set>
        </property>
        <!--NULL注入-->
        <property name="wife">
            <null></null>
        </property>
        <!--Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">18207218</prop>
                <prop key="性别">男</prop>
                <prop key="姓名">何大春</prop>
            </props>
        </property>
    </bean>

</beans>

控制台输出结果:

/*
Student{name='何大春',
address=Address{address='南昌'},
books=[红楼梦, 西游记, 三国演艺, 水浒传],
hobbys=[打篮球, 打王者, 玩飞机],
card={校园卡=1111111111, 电话卡=2222222222, 银行卡=3333333333},
game=[LOL, NBA2K], wife='null',
info={学号=18207218, 性别=男, 姓名=何大春}}
*/

6.3 其他注入

我们可以通过使用p命名空间和c命名空间进行注入。

具体可参考官方文档:

中文文档参考:

Spring Framework 4.3.21.RELEASE 中文文档 - 7. IoC 容器 | Docs4dev

使用:

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--p命名空间注入,可以直接注入属性的值:prpety-->
        <bean id="user" class="com.hedachun.pojo.User" p:name="何大春" p:age="20"/>
        <!--c命名空间,通过构造器注入 construct-args-->
        <bean id="user2" class="com.hedachun.pojo.User" c:name="何小春" c:age="20"/>
</beans>

测试:

    @Test
    public void test2(){
        ApplicationContext context = new ClasspathXmlApplicationContext("beans2.xml");
        User user = (User) context.getBean("user");
        System.out.println(user.toString());
    }
    @Test
    public void test3(){
        ApplicationContext context = new ClasspathXmlApplicationContext("beans2.xml");
        User user = (User) context.getBean("user2");
        System.out.println(user.toString());
    }

注意:p命名空间和c命名空间不能直接使用,需要导入xml约束!

       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"

6.4 bean的作用域

在这里插入图片描述

1.单例模式(Spring认机制)

<bean id="user" class="com.hedachun.pojo.User" p:name="何大春" p:age="20" scope="singleton"/>

每次获取的都是同一个bean

测试:

    @Test
    public void test2(){
        ApplicationContext context = new ClasspathXmlApplicationContext("beans2.xml");
        User user = (User) context.getBean("user");
        User user2 = (User) context.getBean("user");
        System.out.println(user==user2);
    }

输出为ture

2.原型模式:每次从容器中get的时候,都会产生一个新的对象!

<bean id="user2" class="com.hedachun.pojo.User" c:name="何小春" c:age="20" scope="prototype"/>

测试:

    @Test
    public void test3(){
        ApplicationContext context = new ClasspathXmlApplicationContext("beans2.xml");
        User user = (User) context.getBean("user2");
        User user2 = (User) context.getBean("user2");
        System.out.println(user==user2);
    }

输出为false。

3.其余的request、session、application等这些只能在web开发中用到。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。