π μ€νλ§ λΉ(Bean) μ΄λ?
λΉ(Bean)μ μ€νλ§ μ»¨ν μ΄λμ μν΄ κ΄λ¦¬λλ μ¬μ¬μ© κ°λ₯ν μννΈμ¨μ΄ μ»΄ν¬λνΈμ΄λ€.
μ¦, μ€νλ§ μ»¨ν μ΄λκ° κ΄λ¦¬νλ μλ° κ°μ²΄λ₯Ό λ»νλ©°, νλ μ΄μμ λΉ(Bean)μ κ΄λ¦¬νλ€.
λΉμ μΈμ€ν΄μ€νλ κ°μ²΄λ₯Ό μλ―Ένλ©°, μ€νλ§ μ»¨ν μ΄λμ λ±λ‘λ κ°μ²΄λ₯Ό μ€νλ§ λΉμ΄λΌκ³ νλ€.
μ½κ² μ΄ν΄νμλ©΄ new ν€μλ λμ μ¬μ©νλ€κ³ 보면λλ€.
<bean id="helloService" class="com.example.myapp.di.HelloService"/>
IHelloService helloService = new IHelloService()
IHelloService λΌλ μΈν°νμ΄μ€κ° μ‘΄μ¬νκ³ helloService μΈμ€ν΄μ€λ₯Ό λ§λ€κΈ° μν΄μλ new λΌλ ν€μλλ₯Ό μ¬μ©ν΄ μΈμ€ν΄μ€λ₯Ό μμ±νμλ€.
μ€νλ§ μ»¨ν μ΄λκ° κ΄λ¦¬νλλ‘ νλ λ°©λ²μ λΉ νκ·Έλ₯Ό μ¬μ©νλ©΄ λλ€.
μ΄λ κ² xml νμΌ λΉ νκ·Έλ₯Ό μΆκ°νλ©΄ helloService μΈμ€ν΄μ€λ₯Ό μ€νλ§ μ»¨ν μ΄λκ° κ΄λ¦¬νκ² λλ€.
β μ€νλ§ λΉ(Bean) μ¬μ©μ΄μ ?
κ°μ₯ ν° μ΄μ λ μ€νλ§ κ° κ°μ²΄κ° μμ‘΄κ΄κ³λ₯Ό κ΄λ¦¬νλλ‘ νλ κ²μ κ°μ₯ ν° λͺ©μ μ΄ μλ€. κ°μ²΄κ° μμ‘΄κ΄κ³λ₯Ό λ±λ‘ν λ μ€νλ§ μ»¨ν μ΄λμμ ν΄λΉνλ λΉμ μ°Ύκ³ , κ·Έ λΉκ³Ό μμ‘΄μ±μ λ§λ λ€.
π μ€νλ§ λΉ(Bean) λ±λ‘ λ°©λ²
Spring Beanμ λ±λ‘νλ λ°©λ²μ λνμ μΌλ‘ 3κ°μ§ μ λκ° μλ€.
- xmlμ μ§μ λ±λ‘
- @Bean μ΄λ Έν μ΄μ μ μ΄μ©
- @Component, @Controller, @Service, @Repository μ΄λ Έν μ΄μ μ μ΄μ©
1. XMLμ μ§μ λ±λ‘νλ λ°©λ²
<bean> νκ·Έλ₯Ό μ¬μ©νλ κ²μ΄λ€.
<bean id="helloService" class="com.example.myapp.di.HelloService"/>
<bean id="helloController" class="com.example.myapp.di.HelloController" p:helloService-ref="helloService">
</bean>
application-config.xml μ resource μλμ λ§λ€μ΄ μ€λ€. κ·Έλ¦¬κ³ <beans> νκ·Έ μμ <bean>μ λ§λ€μ΄ μ€λ€. μμ μ½λλ μμ‘΄μ± μ£Όμ κΉμ§ ν μ½λμ΄λ€.
2. @Bean μ΄λ Έν μ΄μ μ μ΄μ©
package com.example.myapp.di;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@Configurable
@ComponentScan(basePackages= {"com.example.myapp"})
@ImportResource(value= {"classpath:application-config.xml"})
public class AppConfig {
@Bean
public IHelloService helloService() {
return new HelloService();
}
@Bean
public HelloController helloController() {
HelloController controller = new HelloController();
controller.setHelloService(helloService());
return controller;
}
}
μΈμ€ν΄μ€λ₯Ό λ°ννκ³ λΉμ λ±λ‘νλ μ½λμ΄λ€.
λ©μλ μμ @Beanνκ·Έλ₯Ό μ¬μ©νκ³ AppConfig κ°μ²΄ μμ @Configurable, @ComponentScan, @ImportResource μ΄λ Έν μ΄μ μ μ μΈν΄μ€λ€.
μ΄λ Έν μ΄μ μ μ¬μ©νκΈ° μν΄ κΌ application-xmlμμ contextλ₯Ό μΆκ°ν΄μ£Όλ κ²μ κΉλ¨Ήμ§ λ§μ !!
package com.example.myapp.di;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class HelloMain {
public static void main(String[] args) {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println("-----------------------");
HelloController controller = context.getBean("helloController", HelloController.class);
controller.hello("νκΈΈλ");
System.out.println("=====================");
context.close();
}
}
κ·Έλ¦¬κ³ λ©μΈμμ Annotationμ μ¬μ©νκΈ° μν΄ AnnotationConfigApplicationContext λ₯Ό μ¬μ©νλ€.
3. @Component, @Controller, @Service, @Repository μ΄λ Έν μ΄μ μ μ΄μ©
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.example.myapp.hr"/>
</beans>
application-config.xml νμΌ
μ¬κΈ°μ namespaceμμ context μΆκ°νκ³ μλμ contextλ₯Ό μΆκ°ν΄μ€λ€.
package com.example.myapp.hr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
@Controller
public class EmpController {
private IEmpService empService;
@Autowired
public EmpController(IEmpService empService) {
this.empService = empService;
}
void printInfo() {
int count = empService.getEmpCount(50);
System.out.println("μ¬μμ μ : " + count);
}
}
@Controllerμ @Autowiredλ₯Ό μ΄μ©ν΄ μμ‘΄μ± μ£Όμ κΉμ§ ν μ½λμ΄λ€.
'π» BackEnd > π’ Spring | SpringBoot' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
π’ [Spring] μ€νλ§ μμ‘΄μ± μ£Όμ (DI) - [1] (0) | 2023.07.06 |
---|---|
π’ [Spring] μ€νλ§ νμΌ μ λ‘λ (0) | 2023.06.30 |
π’ [Spring] μ€νλ§ μ€μ νμΌ (XML) (0) | 2023.06.28 |
π’ Spring Boot μμνκΈ° (0) | 2023.03.09 |
μΈν 리μ μ΄(Intellij) νμ λΌμ΄μΌμ€ κ°±μ (0) | 2023.03.08 |