Groovy Bean Configuration in Spring 4

 

- Pulkit Pushkarna 

  • Currently working for ToTheNew as Senior Software Enigineer.
  • Over 2 years of experience in Grails and Spring.
  • Love to play with Java 8, Groovy and Spring.

A bit about me

Pulkit Pushkarna

Agenda

  • Brief Intro of Spring and Groovy 
  • Configuring Spring beans in groovier way
  • Aliasing a bean
  • Setting values of instance variables
  • Define the scope of the bean
  • Lifecycle callbacks
  • Bean Definition Inheritance
  • Setting values for reference type
  • Annotation config
  • AOP configuration

What is Spring?

Spring is described as lightweight framework for building java applications.
The core of Spring Framework is based on the principle of Inversion of Control (IOC).
IOC is a technique that externalizes the creation and management of Component dependencies.
Spring acts like a container which provides instances of your application classes with all the dependencies they need.
A Spring managed resource is referred to as bean.

 

What is groovy ?

Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity

  • Flat learning curve
  • Smooth Java Integration
  • Domain Specific Language
  • Powerful Features like Closures, Functional Programming, Type Inferencing etc

Configuring a bean in Spring container

XML Configuration

Groovy Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="com.spring.demo.Employee"/>
</beans>
beans {
     employee(Employee)
 }

Getting a bean from Spring container

XML Configuration

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("config.xml");
Employee employee=applicationContext.getBean("employee",Employee.class);
employee.display();

Groovy Configuration

ApplicationContext applicationContext=new GenericGroovyApplicationContext("classpath:config.groovy");
Employee employee=applicationContext.getBean("employee",Employee.class);
employee.display();

Aliasing a bean

XML Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

     <bean id="employee" class="com.spring.demo.Employee"/>
     <alias name="employee" alias="employeeAlias"/>
</beans>
beans{
    employee(Employee)
    registerAlias("employee","employeeAlias")
}

Groovy Configuration

Setting values of instance variables

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

     <bean id="employee" class="com.spring.demo.Employee">
          <property name="age" value="27"/>
          <property name="name" value="Spring"/>
     </bean>

</beans>
beans{
    employee(Employee){
        age=27
        name="Groovy"
    }
}

XML Configuration

Groovy Configuration

beans {
    employee(Employee, age:24, name:"Groovy")
}

Defining scope of a bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="employee" class="com.spring.demo.Employee" scope="singleton">
        <property name="name" value="Spring"/>
        <property name="age" value="27"/>
    </bean>

</beans>

XML Configuration

Groovy Configuration

beans{
    employee(Employee){ bean->
        bean.scope = "singleton"
        name="Groovy"
        age=26
    }
}

Lifecycle Callback

XML Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="employee" class="com.spring.demo.Employee" init-method="init" 
        destroy-method="destroy">
        <property name="name" value="Spring"/>
        <property name="age" value="27"/>
    </bean>

</beans>

Groovy Configuration

beans{
    employee(Employee){ bean->
        bean.initMethod = "init"
        bean.destroyMethod = "destroy"
        name="Groovy"
        age=26
    }
}

Bean Definition Inheritance

XML Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="parentBean" class="com.spring.demo.Employee">
        <property name="name" value="Default"/>
    </bean>

    <bean id="employee" class="com.spring.demo.Employee" parent="parentBean" >
        <property name="name" value="Spring"/>
        <property name="age" value="27"/>
    </bean>

</beans>

Groovy Configuration

beans{
    parentBean(Employee){
        name="Default"
    }

    employee(Employee){ bean->
        bean.parent="parentBean"
        name="Groovy"
        age=26
    }
}

Setting Value for Reference type

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.spring.demo.Address">
        <property name="city" value="Delhi"/>
        <property name="country" value="India"/>
    </bean>

    <bean id="employee" class="com.spring.demo.Employee">
        <property name="address" ref="address"/>
        <property name="name" value="Spring"/>
        <property name="age" value="27"/>
    </bean>

</beans>
beans{
    address(Address){
        city="Mumbai"
        country="India"
    }

    employee(Employee){
        address=ref('address')
        name="Groovy"
        age=26
    }
}

XML Configuration

Groovy Configuration

Annotation config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
    <bean id="address" class="com.spring.demo.Address">
        <property name="city" value="Delhi"/>
        <property name="country" value="India"/>
    </bean>

    <bean id="employee" class="com.spring.demo.Employee">
        <property name="name" value="Spring"/>
        <property name="age" value="27"/>
    </bean>

</beans>

XML Configuration

beans{

    xmlns([context:'http://www.springframework.org/schema/context'])

    context.'annotation-config'()

    address(Address){
        city="Mumbai"
        country="India"
    }

    employee(Employee){
        address=ref('address')
        name="Groovy"
        age=26
    }
}

Groovy Configuration

Collection Initialization

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="employee" class="com.spring.demo.Employee">
            <property name="list">
                <list>
                    <value>1</value>
                    <value>2</value>
                    <value>3</value>
                </list>
            </property>
            <property name="set">
                <set>
                    <value>1</value>
                    <value>2</value>
                </set>
            </property>
            <property name="map">
                <map>
                    <entry key="1" value="One"/>
                    <entry key="2" value="Two"/>
                </map>
            </property>
        </bean>
</beans>

XML Configuration

beans{
    employee(Employee){
        list=[1,2,3,4]
        set=[1,2]
        map =[1:"One",2:"Two"]
    }
}

Groovy Configuration

Importing beans from XML config

beans{

    importBeans("classpath:config.xml")

}

AOP Configuration

beans{

    xmlns([aop:"http://www.springframework.org/schema/aop"])

    aop.'aspectj-autoproxy'()

   }

Groovy Bean Configuration

By Pulkit Pushkarna

Groovy Bean Configuration

  • 1,705