spring Bean的命名

本文介绍Spring框架中Bean ID的命名规则及其使用方法,包括如何通过ID、名称和别名获取Bean实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每个Bean可以有一个或多个Id,我们把第一个id成为标识符,其余id叫做别名,这些id在ioc容器中必须唯一,bean id的命名约定如下。
   
bean id的命名方式:

配置全限定类名,唯一
示例:
定一个helloworld接口
package com.zto;

public interface HelloWorld {
    public void  sayHello();
}
实现该接口
package com.zto.impl;

import com.zto.HelloWorld;

public class HelloWorldImpl implements HelloWorld {

    @Override
    public void sayHello() {
        System.out.println("Hello World!!");
    }

}

定义一个spring的配置文件text.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" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task  
     http://www.springframework.org/schema/task/spring-task-4.0.xsd
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-4.1.xsd
      http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<bean class="com.zto.impl.HelloWorldImpl" ></bean>
</beans>

创建一个测试类:Main
package com.zto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {

    public static void main(String[] args) {
        BeanFactory beanFactory = 
                new ClassPathXmlApplicationContext("classpath*:test.xml");

        HelloWorld hello = beanFactory.getBean(HelloWorld.class);
        hello.sayHello();
    }

}

运行该方法,可以得到
2016-09-05 21:54:52 main INFO  support.ClassPathXmlApplicationContext:[578]:Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@694e1548: startup date [Mon Sep 05 21:54:52 CST 2016]; root of context hierarchy
2016-09-05 21:54:52 main INFO  xml.XmlBeanDefinitionReader:[317]:Loading XML bean definitions from URL [file:/E:/workspace1/subscribecore-parents/subscribecore/wechat/target/classes/test.xml]
Hello World!!

指定id,唯一

示例代码

java代码不变,修改text.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" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task  
     http://www.springframework.org/schema/task/spring-task-4.0.xsd
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-4.1.xsd
      http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <bean class="com.zto.impl.HelloWorldImpl" />
    <bean id="helloworld" class="com.zto.impl.HelloWorldImpl" />
</beans>

修改测试类:Main
package com.zto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        /*BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");

        HelloWorld hello = beanFactory.getBean(HelloWorld.class);
        hello.sayHello();*/

        syaHelloWroldById();
    }

    public static void syaHelloWroldById() {
        BeanFactory bean = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =bean.getBean("helloworld",HelloWorld.class);
        hello.sayHello();

    }
}

同样运行的结果为:
2016-09-05 21:54:52 main INFO  support.ClassPathXmlApplicationContext:[578]:Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@694e1548: startup date [Mon Sep 05 21:54:52 CST 2016]; root of context hierarchy
2016-09-05 21:54:52 main INFO  xml.XmlBeanDefinitionReader:[317]:Loading XML bean definitions from URL [file:/E:/workspace1/subscribecore-parents/subscribecore/wechat/target/classes/test.xml]
Hello World!!


指定name,唯一

同上,只修改配置文件
<?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:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task  
     http://www.springframework.org/schema/task/spring-task-4.0.xsd
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-4.1.xsd
      http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <bean class="com.zto.impl.HelloWorldImpl" />
    <bean id="helloworld" class="com.zto.impl.HelloWorldImpl" />
    <bean name="helloworldByName" class="com.zto.impl.HelloWorldImpl" />


</beans>

修改测试类:Main
package com.zto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        /*BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");

        HelloWorld hello = beanFactory.getBean(HelloWorld.class);
        hello.sayHello();*/

        /*syaHelloWroldById();*/
        syaHelloWroldByName();

    }

    public static void syaHelloWroldById() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworld",HelloWorld.class);
        hello.sayHello();

    }
    public static void syaHelloWroldByName() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworldByName",HelloWorld.class);
        hello.sayHello();

    }
}



指定id和name,唯一


<?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:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task  
     http://www.springframework.org/schema/task/spring-task-4.0.xsd
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-4.1.xsd
      http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <bean class="com.zto.impl.HelloWorldImpl" />
    <bean id="helloworld" class="com.zto.impl.HelloWorldImpl" />
    <bean name="helloworldByName" class="com.zto.impl.HelloWorldImpl" />
    <bean id="helloworldById" name="helloworldByName01" class="com.zto.impl.HelloWorldImpl" />

</beans>

package com.zto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        /*BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");

        HelloWorld hello = beanFactory.getBean(HelloWorld.class);
        hello.sayHello();*/

        /*syaHelloWroldById();*/
        /*syaHelloWroldByName();*/
        syaHelloWroldByNameAndId();

    }

    public static void syaHelloWroldById() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworld",HelloWorld.class);
        hello.sayHello();

    }
    public static void syaHelloWroldByName() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworldByName",HelloWorld.class);
        hello.sayHello();

    }

    public static void syaHelloWroldByNameAndId() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworldById",HelloWorld.class);
        hello.sayHello();
        HelloWorld hello2=beanFactory.getBean("helloworldByName01",HelloWorld.class);
        hello.sayHello();
    }
}


指定多个name,唯一

<?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:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task  
     http://www.springframework.org/schema/task/spring-task-4.0.xsd
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-4.1.xsd
      http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <bean class="com.zto.impl.HelloWorldImpl" />
    <bean id="helloworld" class="com.zto.impl.HelloWorldImpl" />
    <bean name="helloworldByName" class="com.zto.impl.HelloWorldImpl" />
    <bean id="helloworldById" name="helloworldByName01" class="com.zto.impl.HelloWorldImpl" />

    <bean name="bean1;alias11;alias12;alias13" class="com.zto.impl.HelloWorldImpl" />

    <bean id="bean2" name="alias21;alias22;alias    23" class="com.zto.impl.HelloWorldImpl" />

