6/5

리무 rimu ·2023년 6월 5일

Spring

목록 보기
4/9

멤버변수 : 객체를 구성하는 정보

스프링에게 관리되는 객체 Bean
Bean안에 관리하고 있는 객체들 중에서 가져다 줌! (의존성 주입)
의존성은 스프링이 관리~😀😀

@(Annotation) 은 인터페이스로 만듬
@끼리도 상속이 됨
기본적으로 가지고 있는 개념이 이걸 bean으로 등록합니다 라는 것
bean으로 등록하는 방법 중 제일 간단한게 @ 를 붙이는 것
내가 코드 수정하려면 @ 붙이면 됨
같은 걸 여러개 등록 불가 하나만 등록 가능!

근데 내가 만든거만 쓰는게 아님
api에 있는 클래스 객체(외부 라이브러리)를 쓸 수도 있고 다른 누군가가 만든 클래스 객체를 사용하는 경우에는 xml 파일로 등록해야 함!
기본적으로 singlton으로 되어있음

3.1.1을 사용하는 이유?
회사에서 저 버전을 쓰기 때문

controller
service

면접 잡히면
면접확인서 내기
하루에 하나만 내면 됨 (직인 꼭 받아오기, 없으면 명함이라도 받아오기)

<?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 https://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	<!-- id가 zealot1인 객체를 빈으로 등록하라. 단 생성자를 통해 이름은 "Zealot1", 체력은 120으로 설정한다.-->
	<bean id = "zealot1" class = "org.green.first.Zealot">
	<!-- index가 붙으면 순서 설정 가능~! -->
		<constructor-arg value="zealot1" index = "0" />
		<constructor-arg value="120" index = "1" />
	</bean>
</beans>
package org.green.second;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class BeanMaker {
	// 리턴하는 객체가 bean이 됨 some이 bean으로 등록됨
	@Bean
	public Some some1st() {
		return new Some();
	}
	
	@Bean
	public Some some2nd() {
		return new Some(100);
	}
	
	@Bean
	public SomeContainer someContainer() {
		return new SomeContainer(some1st());
	}
	
}

이미 코드 실행되기전에 root-context에서 객체를 생성 해 놓기 때문에
new Some 이 두번 호출되는거임

https://mvnrepository.com/artifact/cglib/cglib/2.2.2

<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>
</dependency>

여기다 붙이기 pom.xml

이렇게하면 자동으로 들어간다~

Maven을 내장하고 있음

@Configuration
객체를 두번 만들지 않음

rc610 22kf

<!--
scope 
	singleton : default
	prototype : 매번 새로 생성
    request : 요청시 마다 생성
	session : 세션 바뀌면 새로 만들겠다!
-->
<bean id = foo" class = "org.doo.bean.Foo" scope = "singleton"/>

이 bean은 singleton으로 관리하겠다!

JMDI : 자원의 리소스 위치가 바껴도 설정하는 곳에서 바꾸면 다 따라바뀜~

servlet-context.xml

<!-- Enables the Spring MVC @Controller programming model -->
<!-- @기반으로 컨트롤러를 저렇게 표시할거야 저렇게 표시하면 돼!, 우리가 따로 할 필욘 X-->
<!-- context:component-scan base-package 여기를 스캔해~  -->
<annotation-driven />

즉, <context:component-scan base-package="org.green.second" />
여기서 찾아! 라는 의미

di - injection의 의미 : 누가 넣어주는거~
멤버변수에 대한 의존성을 스프링이 알아서 해줌
scope(범위) : 언제까지 유지할것인지

ioc(제어의 역전) : 이 프로그램의 구조, 어떻게 사용되는지 라이브러리는 개발자가, 프레임워크는 자체적인 룰이 있음 (누가 주도권을 쥐고 누가 흐름을 제어하는가)

공부하기
https://school.programmers.co.kr/learn/challenges?order=recent&levels=1&page=1

profile
JAVA / SQL / Spring 을 공부하고 있습니다 🐥

0개의 댓글