spring xml文件

来源:互联网 发布:mysql 官网下载旧版本 编辑:程序博客网 时间:2024/06/12 17:23

XML文件的结构一般如下

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.   
  4. xmlns="http://www.springframework.org/schema/beans"  
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6. xmlns:context="http://www.springframework.org/schema/context"  
  7. xsi:schemaLocation="  
  8. http://www.springframework.org/schema/beans         
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10. http://www.springframework.org/schema/context            
  11. http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  12.   
  13. <bean id="helloWorld" class="main.java.com.sommer.learn.HelloWorldImpl"></bean>  
  14.   
  15. </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的程序

[java] view plain copy
  1. ApplicationContext apc = new ClassPathXmlApplicationContext("springXML/HelloWorld.xml");    
  2. HelloWorld hello = apc.getBean(HelloWorld.class);    
  3. System.out.println(hello.sayHi());    

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

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

获取bean的程序

[java] view plain copy
  1. ApplicationContext apc = new ClassPathXmlApplicationContext("springXML/HelloWorld.xml");    
  2. HelloWorld hello = apc.getBean("helloWorld",HelloWorld.class);    
  3. System.out.println(hello.sayHi());    


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

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

获取bean的程序

[java] view plain copy
  1. ApplicationContext apc = new ClassPathXmlApplicationContext("springXML/HelloWorld.xml");    
  2. HelloWorld hello = apc.getBean("helloWorld",HelloWorld.class);    
  3. System.out.println(hello.sayHi());   

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

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


获取bean的程序

[java] view plain copy
  1. ApplicationContext apc = new ClassPathXmlApplicationContext("springXML/HelloWorld.xml");    
  2. HelloWorld hello = apc.getBean("alias1",HelloWorld.class);  //这里当然也可以根据name获得  
  3. System.out.println(hello.sayHi());    

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

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

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


0 0
原创粉丝点击