Spring Bean的命名(学习笔记)

Bean id 的命名方式:

  • 配置全限定类名,唯一

Conf-definiton.xml

<?xmlversion="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 class="com.bestone.spring.chapter04.definition.HelloWorldImpl" />                <!--通过全限定命名命名bean  -->
<bean name="helloWorldByName"class="com.bestone.spring.chapter04.definition.HelloWorldImpl" />
</beans>


Main.java

public static voidsayHelloWorldByClass() {   //通过全限定命名命名bean
BeanFactory beanFactory =
newClassPathXmlApplicationContext("conf/conf-definition.xml"); //读取配置文件实例化一个IOC容器
HelloWorld helloWorld =beanFactory.getBean(HelloWorldImpl.class); //从容器中获取Bean
helloWorld.sayHello();  //执行业务逻辑
}


  • 指定id,唯一

Conf-definiton.xml

<?xmlversion="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">
class="com.bestone.spring.chapter04.definition.HelloWorldImpl"/>                 <!--通过指定id进行bean的命名  -->
<beanname="helloWorldByName"class="com.bestone.spring.chapter04.definition.HelloWorldImpl" />
</beans>


Main.java

public static void sayHelloWorldById(){        //通过指定id进行bean的命名
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-definition.xml");        //1.配置文件的加载和IOC容器的启动
HelloWorld helloWorld =beanFactory.getBean("helloWorld", HelloWorld.class); //2.通过id从容器中获取HelloWold bean的实例
helloWorld.sayHello(); //3.利用HelloWorld Bean实例输出问候信息
}
 


  • 指定name,唯一

name即所谓的标识符

<beanname="helloWorldByName"class="com.bestone.spring.chapter04.definition.HelloWorldImpl" />                <!--通过name命名bean -->

 

Main.java

public static void sayHelloWorldByName() {
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-definition.xml");
HelloWorldhelloWorld = beanFactory.getBean("helloWorldByName",HelloWorld.class);
helloWorld.sayHello();
}


 

  • 指定id和name,唯一

 

<!--通过id和name命名bean-->

<beanid="helloWorldById" name="helloWorldByName01"

class="com.bestone.spring.chapter04.definition.HelloWorldImpl"/>

 

Main.java

public static void sayHelloWorldByNameAndId() {
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-definition.xml");
HelloWorldhelloWorld01 = beanFactory.getBean("helloWorldById",HelloWorld.class);
helloWorld01.sayHello();          
HelloWorldhelloWorld02 = beanFactory.getBean("helloWorldByName01",HelloWorld.class);
helloWorld02.sayHello();
 
}


 

 

  • 指定多个name,唯一

<!-- 通过指定多个name命名bean --><!-- 指定单独的id和多个别名命名bean -->

<beanname="bean1;alias11;alias12;alias13"

class="com.bestone.spring.chapter04.definition.HelloWorldImpl"/>

<bean id="bean2"name="alias21,alias22,alias23"        

class="com.bestone.spring.chapter04.definition.HelloWorldImpl"/>

 

 

Main.java

public static void sayHelloWorldByMultiName() {
BeanFactory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-definition.xml");
HelloWorld bean1 =beanFactory.getBean("bean1", HelloWorld.class);        //调用不同的name和id来实例化bean
bean1.sayHello();         
HelloWorldbean11 = beanFactory.getBean("alias11", HelloWorld.class);
bean11.sayHello();
HelloWorld bean12 = beanFactory.getBean("alias12",HelloWorld.class);
bean12.sayHello();
HelloWorldbean13 = beanFactory.getBean("alias13", HelloWorld.class);
bean13.sayHello();
 
HelloWorldbean2 = beanFactory.getBean("bean2", HelloWorld.class);
bean1.sayHello();
HelloWorldbean21 = beanFactory.getBean("alias21", HelloWorld.class);
bean21.sayHello();
HelloWorldbean22 = beanFactory.getBean("alias22", HelloWorld.class);
bean22.sayHello();
HelloWorldbean23 = beanFactory.getBean("alias23", HelloWorld.class);
bean23.sayHello();
}


 

 

  • 指定别名,唯一

<!-- 指定别名命名bean-->

<bean name="bean3"

class="com.bestone.spring.chapter04.definition.HelloWorldImpl"/>

<aliasalias="alias31" name="bean3" />

<aliasalias="alias32" name="bean3" />

 

 

Main.java

public static void sayHelloWorldByAlias() {
Bean Factory beanFactory =
new ClassPathXmlApplicationContext("conf/conf-definition.xml");
HelloWorldbean3 = beanFactory.getBean("bean3", HelloWorld.class);
HelloWorldbean31 = beanFactory.getBean("alias31", HelloWorld.class);
HelloWorldbean32 = beanFactory.getBean("alias32", HelloWorld.class);
bean3.sayHello();
bean31.sayHello();       //通过bean的别名获得实例
bean32.sayHello();
}


 

 

Bean id的命名约定:

1.遵循XML命名规范

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

3.驼峰式,首个单词字母大写

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值