Spring – 10 – Autowiring with Component Scan Annotations
Merhaba Arkadaslar
Bu bolumde Auto Components Scan Annotation ‘lari kullanarak autowiring islemini gerceklestirmeyi inceleyecegiz. Bu annotation’lar org.springframework.stereotype paketinde yer alan ;
- @Component
- @Repository
- @Service
- @Controller
Bu annotation’larin hepsi ayni isi yapmaktadir. Kullanim yerlerine gore okunabilirlik (readability) acisindan ilgili annotation’i kullanmak daha dogru olacaktir.
@Component , Auto scan component siniflarini temsil eder.
@Repository , Persistence Layer / Kalicilik Katmani icin DAO siniflari icin kullanilir.
@Service , Business Layer icin Service siniflari icin kullanilir
@Controller , Presentation Layer icin Controller siniflari icin kullanilir.
Layered Architecture’i (Katmanli Mimari) dusundugumuzde Controller , Service Layer’a erismeli , Service Layer da DAO Layer’a erismelidir. Tam tersi erisim uygun degildir , ya da View Layer’dan , Web Layer’dan (Controller) , DAO Layer’a erisim uygun degildir.
DAO Layer
EmployeeDAO.java
EmployeeDAO interface’inde basit bir insert metodu olsun.
package _13.autowiring.stereotype.dao; import _13.autowiring.stereotype.model.Employee; public interface EmployeeDAO { public void insertEmployee(Employee employee); }
EmployeeDAOImpl.java
EmployeeDAOImpl class’i , EmployeeDAO interface’ini uygulasin (implements)
package _13.autowiring.stereotype.dao; import org.springframework.stereotype.Repository; import _13.autowiring.stereotype.model.Employee; @Repository // default olarak value sinif ismi ile aynidir camelCase yapida olur. // @Repository(value="employeeDAOImpl") public class EmployeeDAOImpl implements EmployeeDAO { // dao methods @Override public void insertEmployee(Employee employee) { System.out.println("EmployeeDAOImpl - insertEmployee"); System.out.println(employee); } }
EmployeeDAO , Persistence Layer/DAO Layer sinifi oldugu icin @Repository annotation’i kullanmak uygun olacaktir.
Service Layer
EmployeeService.java
EmployeeService , Service Layer interface’si olsun.
package _13.autowiring.stereotype.service; import _13.autowiring.stereotype.model.Employee; public interface EmployeeService { public void insertEmployee(Employee employee); }
EmployeeServiceImpl.java
EmployeeServiceImpl sinifi Business Layer sinifi olacagi icin @Service annotation’i kullanmak uygun olacaktir.
package _13.autowiring.stereotype.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import _13.autowiring.stereotype.dao.EmployeeDAO; import _13.autowiring.stereotype.model.Employee; @Service // default olarak value sinif ismi ile aynidir camelCase yapida olur. // @Service(value="employeeServiceImpl") public class EmployeeServiceImpl implements EmployeeService { @Autowired // @Autowired annotation icin setter metoda ihtiyac yoktur. reflection // kullanilarak set edilir. private EmployeeDAO employeeDAO; @Override public void insertEmployee(Employee employee) { System.out.println("EmployeeServiceImpl - insertEmployee"); employeeDAO.insertEmployee(employee); } // more methods can be added. }
Domain Layer
Domain Layer’da Address , Employee ve Department class’larimiz olacak. Bu bolumde yapacagimiz @Autowired islemleri test amacli olacak , genel olarak bu tarz siniflar icin @Autowired pek uygun olmayacaktir.
Address.java
package _13.autowiring.stereotype.model; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component //default olarak value sinif ismi ile aynidir camelCase yapida olur. //@Component(value="address") public class Address { @Value(value="Istanbul") private String city; //getters and setters ... }
Address sinifi basinda @Component annotation kullaniyoruz.
@Value annotation ile annotation yardimi ile inject islemini yapabiliriz. Model icin bu pek uygun olmayacaktir burada bilgi amacli gosteriyorum.
Department.java
package _13.autowiring.stereotype.model; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component //default olarak value sinif ismi ile aynidir camelCase yapida olur. //@Component(value="department") public class Department { @Resource(name = "departmentId") private String departmentName; //getters and setters .... }
@Resource annotation ile XML dosyamizda yer alan bean’i Annotation yardimi ile inject ediyoruz.
Employee.java
package _13.autowiring.stereotype.model; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component //default olarak value sinif ismi ile aynidir camelCase yapida olur. //@Component(value="employee") public class Employee { @Value(value = "Levent") private String name; @Value(value = "Erguder") private String surname; private Department department; @Autowired public Employee(Department department) { this.department = department; } @Autowired private Address address; //getters and setters .... }
@Autowired annotation’i property/instance variable ve constructor ile kullanabiliriz.
13.autowiring.sterotype.xml
context:component-scan ve @Autowired annotation ile Spring’in autowiring islemini gerceklestirmesini saglariz.
<?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="_13.autowiring.stereotype.service , _13.autowiring.stereotype.dao , _13.autowiring.stereotype.model" /> <!-- @Resource annotation ile kullanabiliriz. --> <bean id="departmentId" class="java.lang.String"> <constructor-arg value="Software Department" /> </bean> </beans>
AutowiringSterotypeTest.java
package _13.autowiring.sterotype.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import _13.autowiring.stereotype.model.Employee; import _13.autowiring.stereotype.service.EmployeeServiceImpl; public class AutowiringSterotypeTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("13.autowiring.sterotype.xml"); // employeeServiceImpl is default! // employeeServiceImpl , varsayilan olarak camelCase yapida isimlendirme // kullanilir. // EmployeeServiceImpl sinifinin basinda @Service annotation olduguna // dikkat edelim ! EmployeeServiceImpl employeeService = ctx.getBean("employeeServiceImpl", EmployeeServiceImpl.class); // Ayni sekilde Employee sinifinda da @Component olarak annotation // kullandik. // varsayilan isim ; "employee olacaktir " --> // @Component(value="employee") Employee employee = ctx.getBean("employee", Employee.class); employeeService.insertEmployee(employee); ((ClassPathXmlApplicationContext) ctx).close(); } }
getBean metoduna arguman olarak employeeService , employee String degerlerini veriyoruz. Eger annotation’larda value attribute kullanmazsak bu durumda varsayilan olarak camelCase yapisinda id bilgisi verilir.
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
Leave a Reply