Spring Bean & μ£Όμ (Injection)
Bean & Injection
-
Bean
Spring Framework
μμBean
μ΄λSpring IoC
컨ν μ΄λκ° κ΄λ¦¬νλ μλ° κ°μ²΄λ₯Ό λ»νλ€. κ°λ¨νκ² μ€λͺ νμλ©΄, μλ°μnew
μ°μ°μλ₯Ό ν΅ν΄ μμ±νλ κ°μ²΄λBean
μ΄λΌκ³ λ§ν μ μλ€. κ°νΉSpring
κΈ°λ°μ νλ‘μ νΈμμ λΉμ¦λμ€ λ‘μ§μ λ΄κ³ μλ λ©μλλ₯Ό κ°μ§ ν΄λμ€μ μλμ κ°μ μ½λλ₯Ό λ³Ό μ μμ κ²μ΄λ€.@Autowired private TestBean testBean; public void λ©μλ() { this.testBean.doSomething(); }
testBean
μ΄λΌλTestBean
μ κ°μ²΄ λ³μλSpring IoC
컨ν μ΄λλ₯Ό ν΅ν΄ κ°μ²΄λ₯Όμ£Όμ
λ°μ μ¬μ©λλ κ²μ΄λ€.κ·Έλ λ€λ©΄ βμ΄λ»κ²β
Spring IoC
컨ν μ΄λμBean
μ λ±λ‘νλμ§ λνμ μΈ λ°©λ²μ λ€μκ³Ό κ°λ€.-
Java Annotation
Annotation
μΌλ‘Bean
μ λ±λ‘ ν λμλ, λ±λ‘νλ €λ ν΄λμ€μ@Component
,@Controller
,@Service
,@Repository
μ΄λ Έν μ΄μ μ΄ νμ°μ μΌλ‘ λͺ μλμ΄μΌ νλ€. ν΄λΉAnnotation
μ κΈ°μ¬νλ©΄μ,Spring IoC
컨ν μ΄λμ μ΄ ν΄λμ€λ₯Ό λΉμΌλ‘ λ±λ‘νκ² λ€ λΌκ³ λͺ μνλκ²μ΄λ€.@Component public class TestBean { private String name; ... } @Controller public class TestController { ... } @Service public class TestService { ... }
Spring
κΈ°μ΄ μμ@Component
,@Controller
,@Service
,@Repository
λ λ€ λκ°λ€?λ°μ λ§κ³ λ°μ ν리λ€. κΈ°λ₯μ μΌλ‘λ λͺ¨λ
Bean
κ°μ²΄μ μ΄λ€ μ΄λ Έν μ΄μ μ μ¬μ©νλ, μ¬μ€μ μ λΆ@Component
λ₯Ό μ¬μ©ν΄λ λ‘μ§μ ꡬμ±νλλ°μ λ³ λ¬Έμ λ μλ€.νμ§λ§
Spring Framework
μ체λ νμ κ³Ό μμ°μ± ν₯μμ μν΄μ λ§λ€μ΄μ§ νλ μμν¬μ΄λ©°(μ΄ μΈμλ μμ μ±, 보μμ± λ±λ± λ§μ κ²μ΄ μλ€.),κ°λ° νλ‘μ νΈμμλ λμ μλ€λΌλ¦¬ βμΈμ΄βκ° μλ βμ½λβλ‘ μν΅ν΄μΌ νλ κ²½μ°κ° λΉλ²νλ€.
κΈ°λ₯μ΄ λλ©΄ κ·Έλ₯ κ·Έκ±° μ°μ§ νκ³ λμ΄κ°μ§ λ§κ³ , λ°λμ
Bean
μ΄ λ¬΄μ¨ μν μ νκ³ μ΄λ€ λ μ΄μ΄μ μν΄μμ΄μΌ νλμ§ μ΄λ Έν μ΄μ μΌλ‘ μκΈ°μμΌμ£Όμ. -
XML Config
λ¨λμ§μ μ μΌλ‘ λ§νλ©΄, νμλ ν΄λΉ λ°©λ²μ μ νΈνμ§ μλλ€. νμ§λ§ μμ§λ λ§μ νμ¬λ€μ μΉμλΉμ€μλ
XML Config
νμμ Beanμ΄ λ±λ‘λμ΄ μμ μ μκ³ , κ°μ ν΄μΌ λλ νλ‘μ νΈμλ μμ νλ₯ μ΄ μκΈ° λλ¬Έμ μΈμ€μ λͺ°λΌλ βμ½μμ€μ μμμΌβ νλ λ±λ‘ λ°©λ²μ΄λ€.μ€νλ§ κΈ°λ°μ νλ‘μ νΈμμ, μ£Όλ‘ μλμ κ°μ
xml
νμΌμ΄ μ‘΄μ¬ ν κ²μ΄λ€. (νμΌλͺ μ λ€ λ€λ₯Ό μκ° μλ€.){projectDir}/src/main/resources/application.xml
μ£Όλ‘ λ€μκ³Ό κ°μ νμμΌλ‘ 무μΈκ°
<bean>
νκ·Έλ€μ΄ μ‘΄μ¬νλ€.<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="testBean" class="com.component.TestBean"> <property name="name" value="μ΅μ¬νΈ"/> </bean> </beans>
λ€μκ³Ό κ°μ΄
xml
μTestBean
λ±λ‘ ν,public class DemoApplication { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml"); TestBean testBean= ctx.getBean(TestBean.class); System.out.println(testBean.getName()); } }
λΆλ¬μμ
Name
μ μΆλ ₯νλ©΄,<property>
νκ·Έμμ μ λ ₯ν μ΄λ¦μ΄ μΆλ ₯λλ€.
-
-
Dpendency Injection
Spring Framework
μμ μμ‘΄μ± μ£Όμ (Dependency Injection
μ΄νDI
)μ΄λ,Bean
μΌλ‘ λ±λ‘λ ν΄λμ€λ₯ΌIoc
컨ν μ΄λλ‘λΆν° λ겨λ°λκ²μ μλ―Ένλ€. μ λ λ‘μ§μμμ βμμ±β λμ§ μλλ€.DI
μ λ°©λ²μ μΈκ°μ§κ° μλλ° λ€μκ³Ό κ°λ€.-
Field Injection
μ€λ¬΄μμ κ°μ₯ μμ£Ό 보μ΄λ
DI
λ°©μμ΄λ©°, μ¬μ€ λ°μ§κ³ 보면 μ μΌ νΈλ¦¬ν λ°©λ²μ΄λ€. μμBean
μ€λͺ μ μλ μ½λλ₯Ό 보면, λ€μκ³Ό κ°μ΄ μ¬μ©νλ€.@Service public class TestService { @Autowired private TestBean testBean; }
νμ§λ§ μ΄ λ°©λ²μ μΆμ²νμ§ μμΌλ©°, μ΄μ λ μλμ
Constructor Injection
μμ νμ νλλ‘ νλ€. -
Setter Injection
νμκ° κ°λ°μλ‘μμ μ°μ°¨λ μ€λλμ§ μμκ³ , κ²½νλ μ μ§λ§, μ€λ¬΄μμ νλ²λ λ³΄μ§ λͺ»ν λ°©μμ΄λ€.
@Service public class TestService { private TestBean testBean; @Autowired public void setTestBean(TestBean testBean) { this.testBean = testBean; } }
ν΄λΉ λ°©λ²λ
Field Injection
κ³Ό ν¨κ» μΆμ²νμ§ μλλ€. μ΄μ λ λ§μ°¬κ°μ§λ‘ νμ νλ€. -
Constructor Injection
Bean
μ μμ±μμμ μ£Όμ μ λ°λ λ°©λ²μ΄λ€. μ½λμ μμλ λ€μκ³Ό κ°λ€.@Service public class TestService { private final TestBean testBean; public void TestService(TestBean testBean) { this.testBean = testBean; } }
Constructor Injection
μ μ¬μ©ν΄μΌ νλ μ΄μ λ λ€μκ³Ό κ°λ€.-
final
μ μΈ κ°λ₯μ²μλΆν° μ§κΈκΉμ§ κΈμ μ μ½μλ€λ©΄ μμ λ
DI
κ·Ήλͺ νκ² λ€λ₯Έ μ°¨μ΄μ μ΄ λ³΄μΌκ²μ΄λ€. κ·Έκ²μfinal
μ μΈμ΄ κ°λ₯ν΄ μ‘λ€λ κ²μ΄λ€. κ°μ²΄λ₯Όfinal
λ‘ μ μΈν¨μΌλ‘, λ°νμμμ ν΄λΉ κ°μ²΄μ λΆλ³μ±μ β보μ₯β ν΄ μ€μ μλ€. -
μνμ°Έμ‘° λ°©μ§
Constructor Injection
λ°©μκ³Ό,Field Injection
λBean
μ μ£Όμ νλ μμ μμ²΄κ° λ€λ₯΄λ€.Field Injection
μμλBean
μ μμ± ν ν, μ£Όμ νλ €λBean
μ μ°Ύλ λ°©μμ΄κ³ ,Constructor Injection
λ°©μμ,Arguments
μBean
μ λ¨Όμ μ°Ύλλ€. λμ΄ λ¨Όμ λ, λ¬κ±μ΄ λ¨Όμ λμ μ°¨μ΄μ§λ§,Constructor Injection
μμλRuntime
μΌλ‘ λμ΄κ°κΈ° μ μνμ°Έμ‘° μλ¬λ₯Ό μΊμΉν΄λΌ μ μλ€. -
ν μ€νΈ μ½λ μμ± μ©μ΄
Test
μ½λλ₯Ό μμ± ν λ,Field Injection
λ°©μμ μ¬μ©νBean
μ ν μ€νΈ νλ €λ©΄,Spring Ioc
컨ν μ΄λλ₯Ό μ°Έμ‘°ν΄μΌνκ³ , λΆλ¬μμΌνλ μμ‘΄μ±μ΄ νλκ°κ° μλλ°,Constructor Injection
λ°©μμBean
μ λ¨μνnew
μ°μ°μλ₯Ό ν΅ν΄ κ°μ²΄λ₯Ό μμ±νμ¬(μΈμλ‘ μ¬μ©λλBean
μMock
κ°μ²΄λ‘ λ£μ΄μ£Όμ) ν μ€νΈλ₯Ό μμνκ² ν μ μλ€.
-
-
λκΈλ¨κΈ°κΈ°