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

May 21

If you do not want to store your log4j configuration in your classes directory then you need to tell log4j where it can find it.

One possibility is to configure it in your applicationContext.xml as shown here

<bean id="log4jInitialization"
 class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
   <property name="targetClass"
      value="org.springframework.util.Log4jConfigurer" />
   <property name="targetMethod" value="initLogging" />
   <property name="arguments">
      <list>
         <value>conf/log4j.xml</value>
      </list>
   </property>
</bean>

If you are running your web application as an expanded war then another option is to use a listener in your web.xml. This will only work on an unexpanded war.

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/resources/log4j.properties</param-value>
</context-param>
<context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>1000</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

written by objects \\ tags:

Dec 10

This error occurs because the File Protocol has not been added to your Restlet Component.

How to add a Protocol to your Restlet application using Spring is explained here.

written by objects \\ tags: , ,