π‘ Spring FrameWork νμ κ°λ
μ λ΅ ν¨ν΄μ΄λ κ°κ°μ μκ³ λ¦¬μ¦μ κ΅νμ΄ κ°λ₯νλλ‘ μ μ, μΊ‘μνλ₯Ό ν λ€μ, μλ‘ κ΅νν΄μ μ¬μ©ν μ μλ ν¨ν΄μ΄λ€. μ¦, κ°μ²΄λ€μ΄ ν μ μλ λμμ κ°κ°μ μ λ΅μΌλ‘ λ§λ€μ΄ λκ³ λμ μΌλ‘ λμμ λ³κ²½ν΄μΌ νλ€λ©΄ μ λ΅λ§ λ³κ²½νμ¬ λμμ΄ λ°λλλ‘ νλ ν¨ν΄μ΄λ€.
β λμ¨ν κ²°ν©κ³Ό μ μ΄μ μμ μ μ©
μ λ΅ ν¨ν΄μ κΈ°λ₯λ€μ΄ μ€λ³΅ ꡬνλλ κ²μ λ§κΈ° μν΄ μ¬λ¬ κ°μ§ κΈ°λ₯μ κΈ°λ₯λ³λ‘ λλμ΄ νλμ λ©μ΄λ¦¬(ν΄λμ€)λ‘ λ§λ λ€. μ΄λ‘μ¨ μ λ΅(κΈ°λ₯)μ κ°λ³ κ°μ²΄μ λ§κ² μ¬λ¬ κ°μ§λ‘ μ‘°ν©νμ¬ μ¬μ©ν μ μκ² ν΄μ€λ€. ν νλ¦Ώ λ©μλ ν¨ν΄μΌλ‘λ μμ κ·Έλ¦Όμ ꡬνν΄λΌ μ μλ€. μ λ΅ ν¨ν΄μ Springμ DIλ₯Ό μ νν νννλ€.
λ κ°μ§ μμ‘΄μ±μ μνμν΄μΌλ‘μ¨ μ λ΅ ν¨ν΄μ΄ μμ±λλ€.
1. κ°μ²΄ μμ± μμ‘΄μ± : IoCν¨μΌλ‘μ¨ ν΄μ κ°λ₯
2. νμ
μμ‘΄μ± : Interfaceλ₯Ό νμ©ν¨μΌλ‘μ¨ λμ¨ν κ²°ν©μ νμ¬ ν΄μ κ°λ₯
μ΄ λ κ°μ²΄ μμ± μμ‘΄μ±κ³Ό κ΄λ ¨νμ¬ 'μμ‘΄μ± μ£Όμ
' λ°©λ²μλ λ κ°μ§ λ°©λ²μ΄ μλ€.
1) μμ±μμ κ°μ²΄λ₯Ό λ£μ΄μ£Όλ©΄ Constructor Injection
2) μ€μ μμ κ°μ²΄λ₯Ό λ£μ΄μ£Όλ©΄ Property Injection/Setter Injection
// μ½λλ‘ μ΄ν΄λ³΄λ©΄ μλμ κ°λ€.
public class Test {
public static void main(String[] args) {
//workμ λν μμ‘΄μ±μ΄ Employeeμμ Testλ‘ μ΄κ΄λ¨.
//μ΄κ²μ΄ IoC (μ μ΄μ μμ )
DoWork work = new DoWork();
//Testκ° μμ±ν work κ°μ²΄λ₯Ό Employeeμ λ£μ΄μ€
//μ΄κ²μ΄ DI (μμ‘΄μ± μ£Όμ
)
Person employee = new Employee(work);
p.daily();
}
}
public interface DoSomething {
public void doSomething();
}
public class DoStudy implements DoSomething{
@Override
public void doSomething() {
System.out.println("곡λΆνλ€.");
}
}
public class DoWork implements DoSomething {
@Override
public void doSomething() {
System.out.println("μμ κ°μ΄ μΌμ νλ€.");
}
}
public interface Eating {
public void eat();
}
public class EatCafeteria implements Eating{
@Override
public void eat() {
System.out.println("κΈμμ λ¨Ήλλ€.");
}
}
public class EatWellStory implements Eating{
@Override
public void eat() {
System.out.println("μ°μ€ν 리λ₯Ό λ¨Ήλλ€.");
}
}
public class Person {
private String name;
private DoSomething doSomething;
private Eating eating;
public void setDoSomething(DoSomething doSomething) {
this.doSomething = doSomething;
}
public void setEating(Eating eating) {
this.eating = eating;
}
public void daily() {
System.out.println("μ
μ€μ²΄ν¬λ₯Ό νλ€.");
doSomething.doSomething();
eating.eat();
System.out.println("ν΄μ€μ²΄ν¬λ₯Ό νλ€.");
}
}
public class PersonFactory {
// μ μ΄μ μμ
static DoSomething work = new DoWork();
static DoSomething study = new DoStudy();
static Eating cafeteria = new EatCafeteria();
static Eating wellStory = new EatWellStory();
public static Person getInstance(String type) {
Person p = new Person();
switch (type) {
case "Employee":
// μμ‘΄μ± μ£Όμ
(μμ‘΄ κ΄κ³ μ°κ²°)_Setter Injection
p.setDoSomething(work);
p.setEating(wellStory);
break;
case "Student":
p.setDoSomething(study);
p.setEating(cafeteria);
break;
case "SSAFY":
p.setDoSomething(study);
p.setEating(wellStory);
break;
}
return p;
}
}
π μ μ΄μ μμ μ ν κ³³μ λͺ°μλ£μ΄ ν κ³³μμλ§ λͺ¨λ new κ°μ²΄ μμ±μ΄ μΌμ΄λλ€. κ·Έ ν
κ³³μ PersonFactoryμ΄λ©°, Springμμ 'κ°μ²΄ κ΄λ¦¬ 컨ν
μ΄λ'μ΄λ€. μ΄ κ³³μμ Springμ
κ°μ²΄ μμ±, μμ‘΄κ΄κ³ μ°κ²°μ (μ€μ λ§ ν΄μ£Όλ©΄) μ±
μμ§κ³ μ€ννλ€.
π μΈν°νμ΄μ€λ‘ μΆμνλ₯Ό μν€λ©΄ κ·Έ μΈν°νμ΄μ€λ₯Ό ꡬνν κ°μ²΄λ€μ μ΄λ€ κ²μ΄λ ν΄λΉ
μΈν°νμ΄μ€μ λμ
κ°λ₯νλ€. λμ¨ν κ²°ν©μ νμ¬ νμ
μμ‘΄μ±μ ν΄μν κ²μ΄λ€.