Spring- Bean的命名

Posted RZ_Lee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring- Bean的命名相关的知识,希望对你有一定的参考价值。

Bean id的命名约定:

  1.遵循XML命名规范

  2.由字母数字下划线组成

  3.驼峰式,首个单词字母小写,第二个单词首字母要大写

样例项目的结构

xml应该放在src下面的conf中,如果不方法src下将会找不到conf文件

conf-definition.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 
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        <!-- bean的六种命名方式 -->
        <!-- 配置全限定类名,唯一 -->
        <bean class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        <!-- 指定id,唯一 -->
        <bean id="helloWorld" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        <!-- 指定name,唯一 -->
        <bean name="helloWorldByName" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        <!-- 指定id和name,唯一 -->
        <bean id="helloWorldById" name="helloWorldByName01" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        
        <!-- 指定多个name,唯一 -->
        <bean name="bean1;alias11;alias12;alias13" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        
        <bean id="bean2" name="alias21;alias22;alias23" class="com.zc.spring.chapter04.definition.HelloWorldImpl" />
        
        
        <!-- 指定别名,唯一 -->
        <bean name="bean3" class="com.zc.spring.chapter04.definition.HelloWorldImpl" /> 
        <alias name="bean3" alias="alias31"/>
        <alias name="bean3" alias="alias32"/>
        <alias name="bean3" alias="alias33"/>
       
</beans>

HelloWorld接口

package com.zc.spring.chapter04.definition;

public class HelloWorldImpl implements HelloWorld {

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

	}

}  

Main运行类

package com.zc.spring.chapter04.definition;

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

public class Main {

	
	public static void main(String[] args) {
		
		
		
		sayHelloWorldByAlias();
	}
	
	public static void sayHelloWorldById() {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
		HelloWorld helloWorld = beanFactory.getBean("helloWorld", HelloWorld.class);
		helloWorld.sayHello();
		
	}
	
	public static void  sayHelloWorldByClass() {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
		HelloWorld helloWorld = beanFactory.getBean(HelloWorldImpl.class);
		helloWorld.sayHello();
	}

	public static void  sayHelloWorldByName() {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
		HelloWorld helloWorld = beanFactory.getBean("helloWorldByName", HelloWorld.class);
		helloWorld.sayHello();
	}
	
	public static void  sayHelloWorldByNameAndId() {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
		HelloWorld helloWorld = beanFactory.getBean("helloWorldById", HelloWorld.class);
		helloWorld.sayHello();
		HelloWorld helloWorld01 = beanFactory.getBean("helloWorldByName01", HelloWorld.class);
		helloWorld01.sayHello();
	}
	
	public static void sayHelloWorldMultiName() {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
		HelloWorld helloWorld = beanFactory.getBean("bean1", HelloWorld.class);
		helloWorld.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  sayHelloWorldByAlias() {
		//配置文件加载以及IOC容器启动
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-definition.xml");
		//通过别名获取bean实例
		HelloWorld bean3 = beanFactory.getBean("bean3", HelloWorld.class);
		//利用bean实例输出helloworld信息
		bean3.sayHello();
		HelloWorld bean31 = beanFactory.getBean("alias31",HelloWorld.class);
		bean31.sayHello();
		HelloWorld bean32 = beanFactory.getBean("alias32",HelloWorld.class);
		bean32.sayHello();
		HelloWorld bean33 = beanFactory.getBean("alias33",HelloWorld.class);
		bean33.sayHello();
	}
}

  

以上是关于Spring- Bean的命名的主要内容,如果未能解决你的问题,请参考以下文章

Spring中Bean的命名问题及ref和idref之间的区别

Spring---bean的命名

Spring学习笔记——02 Bean的命名及实例化

Spring中Bean的命名问题及ref和idref之间的区别

Spring中Bean的命名问题及ref和idref之间的区别

Spring- Bean的命名