</beans>

package com.zto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        /*BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");

        HelloWorld hello = beanFactory.getBean(HelloWorld.class);
        hello.sayHello();*/

        /*syaHelloWroldById();*/
        /*syaHelloWroldByName();*/
        /*syaHelloWroldByNameAndId();*/
        syaHelloWroldByMultiName();

    }

    public static void syaHelloWroldById() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworld",HelloWorld.class);
        hello.sayHello();

    }
    public static void syaHelloWroldByName() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworldByName",HelloWorld.class);
        hello.sayHello();

    }

    public static void syaHelloWroldByNameAndId() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworldById",HelloWorld.class);
        hello.sayHello();
        HelloWorld hello2=beanFactory.getBean("helloworldByName01",HelloWorld.class);
        hello.sayHello();
    }
    public static void syaHelloWroldByMultiName() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld bean1= beanFactory.getBean("bean1",HelloWorld.class);
        bean1.sayHello();
        HelloWorld bean11= beanFactory.getBean("alias11",HelloWorld.class);
        bean11.sayHello();
        HelloWorld bean12= beanFactory.getBean("alias12",HelloWorld.class);
        bean12.sayHello();
        HelloWorld bean13= beanFactory.getBean("alias13",HelloWorld.class);
        bean13.sayHello();

        HelloWorld bean2= beanFactory.getBean("bean2",HelloWorld.class);
        bean2.sayHello();
        HelloWorld bean21= beanFactory.getBean("alias21",HelloWorld.class);
        bean21.sayHello();
        HelloWorld bean22= beanFactory.getBean("alias22",HelloWorld.class);
        bean22.sayHello();
        HelloWorld bean23= beanFactory.getBean("alias23",HelloWorld.class);
        bean23.sayHello();
    }
}

指定别名,唯一

<?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:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task  
     http://www.springframework.org/schema/task/spring-task-4.0.xsd
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-4.1.xsd
      http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <bean class="com.zto.impl.HelloWorldImpl" />
    <bean id="helloworld" class="com.zto.impl.HelloWorldImpl" />
    <bean name="helloworldByName" class="com.zto.impl.HelloWorldImpl" />
    <bean id="helloworldById" name="helloworldByName01" class="com.zto.impl.HelloWorldImpl" />

    <bean name="bean1;alias11;alias12;alias13" class="com.zto.impl.HelloWorldImpl" />

    <bean id="bean2" name="alias21;alias22;alias    23" class="com.zto.impl.HelloWorldImpl" />

    <bean id="bean3" class="com.zto.impl.HelloWorldImpl" />
    <alias name="bean3" alias="alias31" />
    <alias name="bean3" alias="alias32" />

</beans>


package com.zto;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        /*BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");

        HelloWorld hello = beanFactory.getBean(HelloWorld.class);
        hello.sayHello();*/

        /*syaHelloWroldById();*/
        /*syaHelloWroldByName();*/
        /*syaHelloWroldByNameAndId();*/
        /*syaHelloWroldByMultiName();*/
        syaHelloWroldByAlias();
    }

    public static void syaHelloWroldById() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworld",HelloWorld.class);
        hello.sayHello();

    }
    public static void syaHelloWroldByName() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworldByName",HelloWorld.class);
        hello.sayHello();

    }

    public static void syaHelloWroldByNameAndId() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld hello =beanFactory.getBean("helloworldById",HelloWorld.class);
        hello.sayHello();
        HelloWorld hello2=beanFactory.getBean("helloworldByName01",HelloWorld.class);
        hello.sayHello();
    }
    public static void syaHelloWroldByMultiName() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld bean1= beanFactory.getBean("bean1",HelloWorld.class);
        bean1.sayHello();
        HelloWorld bean11= beanFactory.getBean("alias11",HelloWorld.class);
        bean11.sayHello();
        HelloWorld bean12= beanFactory.getBean("alias12",HelloWorld.class);
        bean12.sayHello();
        HelloWorld bean13= beanFactory.getBean("alias13",HelloWorld.class);
        bean13.sayHello();

        HelloWorld bean2= beanFactory.getBean("bean2",HelloWorld.class);
        bean2.sayHello();
        HelloWorld bean21= beanFactory.getBean("alias21",HelloWorld.class);
        bean21.sayHello();
        HelloWorld bean22= beanFactory.getBean("alias22",HelloWorld.class);
        bean22.sayHello();
        HelloWorld bean23= beanFactory.getBean("alias23",HelloWorld.class);
        bean23.sayHello();
    }

    public static void syaHelloWroldByAlias() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath*:test.xml");
        HelloWorld bean3= beanFactory.getBean("bean3",HelloWorld.class);
        bean3.sayHello();
        HelloWorld alias31= beanFactory.getBean("alias31",HelloWorld.class);
        alias31.sayHello();
        HelloWorld alias32= beanFactory.getBean("alias32",HelloWorld.class);
        alias32.sayHello();

    }
}




bean id 的命名约定
1、遵循XML命名规范
2、由字母、数字、下划线组成
3、驼峰式,首个单词的字母大写




















bean id 的命名约定
1、遵循XML命名规范
2、由字母、数字、下划线组成
3、驼峰式,首个单词的字母大写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值