Spring学习(十三)Spring Bean 的命名方式介绍
来源:互联网 发布:阿里云搭建http代理 编辑:程序博客网 时间:2024/05/02 01:12
一般情况下,在配置一个Bean时需要为其指定一个id属性作为bean的名称。id在IoC容器中必须是唯一的,此外id的命名需要满足xml对id的命名规范。
在实际情况中,id命名约束并不会给我们带来影响。但是如果用户确实希望用到一些特殊字符来对bean进行命名,那么可以使用bean的name属性来进行命名,name属性没有字符上的限制,几乎可以使用任何字符。
每个Bean可以有一个或多个 id,我们把第一个 id 称为“标识符”,其余id叫做“别名”,这些id在 IoC 容器中必须唯一。
首先来介绍一下Bean id 的命名规则:
- 遵循XML命名规范
- 由字母,数字,下划线组成
- 驼峰式,首个单词字母小写,从第二个单词开始首字母大写。
接下来我们使用具体的例子来介绍Bean的不同命名方式
1. 配置全限定类名,唯一
在示例中主要向大家输出问候信息,我们需要一个HelloWorld接口以及一个名称为HelloWorldImpl的实现类。接下来我们创建一个配置文件和一个程序的入口类。
首先在项目中创建包definition,接下来在包中创建HelloWorld接口:
public interface HelloWorld { public void sayHello();}
接下来我们创建HelloWorldImpl实现类:
public class HelloWorldImpl implements HelloWorld{ public void sayHello() { System.out.println("Hello World"); }}接下来我们在配置文件中为HelloWorldImpl进行Bean的命名:
<bean class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
我们在程序入口Mian.java来加载配置文件以及运行示例。
public static void sayHelloWorldByClass(){ //使用FileSystemXmlApplicationContext加载配置文件信息 BeanFactory beanFactory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); //获取bean实例 HelloWorld helloWorld=beanFactory.getBean(HelloWorldImpl.class); helloWorld.sayHello();}
在Main.java文件当中我们需要:
- 完成配置文件的加载以及Spring IoC容器的启动
- 从容器中获得HelloWorldImpl实现类的实例
- 输出问候信息
2. 指定id,唯一
在配置文件中对bean进行配置
<bean id="HelloWorldById" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>修改Main程序入口,新建方法来调用bean
public static void sayHelloWorldById(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld=factory.getBean("HelloWorldById",HelloWorldImpl.class); helloWorld.sayHello();}
3. 指定name,name为标识符,唯一
在配置文件中对bean进行配置
<bean name="HelloWorldByName" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>修改Main程序入口,新建方法来调用bean
public static void sayHelloWorldByName(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld=factory.getBean("HelloWorldByName",HelloWorldImpl.class); helloWorld.sayHello();}
4. 指定id和name,其中id为标识符,name为别名,唯一
在配置文件中对bean进行配置
<bean id="HelloWorldById01" name="HelloWorldByName01" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>修改Main程序入口,新建方法来调用bean
public static void sayHelloWorldByNameAndId(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld01=factory.getBean("HelloWorldById01",HelloWorldImpl.class); HelloWorld helloWorld02=factory.getBean("HelloWorldByName01",HelloWorldImpl.class); helloWorld01.sayHello(); helloWorld02.sayHello();}
5. 指定多个name,其中多个name需要用分号来进行分割,第一个name为标识符,其他的为别名,唯一
在配置文件中对bean进行配置
<bean name="bean1;alias01;alias02;alias03" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/><bean id="bean2" name="alias11;alias12;alias13" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
修改Main程序入口,新建方法来调用bean
public static void sayHelloWorldByMutilName(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld1=factory.getBean("bean1",HelloWorldImpl.class); HelloWorld helloWorld01=factory.getBean("alias01",HelloWorldImpl.class); HelloWorld helloWorld02=factory.getBean("alias02",HelloWorldImpl.class); HelloWorld helloWorld03=factory.getBean("alias03",HelloWorldImpl.class); helloWorld1.sayHello(); helloWorld01.sayHello(); helloWorld02.sayHello(); helloWorld03.sayHello(); HelloWorld helloWorld2=factory.getBean("bean2",HelloWorldImpl.class); HelloWorld helloWorld11=factory.getBean("alias11",HelloWorldImpl.class); HelloWorld helloWorld12=factory.getBean("alias12",HelloWorldImpl.class); HelloWorld helloWorld13=factory.getBean("alias13",HelloWorldImpl.class); helloWorld2.sayHello(); helloWorld11.sayHello(); helloWorld12.sayHello(); helloWorld13.sayHello();}
6. 指定别名,使用alias标签来进行指定,唯一
在配置文件中对bean进行配置
<bean name="bean3" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/><alias name="bean3" alias="alias21"/><alias name="bean3" alias="alias22"/>
修改Main程序入口,新建方法来调用bean
public static void sayHelloWorldByAlias(){ BeanFactory factory= new FileSystemXmlApplicationContext("src/conf/conf-definition.xml"); HelloWorld helloWorld01=factory.getBean("bean3",HelloWorldImpl.class); HelloWorld helloWorld02=factory.getBean("alias21",HelloWorldImpl.class); HelloWorld helloWorld03=factory.getBean("alias22",HelloWorldImpl.class); helloWorld01.sayHello(); helloWorld02.sayHello(); helloWorld03.sayHello();}
利用别名命名时得先有一个唯一的名称(id和name都可以)
以上就是命名bean的六种方式。
2 0
- Spring学习(十三)Spring Bean 的命名方式介绍
- spring Bean的命名
- spring Bean 的命名
- Spring Bean的命名(学习笔记)
- Spring学习(十八)Bean 的三种依赖注入方式介绍
- Spring 命名bean的错误
- Spring中Bean的命名
- Spring学习(二十三)Spring表达式语言介绍
- Spring学习(四)-Bean的三种装配方式
- Spring学习(二十)Bean 的依赖关系介绍
- Spring学习(十一)Bean生命周期介绍
- Spring学习二:Spring管理Java Bean的容器介绍
- Spring学习(十六)Spring Bean内容模型介绍
- Spring学习(二十二) Bean配置的三种方式(XML、注解、Java类)介绍与对比
- spring的bean管理方式(xml)
- Spring的bean管理(注解方式)
- 学习《Spring实战(第3版)》扎记1:Spring使用注解配置Bean的方式
- Spring学习(十四)Spring Bean 的3种实例化方法介绍
- Markdown 写博文格式讲解
- 原码, 反码, 补码 详解
- MFC界面编程1:GDI+实现不规则窗体
- java 反射加载类的对象
- 二分思路总结
- Spring学习(十三)Spring Bean 的命名方式介绍
- 内省(Introspector)操作JavaBean的属性
- 内省—beanutils工具包 操作javabean属性
- epub是什么文件?epub文件怎么打开?
- CSAPP3e - x86-64 assembly code analysis - Bomb Lab: phase 4
- Jackson入门教程
- 给定一个未排序的整数数组,找到其中位数。 中位数是排序后数组的中间值,
- C语言学习篇-9指针运算、指针与数组
- C语言到c++的第一步 注释转换