Announcement Announcement Module
Collapse
No announcement yet.
Creating a ClassPathXmlApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating a ClassPathXmlApplicationContext

    I currently have a configuration file located in my classpath (C:/blah/blah/blah/spring.xml).

    I am trying to create an ApplicationContext using the ClassPathXmlApplicationContext in the following manner:

    Applicationcontext ctx = new ClassPathXmlApplicationContext("spring.xml");
    object = (object) ctx.getBean("MyService_MyAction");

    I continued to get an IOException "cannot be opened because it does not exist".

    After this, I modified the ClassPathXmlApplicationContext to use ("classpath*:spring.xml"). There is no IOException, but it is unable to create the bean:

    "NoSuchBeanDefinitionException: No Bean named 'MyService_MyAction' is defined.

    I have been able to load this spring.xml file and bean using the FileSysttemXmlApplicationContext, but I would like to have some more flexibility on where the spring config resides. Are there other options?

    Any help on this would be much appreciated.

    Thanks!
    Will

  • #2
    Are you 100% certain that 'C:/blah/blah/blah' is on your classpath?

    Comment


    • #3
      Try

      Code:
      new ClassPathXmlApplicationContext("classpath*:/**/spring.xml");
      Or, since it's ClassPathXmlApplicationContext, I don't think you need the classpath prefix:

      Code:
      new ClassPathXmlApplicationContext("/**/spring.xml");

      Comment


      • #4
        I am developing on a PC and have added it to an environment variable called 'CLASSPATH'. Is there anything additional that I should modify?

        Comment


        • #5
          kbaum - thanks for the suggestions, but I am still receiving the "No bean name" exception.

          I am certain this bean exists as I have been able to load the same file and create the same bean using the FileSystemAppContext.

          Thanks,
          Will

          Comment


          • #6
            try to load:

            Code:
            Applicationcontext ctx = new ClassPathXmlApplicationContext("spring-not-exists.xml");
            object = (object) ctx.getBean("MyService_MyAction");
            You should get a resource not found exception. Then at least you know that in the other case spring is finding something.

            Are there any other beans defined in spring.xml? If so, are there any beans appearing your bean factory asside from "MyService_MyAction"?

            Comment


            • #7
              kbaum - I tried just that and as noted I got an IOException.

              By debugging I can see that there are no Bean Factories being created although there are several Beans defined in config. It seems that it is somehow missing all of the bean definitions, but IS finding the file.

              Is that possible?

              Thanks,
              Will

              Comment


              • #8
                Anything is possible but I'm not sure exactly what is going on. I would still want to verify that it is picking up the correct spring.xml. Try setting spring to debug log level. You might find some interesting information.

                If you don't find out anything interesting in the logs, i would probably resort to stepping through the creation of the bean factory within the debugger.

                Comment


                • #9
                  To ensure that my file was in the classpath, i grabbed the system object and have pasted part of it below - this is the java.class.path object:

                  C:\PVCS\Code\Common_Frameworks\src\frameworks\spri ng.xml;

                  I want to use this file for my context so I am using the following:

                  ApplicationContext ctx = new ClassPathXmlApplicationContext("/**/spring.xml");

                  Although I am not getting a failure on this line, I continue to get an error on the following line:

                  object = (object ) ctx.getBean("MyService_MyAction");

                  "org.springframework.beans.factory.NoSuchBeanDefin itionException: No bean named 'MyService_MyAction' is defined"

                  Any other ideas?

                  Comment


                  • #10
                    Can you post the contents of the "spring.xml" file?

                    Comment


                    • #11
                      Here is the spring.xml - I have changed some things to keep it annonymous

                      <?xml version="1.0" encoding="UTF-8"?>
                      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
                      <beans>
                      <!-- This Section Lists Configuration elements for different Services.-->
                      <bean id="MyService_MyAction" class="com.xxx.arch.ei.saf.SpringServiceAccessor">
                      <!-- Injected Behaviour -->
                      <property name="sendingFilterManager">
                      <ref local="SendingFilterManager" />
                      </property>
                      <property name="receivingFilterManager">
                      <ref local="ReceivingFilterManager" />
                      </property>
                      <property name="messageContext">
                      <ref local="SOAPMessageContext" />
                      </property>
                      <property name="serviceName">
                      <value>Service1</value>
                      </property>
                      <property name="actionName">
                      <value>HTTPAction</value>
                      </property>
                      </bean>

                      <!-- This Section Lists Configuration elements for different Sending Filter Managers -->
                      <bean id="SendingFilterManager" class="com.xxx.arch.ei.iff.filters.SendingFilterMa nager" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">

                      <property name="filterChain">
                      <ref local="SendingFilterChain" />
                      </property>
                      <property name="target">
                      <ref local="HTTPSendTarget" />
                      </property>
                      </bean>

                      <!-- This Section Lists Configuration elements for different Recieving Filter Managers -->
                      <bean id="ReceivingFilterManager" class="com.xxx.arch.ei.iff.filters.ReceivingFilter Manager" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">

                      <property name="filterChain">
                      <ref local="ReceivingFilterChain" />
                      </property>
                      <property name="target">
                      <ref local="HTTPRecieveTarget" />
                      </property>
                      </bean>

                      <!-- This Section Lists Configuration elements for Filter Chains -->
                      <bean id="SendingFilterChain" class="com.xxx.arch.ei.iff.filters.FilterChain" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
                      <property name="filter">
                      <list>
                      <ref local="ConfigFilter" />
                      <ref local="AuditFilter" />
                      <ref local="AssemblyFilter" />
                      </list>
                      </property>
                      </bean>

                      <bean id="ReceivingFilterChain" class="com.xxx.arch.ei.iff.filters.FilterChain" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
                      <property name="filter">
                      <list>
                      <ref local="DisassemblyFilter" />
                      <ref local="AuditFilter" />
                      </list>
                      </property>
                      </bean>

                      <!-- this section contains a listing of the Filters -->
                      <bean id="AuditFilter" class="com.xxx.arch.ei.saf.filters.AuditFilter" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default" />
                      <bean id="ConfigFilter" class="com.xxx.arch.ei.saf.filters.ConfigurationDi scoveryFilter" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
                      <property name="configuration">
                      <ref local="SAFConfiguration"/>
                      </property>
                      </bean>
                      <bean id="AssemblyFilter" class="com.xxx.arch.ei.saf.filters.SOAPEnvelopeAss emblyFilter" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default" />
                      <bean id="DisassemblyFilter" class="com.xxx.arch.ei.saf.filters.SOAPEnvelopeDis assemblyFilter" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default" />

                      <!-- this section contains a listing of the Targets -->
                      <bean id="HTTPSendTarget" class="com.xxx.arch.ei.saf.targets.HTTPSendTarget" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default" >
                      <property name="httpControl">
                      <ref local="HTTPControl"/>
                      </property>
                      <property name="maxFailoverCount">
                      <value>5</value>
                      </property>
                      </bean>

                      <bean id="HTTPRecieveTarget" class="com.xxx.arch.ei.saf.targets.HTTPReceiveEmpt yTarget" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default" />

                      <!-- this section contains the configuration class -->
                      <bean id="SAFConfiguration" class="com.xxx.arch.ei.configuration.SprAppCfg" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default">
                      <property name="locator">
                      <value>C:\PVCS\Code\Common_Frameworks\src\framewor ks\config\SAFConfigurationTemplate.xml</value>
                      </property>
                      <property name="configurationLoader">
                      <ref local="FileConfigurationLoader"/>
                      </property>
                      <property name="cache">
                      <ref local="CustomCache"/>
                      </property>
                      </bean>

                      <bean id="FileConfigurationLoader" class="com.xxx.arch.ei.configuration.loader.FileCo nfigurationLoader" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"/>

                      <bean id="CustomCache" class="com.xxx.arch.ei.configuration.cache.CustomC ache" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"/>


                      <!-- this section contains the message context class -->
                      <bean id="SOAPMessageContext" class="com.xxx.arch.ei.utils.SOAPMessageContext" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"/>

                      <bean id="HTTPControl" class="com.xxx.arch.ei.utils.HTTPControl" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"/>

                      </beans>

                      Thanks for any help,
                      Will

                      Comment


                      • #12
                        OK - can you post the first dozen or so lines of your debug level output from Spring.

                        specifically, you should see a line like this:

                        PathMatchingResourcePatternResolver: Looking for matching resources in directory tree...

                        and even more informative - one like this:
                        PathMatchingResourcePatternResolver: Resolved location pattern [/**/spring.xml] to resources [file [C:\wherever\the\resolved\context\is]

                        I have a suspicion that another "spring.xml" file is being picked up instead and that it does not contain a bean named "MyService_MyAction"

                        Comment

                        Working...
                        X