-
728x90
Bean
스프링에서 빈이란 “ Pojo 기반의 자바 객체이다 “
POJO(Plain Old Java Object) -
주로 특정 자바 모델이나 기능, 프레임워크를 따르지 않는 Java Object를 지칭하며,Java Bean 객체가 대표적이다.
간단하게 getter / setter를 생각하면 될 것 같다.
Bean 과 자바 객체의 차이점
Bean - 스프링 IoC 컨테이너가 관리하는 자바 객체
자바 객체 - 스프링 IoC 컨테이너가 관리하지 않는 자바 객체도 포함한다
결론, 자바 객체는 Bean + 스프링 IoC 컨테이너가 관리하지 않는 자바 객체 이다 ( Bean < 자바 객체 )
Bean 등록 방법
- 🌈등록하려는 객체를 XML에 설정한다.
<?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="memberService"class="me.wordbe.springgoahead.MemberService"> <property name="memberRepository" ref="memberRepository" /> </bean> <bean id="memberRepository"class="me.wordbe.springgoahead.MemberRepository" /> </beans>
Service 내부에 Repository가 존재하기 때문에 property로 repository를 등록한 것이다.
2. 🌈Component Scan스프링이 알아서 찾아서 하라는 것이다. xml에 component을 사용한다고 설정해준다.
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="me.wordbe.springgoahead" /> </beans>
이렇게 설정하면, 다음과 같이 "@Component"를 설정해준 객체를 빈에 등록한다.
@Component public class MemberService{ }
3. 🌈Java Configxml을 사용하지 않으려고 사용한 방법이다.
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ApplicationConfig { @Bean public MemberRepository memberRepository() { return new memberRepository(); } @Bean public MemberService memberService(MemberRepository memberRepository) { MemverService memberService = new memberService(); memberService.setMemberRepository(memberRepository); return memberService; } }
4. 🌈Java Config + Component Scan
Java Config에 BEAN을 하나씩 입력해야하는 불편함을 없애기 위한 방법이다.
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackageClasses = SpringApplication.class) public class ApplicationConfig { }
이렇게 하면 "@Component"를 모두 찾아서 Bean 등록해준다.
5. 🌈springboot의 bean 설정
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { String[] beanDefinitionNames = ctx.getBeanDefinitionNames(); Arrays.stream(beanDefinitionNames).forEach( System.out::println ); }; } }
spring boot에서는 위와 같이 해놓았다. "@SpringBootApplication"에 "@ComponentScan"과 "@Configuration"이 있다.그래서 "@SpringBootApplication"을 적어주면 알아서 스캔, 등록을 다해준다.
참고
https://velog.io/@jkijki12/Spring-스프링-Bean-IoC-Container-DI가-뭔데
'CS' 카테고리의 다른 글
[CS] 스프링부트를 쓰는 이유와 자동설정 원리 (0) 2023.01.05 [CS] AOP (0) 2023.01.04 [CS] 멀티 스레드 (0) 2022.12.27 [CS] 클라우드 (2) 2022.12.27 [CS] 추상클래스, 인터페이스 (0) 2022.12.27