2.spring系列- xml中bean定义详解

bean概念回顾

我们再来回顾一下,被spring管理的对象统称为bean,我们程序中需要用到很多对象,我们将这些对象让spring去帮我们创建和管理,我们可以通过bean xml配置文件告诉spring容器需要管理哪些bean,spring帮我们创建和组装好这些bean对象;那么我们如何从spring中获取想要的bean对象呢,我们需要给bean定义一个名称,spring内部将这些名称和具体的bean对象进行绑定,然后spring容器可以通过这个的名称找对我们需要的对象,这个名称叫做bean的名称,在一个spring容器中需要是唯一的。

bean xml配置文件格式

bean xml文件用于定义spring容器需要管理的bean,常见的格式如下:

<?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-4.3.xsd">

    <import resource="引入其他bean xml配置文件" />
    <bean id="bean标识" class="玩转类型名称"/>
    <alias name="bean标识" alias="别名" />

</beans>

bean元素

用来定义一个bean对象

格式:

<bean id="bean唯一标识" name="bean名称" class="完整类型名称" factory-bean="工厂bean名称" factory-method="工厂方法"/>

bean名称

每个bean都有一个名称,叫做bean名称,bean名称在一个spring容器中必须唯一,否则会报错,通过bean名称可以从spring容器获取对应的bean对象。

bean别名

人有外号,那么bean也一样,这个外号在spring中叫做bean的别名,spring容器允许使用者通过名称或者别名获取对应的bean对象

bean名称别名定义规则

  • 当id存在的时候,不管name有没有,取id为bean的名称
  • 当id不存在,此时需要看name,name的值可以通过,;或者空格分割,最后会按照分隔符得到一个String数组,数组的第一个元素作为bean的名称,其他的作为bean的别名
  • 当id和name都存在的时候,id为bean名称,name用来定义多个别名
  • 当id和name都不指定的时候,bean名称自动生成

案例

xml:

<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-4.3.xsd">

    <bean id="helloSpring" class="com.spring.beanDemo.HelloSpring"/>

    <!-- 通过id定义bean名称:hello1 -->
    <bean id="hello1" class="com.spring.beanDemo.HelloSpring"/>

    <!-- 通过name定义bean名称:hello2 -->
    <bean name="hello2" class="com.spring.beanDemo.HelloSpring"/>

    <!-- id为名称,name为别名;bean名称:hello3,1个别名:[hello3_1] -->
    <bean id="hello3" name="hello3_1" class="com.spring.beanDemo.HelloSpring"/>

    <!-- bean名称:hello4,多个别名:[hello4_1,hello4_2,hello4_3,hello4_4] -->
    <bean id="hello4" name="hello4_1,hello4_2;hello4_3 hello4_4" class="com.spring.beanDemo.HelloSpring"/>

    <!-- bean名称:user5,别名:[hello5,hello5_1,hello5_2,hello5_3,hello5_4] -->
    <bean name="hello5,hello5_1,hello5_2;hello5_3 hello5_4" class="com.spring.beanDemo.HelloSpring"/>


</beans>

测试方法:

@Test
    public void testBeanName(){
        String path = "classpath:/bean/beans.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(path);
        for (String beanName : Arrays.asList("hello1", "hello2", "hello3", "hello4", "hello5")) {
            //获取bean的别名
            String[] aliases = context.getAliases(beanName);
            System.out.println(String.format("beanName:%s,别名:[%s]", beanName, String.join(",", aliases)));
        }

        System.out.println("spring容器中所有bean如下:");

        //getBeanDefinitionNames用于获取容器中所有bean的名称
        for (String beanName : context.getBeanDefinitionNames()) {
            //获取bean的别名
            String[] aliases = context.getAliases(beanName);
            System.out.println(String.format("beanName:%s,别名:[%s]", beanName, String.join(",", aliases)));
        }

    }

上面有2个新的方法:

getAliases:通过bean名称获取这个bean的所有别名
getBeanDefinitionNames:返回spring容器中定义的所有bean的名称

结果:

beanName:hello1,别名:[]
beanName:hello2,别名:[]
beanName:hello3,别名:[hello3_1]
beanName:hello4,别名:[hello4_1,hello4_2,hello4_3,hello4_4]
beanName:hello5,别名:[hello5_1,hello5_2,hello5_3,hello5_4]
spring容器中所有bean如下:
beanName:helloSpring,别名:[]
beanName:hello1,别名:[]
beanName:hello2,别名:[]
beanName:hello3,别名:[hello3_1]
beanName:hello4,别名:[hello4_1,hello4_2,hello4_3,hello4_4]
beanName:hello5,别名:[hello5_1,hello5_2,hello5_3,hello5_4]

当id和name都未指定,看下生成结果:

xml 配置:

    <bean class="com.spring.beanDemo.UserModel"/>
    <bean class="com.spring.beanDemo.UserModel"/>

    <bean class="java.lang.String"/>
    <bean class="java.lang.String"/>

测试方法不变,看下输出结果:

...
beanName:com.spring.beanDemo.UserModel#0,别名:[com.spring.beanDemo.UserModel]
beanName:com.spring.beanDemo.UserModel#1,别名:[]
beanName:java.lang.String#0,别名:[java.lang.String]
beanName:java.lang.String#1,别名:[]

说明:bean名称和别名都是自动生成的,未指定id和name的bean对象,第一个会有别名,别名为完整的类名。bean名称为完整类名#编号。

alias元素

语法:

<alias name="需要定义别名的bean" alias="别名" />

xml如下:

    <bean id="user" class="com.spring.beanDemo.UserModel" />
    <alias name="user" alias="user_1" />
    <alias name="user" alias="user_2" />

测试方法不变,看输出结果:

beanName:user,别名:[user_2,user_1]

import元素

当我们的系统比较大的时候,会分成很多模块,每个模块会对应一个bean xml文件,我们可以在一个总的bean xml中对其他bean xml进行汇总,相当于把多个bean xml的内容合并到一个里面了,可以通过import元素引入其他bean配置文件。

语法:

<import resource="其他配置文件的位置" />

参考:bean名称详解

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值