Spring – 12 – Java Based Configuration

Merhaba Arkadaslar,
Bu bolumde Java Based Configuration ile  injection islemini nasil yaptigimizi inceleyecegiz.
Burada inceleyecegimiz annotation’lar ve class’lar

  • @Configuration
  • @Bean
  • @ComponentScan
  • @ImportResource
  • @PropertyResource
  • @Lazy
  • AnnotationConfigApplicationContext

@Configuration annotation’i , tanimlandigi sinifin bean tanimlamalari icin kaynak oldugunu ve Spring IoC container  tarafindan kullanilacagini belirtir.

@Bean annotation da <bean> tagina karsilik gelmektedir.
@Lazy annotation’i XML konfigurasyonunda <bean> taniminda kullanabilecegimiz lazy-init e karsilik gelmektedir. false oldugu durumda bean objesi startup zamaninda olusturulacaktir , true oldugu durumda bean objesine ihtiyac oldugu durumda ilgili obje olusturulacaktir.
Eger @Lazy annotation’i ya da XML konfigurasyonu yoksa startupta olusturulur (eagerly)

Konfigurasyon sinifini yuklemek(load) icin AnnotationConfigApplicationContext sinifini kullanabiliriz.

JavaBasedConfiguration.java

package _15.java.based.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import _15.java.based.model.Employee;

@Configuration
@PropertySource(value = "classpath:15.java.based.properties")
@ComponentScan(basePackages = "_15.java.based.model")
@ImportResource(value = "classpath:15.java.based.xml")
public class JavaBasedConfiguration {

	@Autowired
	Environment environment;

	@Bean
        @Lazy(value=true)
	public Employee getEmployee() {
		String name = environment.getProperty("name");
		String surname = environment.getProperty("surname");
		return new Employee(name, surname);
	}

}

  • JavaBasedConfiguration sinifimizda @Configuration annotation’i tanimladik. Boylelikle bu sinifin bean tanimlamalari icin kaynak oldugunu ve Spring IoC tarafindan kullanilacagini belirtiyoruz.
  • @PropertySource annotation , Spring ApplicationContext ‘e propertites dosyalarini yuklemek(load) icin kullanilir.

15.java.based.properties

name=Levent
surname=Erguder
  • @ComponentScan annotation <context:component-scan> taginin yaptigi isi yapar. Employee sinifinda yer alan @Autowired annotation’larin calismasi icin ekliyoruz. Boylelikle Address ve Department objelerini Spring inject edebiliyor.
  • @ImportResource annotation ile konfigurasyon icin XML dosyasi ekliyoruz. Burada 15.java.based.xml dosyasini import ediyoruz. Boylelikle Department sinifinda yer alan @Resource annotation ilgili bean tanimina ulasabilir.
  • org.springframework.core.env.Environment ile getProperty metodunu kullaniyoruz. Boylece properties dosyamizdaki name/value ciftlerine ulasabiliriz.

Employee.java

package _15.java.based.model;

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

public class Employee {

	private String name;
	private String surname;

	@Autowired
	private Address address;

	@Autowired
	private Department department;

	public Employee(String name, String surname) {
		super();
		this.name = name;
		this.surname = surname;
	}
....
}

Address.java

package _15.java.based.model;

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

@Component
public class Address {

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

Department.java

package _15.java.based.model;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

@Component
public class Department {

	@Resource(name = "departmentId")
	private String departmentName;

	public String getDepartmentName() {
		return departmentName;
	}
        //getters and setters
...
}

JavaBasedConfigurationTest.java
Dikkat edecek olursak AnnotationConfigApplicationContext sinifini kullaniyoruz.

package _15.java.based.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import _15.java.based.configuration.JavaBasedConfiguration;
import _15.java.based.model.Employee;

public class JavaBasedConfigurationTest {

	public static void main(String[] args) {

		ApplicationContext ctx = new AnnotationConfigApplicationContext(JavaBasedConfiguration.class);		
		Employee employee = ctx.getBean( Employee.class);				
		System.out.println(employee);
		
		((AnnotationConfigApplicationContext) ctx).close();
		
	}
}

15.java.based.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="departmentId" class="java.lang.String">
		<constructor-arg value="Software Department" />
	</bean>

</beans>

Ornegimizi calistirdigimizda;

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

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 *