web 개발/spring

<spring>DI(denpendency injection) , IOC

잼추 2022. 12. 12. 19:13
public class UserServiceImpl implements UserService{
    private static UserServiceImpl instance = null;
    
    private UserServiceImpl(){};
    
    public static UserServiceImpl getinstance(){
        if(instance==null){
            instance = new UserServiceImpl();
        }
        return instance;
    }

** 싱글톤 생성법

 

DI(Dependency injection) 의존성 주입

객체의 의존관계를 외부에서 주입시키는 패턴을 말한다.

class A  와 class B 가 있을 때  A 내부에서 B객체를 필요로 할 때

A 내부에서 new B(); 를 사용하는 것이 아닌

외부에서 B b = new B();를 실행, B 객체를 생성하여 A객체 생성 시 미리 생성되어 있는 B 객체를 주입하는 방식이다.

public A(){
    B b = new B();
}

 

각자가 거의 일체형으로 연결 되어있는 해당 방식 외에 아래 방식을 사용해 각각을 생성하여 조립하는 방식으로 사용

public class A {
    private B b;
    
    public A(B b) {
        this.b = b;
    }
}

 

 

IOC(Inversion of Control)

 

 

'web 개발 > spring' 카테고리의 다른 글

<spring> REST API  (0) 2023.01.12
<spring> get, post  (0) 2022.12.12
<spring> intelij 설정  (0) 2022.12.07
<suvlet> eclipse 초기 세팅  (0) 2022.12.03
http 프로토콜  (0) 2022.11.30