Feb 21

The introduction Spring JavaConfig allowed us to wire up our applications using annotated Java code instead of the xml based configuration we had been using. This makes configuration simpler and removes the limitations of what can be done with XML.

This allowed us to replace the following XML configuration

   <bean name="myBean" class="au.com.objects.MyBean"/>

with the following Java configuration

@Configuration
public class MyConfig {
    
    @Bean
    public MyBean getMyBean() {
      return new MyBean();
   }
}

But what if we are using FactoryBean’s in our configuration

   <bean name="myBean" class="au.com.objects.MyFactoryBean">
       <property name="x" value="1"/>
    </bean>

In this case MyFactoryBean is a FactoryBean, so it is used as a factory that creates the MyBean instance. But how do we do this using JavaConfig.

Prior to Spring 3.0 there was a ConfigurationSupport class which provided a getObject() method just for this purpose


    @Configuration
    public class MyConfig extends ConfigurationSupport {
       
        @Bean
        public MyBean getMyBean() {
           MyFactoryBean factory = new MyFactoryBean();
           return (MyBean) getObject(factory);
        }
    }

The introduction of Spring 3.0 saw JavaConfig migrated into Spring Core and the ConfigurationSupport class removed. Having to extend a class was considered to be undesirable. So what do we do now?

Now we are configuring our beans using Java, we don’t actually have as much need for a FactoryBean. We can simply handle the bean creation in our Java configuration code, or better still create a ‘builder’ that handles the bean creation.

    @Configuration
    public class MyConfig extends ConfigurationSupport {
       
        @Bean
        public MyBean getMyBean() {
           return MyBeanBuilder.myBean()
               .setX(1)
               .create();
        }
    }

Will post more builder examples in the coming weeks

written by objects \\ tags: , , , ,

Oct 01

Say you have a class with a number of properties, and you then have a Collection of these beans and you want to find the bean with the smallest value of one of the properties.

Instead of using a loop to go through the collection to find the smallest element based on our condition we can instead use the Collections.min() method. You just need to pass it the collection to search for the minimum and a Comparator that defines how two beans should be compared.


MyBean max = Collections.min(collectionOfMyBean, 
   new Comparator< MyBean >() {
      public int compare(MyBean bean1, MyBean beans2) {

           // We'll assume for the sake of this example 
           // that the bean property is Comparable.
           // If its not then you would just need to 
           // adjust how the 2 values are compared

           return bean1.getProperty().compareTo(bean2.getProperty());
       }
    });

written by objects \\ tags: , , , , ,

Sep 23

Say you have a class with a number of properties, and you then have a Collection of these beans and you want to find the bean with the largest value of one of the properties.

Instead of using a loop to go through the collection to find the largest element based on our condition we can instead use the Collections.max() method. You just need to pass it the collection to search for the max and a Comparator that defines how two beans should be compared.


MyBean max = Collections.max(collectionOfMyBean, 
   new Comparator< MyBean >() {
      public int compare(MyBean bean1, MyBean beans2) {

           // We'll assume for the sake of this example 
           // that the bean property is Comparable.
           // If its not then you would just need to 
           // adjust how the 2 values are compared

           return bean1.getProperty().compareTo(bean2.getProperty());
       }
    });

written by objects \\ tags: , , , ,