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
Bean Context Hierarchy Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bean Context Hierarchy

    I am bit stuck at the moment. When I create my congifuration with java config and XML (using the post processor) beans defined in java config cannot find the beans from the XML (and vis versa).

    Is this because the java config beans are created in a child of the xml context or are they siblings? or am I doing someting wonrg?

    Any help or insight would be greatly apprecaited.

    Thanks,
    Mike

  • #2
    Hi Mike,

    Here is my first attempt to explain possible cause for this behavior in SJC. As per the first test, bootstrapping JavaConfig from XML with ConfigurationPostProcessor works just fine, including the ability to access beans across contexts. The converse (bootstrapping XML from JavaConfig) is not possible as there is no mechanism to do this in SJC as per my understanding with the second test.

    You should be able to find beans defined in JavaConfig which refers beans defined in XML, as long as the context has the XML configuration as shown in both tests.

    Code:
    public class TestJavaXmlBeans {
        @Test
        public void testGetBeansFromXmlParentAndJavaChild() throws Exception {
            ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext("beansFromXmlAndJava.xml", getClass());
            FullTime fulltime = (FullTime) parent.getBean("fulltime");//bean defined in parent context (XML)
            assertEquals(75000, fulltime.getWage());
            Contract contract = (Contract) parent.getBean("contract");//bean defined in parent context (XML)
            assertEquals(102000, contract.getWage());
            JavaConfigApplicationContext child = new JavaConfigApplicationContext(parent, HrConfig.class);
            fulltime = (FullTime) child.getBean("fulltime");//bean defined in child context (Java)
            assertEquals(90000, fulltime.getWage());
            contract = (Contract) child.getBean("contract");//bean defined in parent context (XML)
            assertEquals(102000, contract.getWage());
        }
    
        @Test
        public void testGetBeansFromJavaParentAndXmlChild() throws Exception {
            JavaConfigApplicationContext parent = new JavaConfigApplicationContext(HrConfig.class);
            FullTime fulltime = (FullTime) parent.getBean("fulltime");//bean defined in parent context (Java)
            assertEquals(90000, fulltime.getWage());
            //No bean named 'contract' is defined in parent context. I think this is expected behavior because there in no XML in context.
    //        Contract contract = (Contract) parent.getBean("contract");
    //        assertEquals(102000, contract.getWage());
            ApplicationContext child = new ClassPathXmlApplicationContext(new String[]{"beansFromXmlAndJava.xml"}, getClass(), parent);
            fulltime = (FullTime) child.getBean("fulltime");//bean defined in child context (XML)
            assertEquals(75000, fulltime.getWage());
            Contract contract = (Contract) child.getBean("contract");//bean defined in child context (XML)
            assertEquals(102000, contract.getWage());
        }
    
    
        @Configuration
        public abstract static class HrConfig {
            private long salary = 90000;
    
            public void setSalary(long salary) {
                this.salary = salary;
            }
    
            @Bean
            public FullTime fulltime() {
                FullTime employee = new FullTime();
                employee.setSalary(salary);
                return employee;
            }
    
            // Will be taken from XML
            @ExternalBean
            public abstract Contract contract();
        }
    
        static class FullTime implements Employee {
            long salary;
    
            public long getSalary() {
                return salary;
            }
    
            public void setSalary(long salary) {
                this.salary = salary;
            }
    
            public long getWage() {
                return salary;
            }
        }
    
        static class Contract implements Employee {
            int hours;
            long rate;
    
            public int getHours() {
                return hours;
            }
    
            public void setHours(int hours) {
                this.hours = hours;
            }
    
            public long getRate() {
                return rate;
            }
    
            public void setRate(long rate) {
                this.rate = rate;
            }
    
            public long getWage() {
                return hours * rate;
            }
        }
    
        static interface Employee {
            long getWage();
        }
    }
    Here is beansFromXmlAndJava.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
            >
    <beans>
        <bean id="contract" class="TestJavaXmlBeans$Contract">
            <property name="hours" value="2040"/>
            <property name="rate" value="50"/>
        </bean>
        <bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>
        <bean class="TestJavaXmlBeans$HrConfig">
            <property name="salary" value="75000"/>
        </bean>
    </beans>
    Let me know if I am missing your point.

    -Arul

    Comment


    • #3
      Mike,

      There should be no issue accessing beans defined in XML or JavaConfig, regardless of which bootstraps which.

      Please paste a very simple code snippet to demonstrate the issue that you're seeing.

      Comment


      • #4
        Hi All,

        Thank you for the replies.

        Let me be a little more specific.

        I have a service that is ApplicationContext aware. It needs to get beans directly using the application context that spring injects.

        If I define the service in xml then it cant find the java config beans. If I define the service in Java Config then it cant find the xml beans.

        Does that make any sense? If not then I can try to build a test for it.

        Thanks,
        Mike

        Comment


        • #5
          Hi Mike - yes, a code sample would be good. Something simple, but complete enough to be pasted into eclipse and run. I'll take a look. Thanks.

          Comment


          • #6
            Hi Chris,

            Sorry for the late reply. I just ended up moving my entire app to javaconfig and all is working.

            Thanks again for all of your work on this project.

            -Mike

            Comment


            • #7
              Very well. Glad to hear things are working for you!

              Comment

              Working...
              X