Spring Bean & μ£Όμ (Injection)
Bean & Injection
-
BeanSpring 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 InjectionSpring 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 InjectionBeanμ μμ±μμμ μ£Όμ μ λ°λ λ°©λ²μ΄λ€. μ½λμ μμλ λ€μκ³Ό κ°λ€.@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κ°μ²΄λ‘ λ£μ΄μ£Όμ) ν μ€νΈλ₯Ό μμνκ² ν μ μλ€.
-
-
λκΈλ¨κΈ°κΈ°