Bean, Inversion of Control, Dependency Injection

 * Bean - object-dir. Spring terefinden idare oluna bilen bir object. 

Bean scopes:

singleton

prototype

request

session

application

websocket


Define Bean: 1) @Bean 2) stereotype annotation -> @Controller, @RestController,  @Service, @Repository, @Component


* Inversion of Control: IoC Container

package com.example.burhan_lesson_1.config;

import com.example.burhan_lesson_1.UserService;
import com.example.burhan_lesson_1.UserServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

@Bean
public UserService userServiceImpl() {
return new UserServiceImpl();
}
}

Tutaq ki biz bu wekilde class yaratdiqda ve @Configuration iwletdikde o zaman IoC Containerde:

UserService userService = UserServiceImpl(); bir object yaranir.


* @Qualifier: eger bir interface-in birden cox implementasiyasi varsa biz bu annotation-u istifade ede bilerik.

package com.example.burhan_lesson_1;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {

private final UserService userService;

public UserController(@Qualifier("userServiceImpl2") UserService userService) {
this.userService = userService;
}

public void foo() {
userService.demo();
}
}


Injectionun 3 tipi var: 1) Constuctor base injectin   2) Field type injection   3) Setter base injection

 Constuctor base injection:

 package com.example.burhan_lesson_1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BurhanLesson1Application implements CommandLineRunner {

private final UserController userController;

@Autowired
public BurhanLesson1Application(UserController userController) {
this.userController = userController;
}

// @Autowired
// public UserController userController;

public static void main(String[] args) {
SpringApplication.run(BurhanLesson1Application.class, args);
}

@Override
public void run(String... args) throws Exception {
// UserService userService = new UserServiceImpl();
// UserController userController = new UserController(userService);
userController.foo();
}
}


Field type injection:

 package com.example.burhan_lesson_1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BurhanLesson1Application implements CommandLineRunner {

// private final UserController userController;
//
// @Autowired
// public BurhanLesson1Application(UserController userController) {
// this.userController = userController;
// }

@Autowired
public UserController userController;

public static void main(String[] args) {
SpringApplication.run(BurhanLesson1Application.class, args);
}

@Override
public void run(String... args) throws Exception {
// UserService userService = new UserServiceImpl();
// UserController userController = new UserController(userService);
userController.foo();
}
}

Field type injection yalniz cagrilan zaman IoC-den gedib cagrilib inject olunacaq.



Setter type injection:

 package com.example.burhan_lesson_1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BurhanLesson1Application implements CommandLineRunner {

// private final UserController userController;
//
// @Autowired
// public BurhanLesson1Application(UserController userController) {
// this.userController = userController;
// }

// @Autowired
// public UserController userController;

private UserController userController;

@Autowired
public void setUserController(UserController userController) {
this.userController = userController;
}

public static void main(String[] args) {
SpringApplication.run(BurhanLesson1Application.class, args);
}

@Override
public void run(String... args) throws Exception {
// UserService userService = new UserServiceImpl();
// UserController userController = new UserController(userService);
userController.foo();
}
}


* Constuctor base injection ustunluyu ondan ibaretdir ki, application qalxan zaman butun beanlari inject edir.











Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class