Detailed ApplicationContext and mutual reference of multiple ApplicationContext.xml

If BeanFactory is the heart of spring, then Application is the complete body . ApplicationContext is derived from BeanFactory.

1. ApplicationContext class

 The main implementation classes of ApplicationContext are ClassPathXmlApplicationContext and FileSystemXmlApplicationContext. The former loads configuration files from the class path by default, and the latter loads files from the file system by default.

1) If the configuration file is placed on the classpath, use the ClassPathXmlApplicationContext implementation class directly:

   ApplicationContext ctx=new ClassPathXmlApplicationContext("com/techman/context/beans.xml");

   The parameters here are equivalent to: "classpath:com/techman/context/beans.xml".

2) If the configuration file is under the path of the file system, you can give priority to using the FileSystemXmlApplicationContext implementation class:

   ApplicationContext ctx=new FileSystemXmlApplicationContext("com/techman/context/beans.xml");

   The parameters here are equivalent to: "file:com/techman/context/beans.xml".

3) If there are multiple configuration files, you can also specify a set of configuration files when obtaining the ApplicationContext object, and Spring automatically integrates multiple configuration files into one configuration file in memory:

  ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"conf/bean1.xml","conf/bean2.xml"});

或者ApplicationContext ctx=new ClassPathXmlApplicationContext("conf/bean*.xml");

 

Second, configure multiple applicationContext.xml

    That is, how to introduce multiple applicationContext.xml when starting the container?

    In web.xml, use applicationContext.xml as the initialization parameter of the current web application. Multiple applicationContext.xml are separated by commas. You can also use

    E.g:

   

    or

 

3. How does an applicationContext.xml file reference a bean in another applicationContext.xml file?

    Introduce someone else 's           applicationContext.xml between the < beans></beans> tags in the applicationContext.xml file that need to reference other applicationContext.xml files , the format is as follows:

    <beans>
           <import resource="applicationContext-hibernate.xml"/>
    </beans>

    (其中,<import> 相当于Java中的导包)
    需要注意的是:
    <import resource="applicationContext-hibernatee.xml"/>这一句要放在所有bean配置的最前面。
   
四、如何引用别人已经配置好了的bean呢?
  例如,将另外一个人配置的commonDAO注入给自己的biz中:配置如下:
   <bean id="cstServiceBiz" class="org.jb.t0821c.biz.CstServiceBiz">
          <property name="commonDAO">
               <ref bean="commonDAO"/>
          </property>

   </bean>

转自 https://www.cnblogs.com/TTTTT/p/6675557.html

https://blog.csdn.net/csdnliuxin123524/article/details/78439766

https://blog.csdn.net/kouwoo/article/details/50232473

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325566351&siteId=291194637