본문 바로가기

WEB/SPRING

토비2 1.3 의존성 주입방법

1. @Autowired

 

1-1 필드 주입

public class Hello{
	@Autowired
    private Printer printer;
}

 

 

1-2 메서드 주입

public class Hello{
	private Printer printer;
    
    @Autowired
    public void setPrinter(Printer printer){
    	this.printer = printer;
    }
}

 

1-3 생성자 주입

public class BasSqlService implements SqlService{
	protected SqlReader sqlReader;
    protected SqlRegistry sqlRegistry;
    
    @Autowired
    public BasSqlService(SqlReader sqlReader, SqlRegistry sqlRegistry){
    	this.sqlReader = sqlReader;
        this.sqlRegistry = sqlRegistry;
    }
}