Spring XML配置文件结构及bean的命名


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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans	      
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context		   
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="helloWorld" class="main.java.com.sommer.learn.HelloWorldImpl"></bean>

</beans>

        xmlns是XML Namespace 的缩写,这里表示spring 命名空间。Spring在Classpath中查找所有的 spring.handlers 并解析xml配置的命名空间与对应的处理类。命名空间的这些项目不是固定的,可从 http://www.springframework.org/schema/ 根据需求选择。

这里我们先不讨论它,主要看<bean> </bean>的组成结构,因为它表示如何从IoC容器中获取对象(bean)并完成我们所需要的功能。


上面代码的<bean id="helloWorld "  class=" main.java.com.sommer.learn.HelloWorldImpl"> </bean>,其中helloWorld表示bean的标识,main.java.com.sommer.learn.HelloWorldImpl表示bean的类。这只是bean的一种最简单的配置。

bean的配置项具体如下:

全限定类名(class:用于定义Bean的实现类;

Bean行为:这些定义了Bean在容器中的行为;包括作用域(单例、原型创建)、是否惰性初始化及生命周期等;

Bean创建方式:说明是通过构造器还是工厂方法创建

Bean之间关系:即对其他bean的引用,也就是依赖关系定义,这些引用bean也可以称之为同事bean或依赖bean,也就是依赖注入。


一般情况下只有全限定类名是必须的,其他都是可选的。


bean的命名

1.不指定id,只配置必须的全限定类名,由IoC容器为其生成一个标识,程序必须通过“getBean(Class<T> requiredType)”获取Bean

<bean class="main.java.com.sommer.learn.HelloWorldImpl"></bean>

获取bean的程序

ApplicationContext apc = new ClassPathXmlApplicationContext("springXML/HelloWorld.xml");  
HelloWorld hello = apc.getBean(HelloWorld.class);  
System.out.println(hello.sayHi());  

2. 指定id,必须在Ioc容器中唯一;

<bean id="helloWorld" class="main.java.com.sommer.learn.HelloWorldImpl"></bean>

获取bean的程序

ApplicationContext apc = new ClassPathXmlApplicationContext("springXML/HelloWorld.xml");  
HelloWorld hello = apc.getBean("helloWorld",HelloWorld.class);  
System.out.println(hello.sayHi());  


3. 指定name,必须在Ioc容器中唯一

<bean name="helloWorld" class="main.java.com.sommer.learn.HelloWorldImpl"></bean>

获取bean的程序

ApplicationContext apc = new ClassPathXmlApplicationContext("springXML/HelloWorld.xml");  
HelloWorld hello = apc.getBean("helloWorld",HelloWorld.class);  
System.out.println(hello.sayHi()); 

4. 指定别名alias(一个bean可以有多个)

<bean name="helloWorld"  alias="alias1"  class="main.java.com.sommer.learn.HelloWorldImpl"></bean>


获取bean的程序

ApplicationContext apc = new ClassPathXmlApplicationContext("springXML/HelloWorld.xml");  
HelloWorld hello = apc.getBean("alias1",HelloWorld.class);  //这里当然也可以根据name获得
System.out.println(hello.sayHi());  

如果同时指定了idnameid就是标识符,而name就是别名,必须在Ioc容器中唯一;

如果指定多个name,第一个被用作标识符,其他的是别名,所有标识符也必须在Ioc容器中唯一;

注:nameid都作为“标识符”,那为什么还要同时存在呢?这是因为当使用基于XML的配置元数据时,在XMLid是一个真正的XML id属性,因此可以利用XML解析器来验证引用的这个id是否存在,从而更早的发现是否引用了一个不存在的bean,而使用name,则可能要在真正使用bean时才能发现引用一个不存在的bean




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值