A Developer's Diary

Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Dec 28, 2012

Spring Framework - Autowiring a map, list or array using Annotation

You can auto-wire a particular property in spring, using @Autowired annotation. For using @Autowired annotation, you will have to register AutowiredAnnotationBeanPostProcessor bean instance in the spring IOC container

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
OR
You can simply include the <context:annotation-config /> element in your bean configuration file. The <context:annotation-config /> automatically registers an instance of AutowiredAnnotationBeanPostProcessor for you

For including <context:annotation-config /> element, you need to include context namespace in the bean definition file
<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-3.0.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

Auto wiring is supported for
1. Constructors
2. Setter Methods
3. Arbitrary Methods
4. Fields

Read more ...

Dec 24, 2012

Spring Framework - Injecting java properties

In this example we demonstrate how to inject Properties in our application.

The bean configuration file for injecting properties

<?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="databaseConnectionPool" class="com.examples.spring.SQLConnectionPool">
    <property name="properties">
        <props>
            <prop key="connections">5</prop>
            <prop key="timeout">3600</prop>
        </props>
    </property>
  </bean>
</beans>

The two properties are defined for the SQLConnectionPool class. These properties are injected using the setProperties setter method

Read more ...

Spring Framework - Injecting a Map

In this example we will try to populate employees map using spring's setter injection.

Bean Configuration File

<?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="myCompany" class="com.examples.spring.Company">
    <property name="name" value="myWorld" />
    <property name="employees">
      <map>
        <entry>
          <key>
            <value>pankaj</value>
          </key>
          <bean class="com.examples.spring.Employee">
            <constructor-arg name="name" value="Pankaj Tiwari" />
          </bean>
        </entry>
        <entry>
          <key>
            <value>paresh</value>
          </key>
          <bean class="com.examples.spring.Employee">
            <constructor-arg name="name" value="Paresh Tiwari" />
          </bean>
        </entry>
        <entry>
          <key>
            <value>ankit</value>
          </key>
          <bean class="com.examples.spring.Employee">
            <constructor-arg name="name" value="Ankit Rawat" />
          </bean>
        </entry>
      </map>
    </property>
  </bean>
</beans>

Read more ...

Dec 23, 2012

Spring Framework - Injecting a list

The example below demonstrates a Company bean class with two properties namely company name and the employees list. Both these properties are injected by the spring container.

<?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="myCompany" class="com.examples.spring.Company">
     <property name="name" value="myWorld"/>
  <property name="employees">
      <list>
          <bean class="com.examples.spring.Employee">
              <constructor-arg name="name" value="Pankaj Tiwari" />
          </bean>
          <bean class="com.examples.spring.Employee">
              <constructor-arg name="name" value="Paresh Tiwari" />
          </bean>
          <bean class="com.examples.spring.Employee">
              <constructor-arg name="name" value="Ankit Rawat" />
          </bean>
      </list>
  </property>
 </bean>
</beans>

Shown above is the spring's bean definition file. Make sure that the Company class has a setter method setEmployees as shown below

Read more ...

Dec 19, 2012

Spring Framework - Constructor Injection

Constructor Injection is another basic operation performed by the Spring Framework. The AppTest class tests the validity of the service name property set via Constructor Injection.

package com.examples.spring;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * File: AppTest.java 
 * 
 * Setting property via constructor argument
 */
public class AppTest {

 ApplicationContext ctx = null;

 @Before
 public void setup() {
  ctx = new ClassPathXmlApplicationContext("service-beans.xml");
 }

 @After
 public void cleanup() {
 }

 @Test
 public void testPropertyInjection() {
  Service srvc = (Service) ctx.getBean("myservice");
  Assert.assertEquals(srvc.getServiceName(), "TimerService");
 }
}

Read more ...

Spring Framework - Setter Injection

The most elementary operation performed by the Spring Framework is setting the property value via Setter Injection. The AppTest class is a junit test class which validates the property set through setter injection.

package com.examples.spring;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * File: AppTest.java 
 * 
 * Setting property via Setter Injection
 */
public class AppTest {

 ApplicationContext ctx = null;

 @Before
 public void setup() {
  ctx = new ClassPathXmlApplicationContext("service-beans.xml");
 }

 @After
 public void cleanup() {
 }

 @Test
 public void testPropertyInjection() {
  Service srvc = (Service) ctx.getBean("myservice");
  Assert.assertEquals(srvc.getServiceName(), "TimerService");
 }
}

Read more ...

Dec 13, 2012

Setting up Spring Development Environment using Maven

The step by step guide for creating a java project using Maven and configuring it for Spring Framework

#1. Setup a simple Java project using Maven
Use mvn archetype:generate command for creating a sample java project. When prompted hit enter to select the version as 1.0-SNAPSHOT and package as com.examples.spring

mvn archetype:generate -DgroupId=com.examples.spring -DartifactId=HelloSpring -DarchetypeArtifactId=maven-archetype-quickstart

Read more ...