ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-06-09 00:01:03  阅读:184  来源: 互联网

标签:xml 依赖 Spring private user context Spring5 public User


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开发中用到。

标签:xml,依赖,Spring,private,user,context,Spring5,public,User
来源: https://blog.csdn.net/weixin_44609958/article/details/117718711

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有