Spring – 11 – Autowiring with JSR – 330 Annotations

Merhaba Arkadaslar.
Spring 3.0 versiyonundan itibaren , JSR 330: Dependency Injection for Java spesifikasyonunda tanimlanan @Inject annotation’i desteklemektedir. Bu annotation Spring’te kullandigimiz @Autowired annotation’ina benzer sekilde is yapar.

Oncelikle JSR 330 annotation’larini kullanabilmek icin javax.inject jarini projemize eklememiz gereklidir. Bunun icin Maven dependency tanimini pom.xml dosyamiza ekleyelim.

<properties>
    ...
	<javax.inject.version>1</javax.inject.version>
</properties>

....
<dependency>
	<groupId>javax.inject</groupId>
	<artifactId>javax.inject</artifactId>
	<version>${javax.inject.version}</version>
</dependency>
....

Spring’te tanimlanan stereotype annotation’lar (ornegin @Component ) yerine annotation yerine @Named annotation kullanabiliriz.

JSR 330 sadece singleton ve nonsingleton bean scope’larini destekler , Spring ise ozellikle Web uygulamalarinda kullanisli olacaktir.
Varsayilan olarak JSR 330 bean’leri nonsigleton’dur yani Spring bean’leri icin prototype scope bean’lere karsilik gelmektedir. Singleton bean olarak tanimlamak istiyorsak @Singleton annotation’i kullanabiliriz.

Onceki ornegimizi simdi de JSR330 Annotation’lari ile yapalim ;

DAO Layer

EmployeeDAO.java

package _14.autowiring.jsr330.dao;

import _14.autowiring.jsr330.model.Employee;

public interface EmployeeDAO {
	public void insertEmployee(Employee employee);
}

EmployeeDAOImpl.java

@Repository yerine @Named annotation kullaniyoruz.

package _14.autowiring.jsr330.dao;

import javax.inject.Named;
import _14.autowiring.jsr330.model.Employee;

@Named
public class EmployeeDAOImpl implements EmployeeDAO {

	// dao methods

	@Override
	public void insertEmployee(Employee employee) {
		System.out.println("EmployeeDAOImpl - insertEmployee");
		System.out.println(employee);
	}
}

Service Layer

EmployeeService.java

package _14.autowiring.jsr330.service;

import _14.autowiring.jsr330.model.Employee;

public interface EmployeeService {
	public void insertEmployee(Employee employee);
}

EmployeeServiceImpl.java

@Service annotation yerine @Named annotation ,
@Autowired annotation yerine @Inject annotation kullaniyoruz.

package _14.autowiring.jsr330.service;

import javax.inject.Inject;
import javax.inject.Named;

import _14.autowiring.jsr330.dao.EmployeeDAO;
import _14.autowiring.jsr330.model.Employee;

@Named
public class EmployeeServiceImpl implements EmployeeService {

	@Inject
	private EmployeeDAO employeeDAO;

	@Override
	public void insertEmployee(Employee employee) {
		System.out.println("EmployeeServiceImpl - insertEmployee");
		employeeDAO.insertEmployee(employee);
	}

	// more methods can be added.
}

Domain Layer

Address.java

package _14.autowiring.jsr330.model;

import javax.inject.Named;
import javax.inject.Singleton;

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

@Named
@Singleton
public class Address {

	@Value(value="Istanbul")
	private String city;
        //getters and setters
...
}

Department.java

package _14.autowiring.jsr330.model;

import javax.annotation.Resource;
import javax.inject.Named;

@Named
public class Department {

	@Resource(name = "departmentId")
	private String departmentName;
        //getters and setters
...
}

Address.java

package _14.autowiring.jsr330.model;

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

import javax.inject.Inject;
import javax.inject.Named;

@Named(value = "employeeId")
public class Employee {

	@Value(value = "Levent")
	private String name;
	@Value(value = "Erguder")
	private String surname;
	private Department department;

	@Inject
	public Employee(Department department) {
		this.department = department;
	}

	@Inject
	private Address address;
        //getters and setters

@Autowired annotation yerine @Inject annotation kullaniyoruz.

14.autowiring.jsr330.xml

<?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:component-scan base-package="_14.autowiring.jsr330.model , 
					      _14.autowiring.jsr330.dao
					     _14.autowiring.jsr330.service " />

	<!-- @Resource annotation ile kullanabiliriz. -->
	<bean id="departmentId" class="java.lang.String">
		<constructor-arg value="Software Department" />
	</bean>

</beans>

AutowiringJSR330Test.java

package _14.autowiring.jsr330.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import _14.autowiring.jsr330.model.Employee;
import _14.autowiring.jsr330.service.EmployeeServiceImpl;


public class AutowiringJSR330Test {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("14.autowiring.jsr330.xml");	
		
		Employee employee = ctx.getBean("employeeId", Employee.class);				
		
		EmployeeServiceImpl employeeService = ctx.getBean("employeeServiceImpl", EmployeeServiceImpl.class);

		employeeService.insertEmployee(employee);

		
		((ClassPathXmlApplicationContext) ctx).close();
		
	}
}

Ornegimizi calistirdigimizda ;

EmployeeServiceImpl - insertEmployee
EmployeeDAOImpl - insertEmployee
Employee [name=Levent, surname=Erguder, department=Department [departmentName=Software Department], address=Address [city=Istanbul]]

Github kaynak dosyalar/ source folder
leventerguder/injavawetrust-spring-tutorial

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *