0

I am trying to understand a web system using spring and I can not debug it. Now I am confused by the order of the XML initialization.

Support I have an web.xml which is like this:

<context-param>
    <param-name>contextConfigLocation</param-name>  
        <param-value>   
            classpath*:/com/pathA/**/applicationContext*.xml,classpath*:/com/pathB/**/applicationContext*.xml     
    </param-value>  
</context-param>

and in /com/pathA I have some xml files that define some beans with the same name. In /com/pathB I also have some xml files that define beans with the same name as in com/pathA's xml file.

I know that spring framework will use the last bean definition by default.But I can't find the order of the xml files initialization.

Here is the beans' definition:

/com/pathA/applicationContextOne.xml

<bean name="/testBean" class="com.TestActionOne">
</bean>

/com/pathA/applicationContextTwo.xml

<bean name="/testBean" class="com.TestActionTwo">
</bean>

/com/pathB/applicationContextThree.xml

<bean name="/testBean" class="com.TestActionThree">
</bean>

can anyone can tell me the initialization order of the differnet xml files in /com/pathA/

and the initialization order of the xml files between com/pathA/ and com/pathB?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
tousinn
  • 73
  • 2
  • 3
  • 11
  • May I ask why you have the need to define so many beans with the same name across different files? Have you considered using specific bean profiles to achieve what you want? – superEb Jun 18 '13 at 02:39
  • @superEb I am not the developer of the system,but I have to understand it. Do you have any idea of the initialization order ? – tousinn Jun 18 '13 at 02:52
  • I think it depends on if there are dependent bean definitions across multiple files, as in **bean a** in `/pathA/context.xml` references **bean b** in `/pathB/context.xml`, then **bean b** will obviously have to be initialized first. Otherwise, assuming no dependencies between beans in different files, I think Spring initializes the files in the order they are declared and/or found. So in your case, I would expect all files under pathA to be initialized before files under pathB. But with the wildcard declaration you're using, I'm not sure how Spring would order files found under the same path. – superEb Jun 18 '13 at 03:03
  • @superEb. Thank you for your answer.I agree with you on the initialization order between pathA and pathB,but have no idea about the initializaton order in pathA – tousinn Jun 18 '13 at 06:36
  • @superEb. I tried this in my local pc. and I find the initializaton order in pathA depends on the file name. – tousinn Jun 19 '13 at 07:45

1 Answers1

0

I have written some samples in local pc (Windows and AIX)to simulate the initialization order and I found the result below.

1.the initialization order of files in different path depends on the order written in classpath*:/com/pathA/*/applicationContext.xml on my question, the [applicationContext*.xml] files in pathA will be initialized first and then in pathB.

2.the initialization order of files in the same path is the order of the file name

for example,if in pathA there are [applicatinContextOne.xml] and [applicationContextTwo.xml],then [applicatinContextOne.xml] will be initialized first and then [applicationContextTwo.xml].

I tried this under both Windows and AIX, and it returns the same result. Hope this will be helpful.

tousinn
  • 73
  • 2
  • 3
  • 11