Spring基础

来源:互联网 发布:mac怎么点击鼠标右键 编辑:程序博客网 时间:2024/05/17 22:19

1-spring为ApplicationContext提供有三种实现


Spring为ApplicationContext提供的3种实现分别为:ClassPathXmlApplicationContext, FileSystemXmlApplicationContext和XmlWebApplicationContext,其中 XmlWebApplicationContext是专为Web工程定制的。使用举例如下:

   1. FileSystemXmlApplicationContext

       eg1. ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); //加载单个配置文件

       eg2.

               String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};

               ApplicationContext ctx = new FileSystemXmlApplicationContext(locations ); //加载单个配置文件

       eg3.    

    ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/project/bean.xml");//根据具体路径加载文件
  2. ClassPathXmlApplicationContext
       eg1.  ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
       eg2.
               String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
               ApplicationContext ctx = new ClassPathXmlApplication(locations);
       注:其中FileSystemXmlApplicationContext和ClassPathXmlApplicationContext与BeanFactoryxml文件定位方式一样是基于路径的。

 3.XmlWebApplicationContext

   eg1. ServletContext servletContext = request.getSession().getServletContext();    
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);


2-autowire="byName"

如果userDAO设置了autowire="byName" ,假设ServiceImpl有一个属性名为userDAO,Spring就会在配置文件里查找有没有名字为userDAO的bean, 自动为ServiceImpl注入。
     如果bean有两个属性,一个想默认注入,一个想自定义,只要设定了autowire,然后显式的声明那个想自定义的,就可以达到要求。这就应了需求,在需要特别配置的时候就提供配置,否则给我一个默认注入。
    还可以在根部的<beans>节点写一句default-autovwrie="byName",可以让文件里的所有bean 都默认autowrie。不过有人认为开发期可以这样,但Production Server上不应该使用Autowire。但有人认为那些自定义一次的地方比如TranscationManager应该详细定义,而Dao, Service这种大量重复定义的bean就可以这样做。

3-关于不同的配置文件中的Bean能否互相引用

  拆分成多个以后,bean的配置可以放在其中一个文件中,不必在每个文件中都复制。  
  多个文件,比如applicationContext-datasource.xml(数据源的)、applicationContext- hibernate.xml、applicationContext-service.xml等,你只需要在web.xml中配置:  
          <context-param>  
                  <param-name>contextConfigLocation</param-name>  
                  <param-value>  
                          /WEB-INF/applicationContext-*.xml  
                  </param-value>  
          </context-param>  
  这样spring就可以把多个文件同时装载。  
  在一个配置文件中的bean想要引用另一个文件中的bean,可以使用  
  <property   name="dataSource"><ref   bean="dataSource"/></property>  
  这样拿到。注意是bean=,而不是local=。  
  如果是使用local=表示从当前配置文件中寻找bean,如果bean是在其他文件中则用bean=来找。  

4-关于Context的分级

如当前环境中没这个bean,就会到parent中去找。

不过我个人觉得一般的情况用多个xml文件就够了,不必用多层的。这里用多层是因为每个不同的DispatcherServlet有自己的特殊的 handlerMappings,viewResolvers,handlerExceptionResolvers这些东西,但是每个 DispatcherServlet又需要引用一些全局的bean,比如datasource之类的。

一个applicationcontext可以load多个配置文件。只要把配置文件作为数组传进去就可以了。和parent毫不相关。
至于parent context和child context之间的关系确实如你所说。在child context可以reference到parent context中的bean。而parent context不知道child的存在,当然就更不能reference了。

至于xml文件的确是可以包含另一个xml文件的。用import就可以。详细可以看spring的dtd。不过import进去的xml不是在child context中。相当与把xml中的bean都include进去。

把context分成parent/child体系从我的角度来体会就是觉得context之间可以不互相影响。例如我可以在一个child context中部署我的scheduler。当发现配置文件改动,也就是其中的task改动时我简单的refresh child context就可以,这样不至于影响到parent context中工作的bean。

先说声谢谢,知道了三种配置xml的方式。
一个applicationContext能同时装入多个xml文件.
在web.xml中能配置多个applicationcontext.xml,可以分别加入,也可以用通配符(我喜欢这种)。
一个xml文件中能import别外一个xml.




Spring中主要的模块化单元是一个application context,它包含很多个beans(由Spring应用环境所管理的对象)。Application contexts可以分级配置,这样一个子application context可以看到定义在其上级的beans,但是反之则不行。Springexportersfactory beans的概念用于导出引用给application context外部的客户端的beans,以及注入引用到定义在一个application context外部的服务。

原创粉丝点击