Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

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:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
Are XML Config info and JavaConfig info visible to each other? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Are XML Config info and JavaConfig info visible to each other?

    Here is what I mean:

    XML-Config
    -----------
    <beans>
    <bean id="ABC" class="example.ABC"/>
    <context:component-scan base-package="example"/>
    </beans>

    Java-Config
    -----------
    package example;

    @Configuration
    public class SimpleConfig
    {
    @Autowired
    ApplicationContext ctx;

    @Bean
    public ABC abc()
    {
    return (ABC) ctx.getBean("ABC");
    }
    }
    -----------
    Will SimpleConfig.abc() successfully returns an ABC bean object. If not, what else do I have to do??

  • #2
    This would work, but I'm not sure why you'd do it. What's the goal you're trying to achieve by re-exposing an XML-declared bean through a @Bean method?

    Bottom line is that all beans declared in XML are visible / referenceable from within @Configuration classes, and vice-versa.

    Usually, it would be something like this:

    Code:
    (com/acme/app-config.xml)
    <beans>
        <bean id="foo" class="com.acme.Foo"/>
    </beans>
    
    @Configuration
    @ImportResource("classpath:com/acme/app-config.xml")
    public class Config {
        @Autowired
        private Foo foo; // wire in the XML-configured bean
    
        public @Bean Bar bar() {
            return new Bar(foo);
        }
    }
    Note that you could also do the above by autowiring Foo via bar()'s method parameters:

    Code:
    @Configuration
    @ImportResource("classpath:com/acme/app-config.xml")
    public class Config {
        public @Bean Bar bar(Foo foo) {
            return new Bar(foo);
        }
    }
    It's a little more concise, however it prevents other beans from depending on Bar and calling bar() directly. But I digress.

    Hope this helps.

    Comment


    • #3
      Originally posted by Chris Beams View Post
      This would work, but I'm not sure why you'd do it. What's the goal you're trying to achieve by re-exposing an XML-declared bean through a @Bean method?

      Bottom line is that all beans declared in XML are visible / referenceable from within @Configuration classes, and vice-versa.

      Usually, it would be something like this:

      Code:
      (com/acme/app-config.xml)
      <beans>
          <bean id="foo" class="com.acme.Foo"/>
      </beans>
      
      @Configuration
      @ImportResource("classpath:com/acme/app-config.xml")
      public class Config {
          @Autowired
          private Foo foo; // wire in the XML-configured bean
      
          public @Bean Bar bar() {
              return new Bar(foo);
          }
      }
      Note that you could also do the above by autowiring Foo via bar()'s method parameters:

      Code:
      @Configuration
      @ImportResource("classpath:com/acme/app-config.xml")
      public class Config {
          public @Bean Bar bar(Foo foo) {
              return new Bar(foo);
          }
      }
      It's a little more concise, however it prevents other beans from depending on Bar and calling bar() directly. But I digress.

      Hope this helps.
      Thanks for the useful info. But I have multiple external xml files that are needed to be import like the following. I tried the following but javac stop me saying "duplicate annotation".

      Code:
      @Configuration
      @ImportResource("classpath:com/acme/app-config.xml")
      @ImportResource("classpath:com/acme/sec-config.xml")
      @ImportResource("classpath:com/acme/perf-config.xml")
      public class Config {
          public @Bean Bar bar(Foo foo) {
              return new Bar(foo);
          }
      }

      Comment


      • #4
        @ImportResource accepts a String array of input to its value attribute. This will get you what you want:

        Code:
        @ImportResource({"classpath:com/acme/app-config.xml", "classpath:com/acme/sec-config.xml", "classpath:com/acme/perf-config.xml"})

        Comment


        • #5
          @ImportResource problem

          I am executing a console application that is configured using Spring 3.0 JavaConfig annoation (see code below). When I run the application within Eclipse, I get the error below. The problem is that the xml file is not being found. It is in fact in that exact location with the web directory being in the root of the project. The xml file only had the header info. I have commented out all other elements. I have tried using an absolute path, every combination of path formatting I can think of, and relocating the xml file, all with the same results. If I comment out the @ImportResource line of code, the application works perfectly.

          Any suggestions (I've also read the reference docs and Googled by fingers off with no luck)?

          Code:
          public class BiddingConsole{
          
              public static void main(String[] args) throws BiddingException {
          		logger.debug("app started");
          		ApplicationContext ctx = new AnnotationConfigApplicationContext(LiquidlogicConfig.class);
              
               // .... rest of my code
          }
          Code:
          @Configuration
          @ImportResource("classpath:web/WEB-INF/liquidlogic-config.xml")
          public class LiquidlogicConfig {
             //.... my code
          }
          Code:
          03:15:36,194  INFO XmlBeanDefinitionReader (XmlBeanDefinitionReader.java:315)     - loadBeanDefinitionsLoading XML bean definitions from class path resource [web/WEB-INF/liquidlogic-config.xml]
          03:15:36,195  INFO DefaultSingletonBeanRegistry (DefaultSingletonBeanRegistry.java:421)     - destroySingletonsDestroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1630ab9: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,liquidlogicConfig,biddingService,applicationService,stateFactory,applicationFactory,bidFactory,application]; root of factory hierarchy
          Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [web/WEB-INF/liquidlogic-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [web/WEB-INF/liquidlogic-config.xml] cannot be opened because it does not exist
          	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
          	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
          	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
          	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
          	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
          	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromImportedResources(ConfigurationClassBeanDefinitionReader.java:233)
          	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:99)
          	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:84)
          	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:187)
          	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:146)
          	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:624)
          	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:614)
          	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:398)
          	at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:65)
          	at com.liquidnetworks.liquidlogic.application.BiddingConsole.main(BiddingConsole.java:62)
          Caused by: java.io.FileNotFoundException: class path resource [web/WEB-INF/liquidlogic-config.xml] cannot be opened because it does not exist
          	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
          	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)

          liquidlogic-config.xml
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
          	xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd">
          Last edited by solarch; Jan 14th, 2010, 04:42 PM. Reason: forgot the xml code

          Comment


          • #6
            @ImportXML

            It seems that if I use the @ImportXML notation, the application. Is this a viable option for bringing in 1 or more application context xml files? I would still like to know about the problem with @ImportResource and if there is a use for this that doesn't produce the error described above.

            Thx

            Comment


            • #7
              Originally posted by solarch View Post
              ...

              Code:
              @Configuration
              @ImportResource("classpath:web/WEB-INF/liquidlogic-config.xml")
              public class LiquidlogicConfig {
                 //.... my code
              }
              Remeber that for a web application the classpath is WEB-INF/classes. So you should put liquidlogic-config.xml into that directory and use @ImportResource("classpath:/liquidlogic-config.xml").

              HTH, napu

              Comment


              • #8
                @ImportResource problem

                Thanks, that worked perfect.

                Comment

                Working...
                X