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: , , , ,

Jul 17

The Random class provides a method that returns a random integer between 0 and n (exclusive).


Random wheel = new Random();
int random = wheel.nextInt(n); 

By default it uses the current timestamp as the seed for the random generator, or alternately you can provide your own.

written by objects \\ tags: ,

May 26

Java provides the ZipInputStream class for reading from a zip file. Heres a general example of its usage

File file = new File("my.zip");
ZipInputStream zin = new ZipInputStream(new FileInputStream(file));
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
   String filename = ze.getName();
   if (!ze.isDirectory()) {
        // Read file contents from 'zin'
        // For example you could read from zin and write it to a FileOutputStream
        // http://helpdesk.objects.com.au/java/how-do-i-copy-one-stream-to-another-using-java
   }
   zin.closeEntry();
}
zin.close();

The same can be used to read the contents of a jar file.

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