Spring

[Spring] Spring Bean

gogi masidda 2023. 8. 28. 17:09

 

빈(Bean)

빈(Bean)은 스프링 컨테이너에 의해 관리되는 재사용 가능한 소프트웨어 컴포넌트이다.

즉, 스프링 컨테이너가 관리하는 자바 객체를 뜻하며, 하나 이상의 빈(Bean)을 관리한다.

빈은 인스턴스화된 객체를 의미하며, 스프링 컨테이너에 등록된 객체를 스프링 빈이라고 한다.

@Bean 어노테이션을 통해 메서드로부터 반환된 객체를 스프링 컨테이너에 등록한다.

스프링컨테이너에는 메서드 이름이 자동으로 등록된다. 이렇게 등록된 객체를 스프링 빈이라 한다.

물론 메서드가 등록되는 이름을 바꿀 수 있다.

빈은 클래스의 등록 정보, Getter/Setter 메서드를 포함하며, 컨테이너에 사용되는 설정 메타데이터로 생성된다.

설정 메타데이터 : XML 또는 자바 어노테이션, 자바 코드로 표현하며, 컨테이너의 명령과 인스턴스화, 설정, 조립할 객체 등을 정의한다.

스프링 컨테이너는 @Configuration이 붙은 것을 설정 정보로 사용한다.

  • getBean() 메서드를 통해 bean의 인스턴스를 가져올 수 있다.
  • ApplicationContext 인터페이스는 bean을 가져오는 여러 가지 방법들이 있다.

Bean 가져오기

//App02HelloWorldSpring.java
package com.in28minutes.learnspringframework;

import com.in28minutes.learnspringframework.game.GameRunner;
import com.in28minutes.learnspringframework.game.SuperContraGame;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App02HelloWorldSpring {
    public static void main(String[] args) {
        //1: Launch a Spring Context -

        var context =
                new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
        //2: Configure the things that we want Spring to manage - @Configuration
        //HelloWorldConfiguration - @Configuration
        //name - @Bean

        //3: Retrieving Beans managed by Spring
        System.out.println(context.getBean("name"));
        System.out.println(context.getBean("age"));
        System.out.println(context.getBean("person"));
        System.out.println(context.getBean("person2MethodCall"));
        System.out.println(context.getBean("person3Parameters"));
        System.out.println(context.getBean("address2"));
        //System.out.println(context.getBean(Address.class));
    }
}
//HelloWorldConfiguration.java
package com.in28minutes.learnspringframework;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

record Person (String name, int age, Address address) { };
record Address(String firstLine, String city) { };

@Configuration
public class HelloWorldConfiguration {
    @Bean
    public String name() {
        return "Dongjun";
    }

    @Bean
    public int age() {
        return 24;
    }

    @Bean
    public Person person() {
        return new Person("Ravi", 22,new Address("Main Street", "Utrecht"));
    }
    @Bean
    public Person person2MethodCall() {
        return new Person(name(), age(), address());
    }

    @Bean
    public Person person3Parameters(String name, int age, Address address3) {
        return new Person(name, age, address3);
    }

    @Bean(name = "address2")
    public Address address() {
        return new Address("Baker Street", "London");
    }

    @Bean(name = "address3")
    public Address address3() {
        return new Address("Dongjak", "Seoul");
    }
}
728x90

'Spring' 카테고리의 다른 글

[Spring] Singleton Pattern  (0) 2023.08.31
[Spring] Spring Container  (0) 2023.08.29
객체 지향 프로그래밍  (0) 2023.08.25
[Spring] Dependency Injection  (0) 2023.08.21
[Spring]@Primary vs @Qualifier  (0) 2023.08.21