πŸ’» BackEnd/🟒 Spring | SpringBoot

🟒 [Spring] μŠ€ν”„λ§ 빈(Bean) μ΄λž€?

Dbswnstjd 2023. 6. 23. 09:31

Spring

πŸ“Œ μŠ€ν”„λ§ 빈(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λ₯Ό μ΄μš©ν•΄ μ˜μ‘΄μ„± μ£Όμž…κΉŒμ§€ ν•œ μ½”λ“œμ΄λ‹€.