Announcement Announcement Module

JavaConfig forum decommissioned in favor of Core Container

As described at

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at
See more
See less
@ImportXml using value from @ExternalValue Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • @ImportXml using value from @ExternalValue

    I am trying to mix configuration styles XML and JavaConfig so that I can dynamically import a different XML beans definition file depending on the value of a property.

    The basic idea is this:

    @ImportXml(locations={"[VALUE of storefrontXml]"})
    public class StoreFrontConfig {
    	@ExternalValue("storefront.xml") String storefrontXml;
    	protected Logger logger = Logger.getLogger(this.getClass());
    	public void init() {"storefrontXml=" + storefrontXml);
    1. Set the value of storefrontXml from the external properties file
    2. use the value of storefrontXml field inside the @ImportXml annotation to import the appropriate XML beans definition.

    Is this possible?

    Thx in advance!


  • #2
    Hi Ed,

    Unfortunately, what you're trying to do is not possible, for several reasons. Here's a possible workaround, however:

    Wherever you're bootstrapping JavaConfigApplicationContext:
    public static void main(String[] args) {
        String xmlFile = args[0];
        ClassPathXmlApplicationContext xmlParent = new ClassPathXmlApplicationContext(xmlFile);
        JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(xmlParent);
        // ...
    All the XML-defined beans will be available to all the JavaConfig-defined beans, and you can dynamically determine which XML file to use.

    Hope this helps!


    • #3
      Thanks Chris,

      I am actually trying to bootstrap the JavaConfigApplicationContext from an existing web application by including the bean inside my applicationContext.xml:

      <bean class=""/>
      I was then hoping to annotate a couple of classes with @Configuration to do the dynamic imports mentioned in my original post. However, I can't seem to get past this exception on startup in Tomcat 6.0.14:

      2009-03-30 11:09:04,984 ERROR [org.springframework.web.context.ContextLoader] - <Context initialization failed>
              at org.springframework.util.ClassUtils.convertClassNameToResourcePath(
              at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(
              at org.springframework.web.context.ContextLoader.createWebApplicationContext(
              at org.springframework.web.context.ContextLoader.initWebApplicationContext(
              at org.springframework.web.context.ContextLoaderListener.contextInitialized(
              at org.apache.catalina.core.StandardContext.listenerStart(
              at org.apache.catalina.core.StandardContext.start(
              at org.apache.catalina.core.ContainerBase.addChildInternal(
              at org.apache.catalina.core.ContainerBase.addChild(
              at org.apache.catalina.core.StandardHost.addChild(
              at org.apache.catalina.startup.HostConfig.deployWAR(
              at org.apache.catalina.startup.HostConfig.deployWARs(
              at org.apache.catalina.startup.HostConfig.deployApps(
              at org.apache.catalina.startup.HostConfig.start(
              at org.apache.catalina.startup.HostConfig.lifecycleEvent(
              at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(
              at org.apache.catalina.core.ContainerBase.start(
              at org.apache.catalina.core.StandardHost.start(
              at org.apache.catalina.core.ContainerBase.start(
              at org.apache.catalina.core.StandardEngine.start(
              at org.apache.catalina.core.StandardService.start(
              at org.apache.catalina.core.StandardServer.start(
              at org.apache.catalina.startup.Catalina.start(
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(
              at java.lang.reflect.Method.invoke(
              at org.apache.catalina.startup.Bootstrap.start(
      I saw an old JIRA relating to this issue, but don't see a way forward. Being a complete newb to JavaConfig I'm sure I am missing something basic. Do I need to bootstrap the context from my web.xml? If so, can I use both the regular WebApplicationContext side-by-side with this one?

      Thanks again for your reply.



      • #4

        Take a look at the reference docs' section on bootstrapping in a web app:

        - C


        • #5
          Thanks for the clarification & your patience...

          Just to make sure I am understanding you and the docs correctly:

          Inside my web.xml I EITHER use the usual implicit loading of the XmlApplicationContext OR the JavaConfigWebApplicationContext configuration to which you referred me.

          If I were to use the latter, then I could have one/many @Configuration classes in which I could import all my web application's existing xml configuration. Inside at least one of those existing XML configuration files I would then need to add the post processor bean:

          <bean class=""/>
          In addition I could then pursue the solution for a "conditional import" that you outlined above.

          Does that sound about right?




          • #6
            If you're bootstrapping via a JavaConfig[Web]ApplicationContext, there's no need at all for declaring a ConfigurationPostProcessor. CPP is only necessary if you're bootstrapping via ClassPathXmlApplicationContext or some other non-JavaConfig-aware ApplicationContext implementation.

            Yes, your understanding is correct, that it's an either/or thing in web.xml. Either bootstrap with the implicit XmlWebApplicationContext or explicitly with JavaConfigWebApplicationContext.

            The 'conditional import' workaround that I detailed would only really work if you're programmatically creating your JavaConfigApplicationContext. In web.xml, you're creating it declaratively, and so there's no real way to 'decide' which beans xml files you want to import.


            • #7
              Thanks Chris. Clear now :-)

              Given that I am working with an existing app, I may investigate alternatives for my underlying problem.