Injecting values using Annotation (SpEL)

When using annotation-style value injection, we just need to substitute the value annotations with the SpEL expressions.

Following example demonstrates injecting values with Annotation based configuration using Spring Expression Language.

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency> 
  </dependencies>

1. POJO

Create following classes

package com.kruders.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("kunnal")
public class Kunnal {
	@Value("Guitar")
	private String instrument;
	@Value("Bailamos")
	private String song;
	public String getInstrument() {
		return instrument;
	}
	public void setInstrument(String instrument) {
		this.instrument = instrument;
	}
	public String getSong() {
		return song;
	}
	public void setSong(String song) {
		this.song = song;
	}
}
package com.kruders.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("puneet")
public class Puneet {
	@Value("Keyboard")
	private String instrument;
	@Value("#{kunnal.song}")
	private String song;
	public String getInstrument() {
		return instrument;
	}
	public void setInstrument(String instrument) {
		this.instrument = instrument;
	}
	public String getSong() {
		return song;
	}
	public void setSong(String song) {
		this.song = song;
	}
}

I have used the SpEL #{kunnal.song} in referencing the property of the other bean.

2. Configuration

Create Spring-Bean.xml and write the following code and enable auto component scanning.

<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
    <context:component-scan base-package="com.kruders.bean" />
	
</beans>

3. Run Program

Create Main.java class and run it as Java Application

package com.kruders.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.kruders.bean.Puneet;
 
public class Main {
    public static void main(String[] args) {
    	ApplicationContext context = new ClassPathXmlApplicationContext(
                "Spring-Bean.xml");
    	Puneet instrument = (Puneet) context.getBean("puneet");
        System.out.println("Instrument " + instrument.getInstrument());
        System.out.println("Song " + instrument.getSong());
    }
 
}

The folder structure of the example is shown below in Figure 58.1

Figure 58.1 Figure 58.1

You can download the source code of this example here.


, , ,

No comments yet.

Leave a Reply