๐Ÿ’ป 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๋ฅผ ์ด์šฉํ•ด ์˜์กด์„ฑ ์ฃผ์ž…๊นŒ์ง€ ํ•œ ์ฝ”๋“œ์ด๋‹ค.