Results 1 to 5 of 5

Thread: Java Config - does it do everything you can do in XML?

  1. #1
    Join Date
    May 2012
    Posts
    3

    Default Java Config - does it do everything you can do in XML?

    Hi,

    I spent a lot of time looking for the answer to this question and I hope someone (anyone?) who knows will respond.

    I have existing XML-based configuration and am moving to Java Config. Using the XML-centric approach described in section 4.12 of version 3.1 of the spring framework doc. I simply want to create a Bean in Java Config that needs a Parent already defined in Spring XML. Is this possible? If so, please tell me how to do it! I have seen the question asked here in these forums twice before, but no one has replied at all. From other references, I surmised that one is supposedly able to do everything in Java Config that you can do in XML. This certainly seems pretty basic, but darned if I can figure it out!

    More details for those folks that need it. The bean to be created via Java Config is a Java class that extends another Bean. The base bean/class has already been created and has a bunch of DI members that I need loaded. All of the beans are singletons.

    thanks for any assistance!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I suggest a read of the reference guide as that explains how to do that. The section called 'Combining Java and XML configuration' should be a give away.

    You have to use the ImportResource annotation.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2012
    Posts
    3

    Default

    Quote Originally Posted by Marten Deinum View Post
    I suggest a read of the reference guide as that explains how to do that. The section called 'Combining Java and XML configuration' should be a give away.

    You have to use the ImportResource annotation.
    Hi Marten,

    Thanks for the response. However, I read that section and referenced it in my post. I don't need "ImportResource" as I am doing "XML-centric" not @Configuration-class-centric. The issue is not that the beans I declare in the XML are not in Spring. They are and they are properly created. The issue is how, from Java Config, do I create a child bean (from an @Bean annotated method), that has a parent bean defined in the XML. There is no use of "parent" in section 4.12 of the document you and I reference. Any ideas?

    More specifically, what is the Java Config way of doing this XML:


    <bean id="testDao" parent="abstractDao" class="com.company.package.SomeDaoClass">
    <property name="aProperty" value="com.company.package.SomePropertyValue" />
    </bean>

    and please don't respond by saying "abstractDao" needs to be a member of the class. IS_A relationships are not semantically equivalent to HAS_A relationships even though they are both aggregation relationships. I would simply like to know, yes or no if this exact representation of XML is doable in Java Config - if it is, I can't figure out how to do it and welcome someone smarter than me to help me out!

    Joe

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You cannot specify a parent bean for beans configured in java configuration simply because it is java and the @Bean methods are used as factory methods. You could create a factory method/callback method to which you pass your beans to do the common configuration (you might need to do a bit of reflection but it would work).

    Code:
    @Configuration
    public class MyConfig {
      
      @Autowired
      private Object yourParent;
    
      protected void configureBean(Object myBean) {
        // copy matching properties from parent to myBean
        // or simply retrieve and set the properties in the @Bean method
      }
    
      @Bean
      public MyBean bean1() {
        MyBean bean = new MyBean();
        configureBean(bean);
        bean.setProperty("value"):
        return bean;
      }
    
      @Bean
      public MyOtherBean bean2() {
        MyOtherBean bean = new MyOtherBean ();
        configureBean(bean);
        bean.setYetAnotherProperty("other_value"):
        return bean;
      }
    }
    Last edited by Marten Deinum; May 22nd, 2012 at 09:26 AM. Reason: Added @Bean
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    May 2012
    Posts
    3

    Default

    Thank you - this is a huge help!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •