스프링 프로젝트 생성 및 실행
1. SpringApplication 생성(new SpringApplication())
SpringApplication의 다양한 Property 설정
2. SpringApplication.run()
실제 스프링 동작에 필요한 정보들을 생성하고 가져오고 메모리에 올리는 과정
@SpringBootConfiguration 어노테이션이 붙어있는 클래스를 스프링부트에 대한 설정정보가 담긴 클래스로 인식하고, 해당클래스 내부에 @Bean 어노테이션이 담긴 메소드를 찾아 빈을 등록하고 생성해주며 싱글톤 패턴을 유지할 수 있도록 돕는 역할
레고를 만들 때 편리한 점
스프링을 쓰면 좋은점
의존성 주입
제어의 역전
그래도 생성 전과 생성 후에 뭔가 하고 싶은게 있다면?
CallBack함수란?
Bean LifeCycle
Xml로 한번 등록해보자
@Configuration
public class ApplicationConfig {
@Bean
public ConveniencePayService conveniencePayService(){
return new ConveniencePayService(
discountByConvenience(),
new HashSet<>(
Arrays.asList(moneyAdapter(), cardAdapter())
)
);
}
@Bean
private static CardAdapter cardAdapter() {
return new CardAdapter();
}
@Bean
private static MoneyAdapter moneyAdapter() {
return new MoneyAdapter();
}
@Bean
private static DiscountByPayMethod discountByConvenience() {
return new DiscountByPayMethod();
}
}
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="conveniencePayService" class="com.sangyunpark.payservice.service.ConveniencePayService">
<constructor-arg name="paymentInterfaceSet">
<set>
<ref bean="moneyAdapter"/>
<ref bean="cardAdapter"/>
</set>
</constructor-arg>
<constructor-arg name="discountInterface" ref="discountByConvenience"/>
</bean>
<bean id="cardAdapter" class="com.sangyunpark.payservice.service.CardAdapter"/>
<bean id="moneyAdapter" class="com.sangyunpark.payservice.service.MoneyAdapter"/>
<bean id="discountByConvenience" class="com.sangyunpark.payservice.service.DiscountByConvenience"/>
<bean id="discountByPayMethod" class="com.sangyunpark.payservice.service.DiscountByPayMethod"/>
</beans>
생성자가 없는 경우에는 빈 tag를 사용해준다.
ApplicationContext applicationContext = new AnnotationConfigApplicationContext("spring-config.xml"); // context로 만들어!
xml로 등록한 후에도 bean이 잘 실행된다.
<?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">
<context:compnent-scan base-package="com.sangyunpark.payservice"/>
</beans>
위와 같이 단 한줄로 구성이 가능하다.(payservice 하위에있는 모든 패키지들을 등록하겠다는 소리)
어노테이션이 없는 경우에는 빈으로 등록이되지 않는다! 꼭 어노테이션을 달아주어야한다.
package com.sangyunpark.payservice.config;
import com.sangyunpark.payservice.service.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.HashSet;
@Configuration
public class ApplicationConfig {
@Bean
public ConveniencePayService conveniencePayService(){
return new ConveniencePayService(
discountByConvenience(),
new HashSet<>(
Arrays.asList(moneyAdapter(), cardAdapter())
)
);
}
@Bean
private static CardAdapter cardAdapter() {
return new CardAdapter();
}
@Bean
private static MoneyAdapter moneyAdapter() {
return new MoneyAdapter();
}
@Bean
private static DiscountByPayMethod discountByConvenience() {
return new DiscountByPayMethod();
}
}
@Configuration
@ComponentScan(basePackages = "com.sangyunpark.payservice")
public class ApplicationConfig {
}
@Primary
public class DiscountByPayMethod implements DiscountInterface{
@Qualifier("discountByConvenience") DiscountInterface discountInterface,
paymentInterfaceSet.forEach(
paymentInterface -> paymentInterfaceMap.put(paymentInterface.getPayMethodType(), paymentInterface)
);
@Scope("singleton")
ConveniencePayService conveniencePayService =
applicationContext.getBean("conveniencePayService",
ConveniencePayService.class);
System.out.println("conveniencePayService : " + conveniencePayService);
ConveniencePayService conveniencePayService2 =
applicationContext.getBean("conveniencePayService",
ConveniencePayService.class);
System.out.println("conveniencePayService : " + conveniencePayService2);
ConveniencePayService conveniencePayService3 =
applicationContext.getBean("conveniencePayService",
ConveniencePayService.class);
System.out.println("conveniencePayService : " + conveniencePayService3);
출력결과
conveniencePayService : com.sangyunpark.payservice.service.ConveniencePayService@189aa67a
conveniencePayService : com.sangyunpark.payservice.service.ConveniencePayService@189aa67a
conveniencePayService : com.sangyunpark.payservice.service.ConveniencePayService@189aa67a
@Scope("prototype")
Prototype인 경우 출력 결과
conveniencePayService : com.sangyunpark.payservice.service.ConveniencePayService@56113384
conveniencePayService : com.sangyunpark.payservice.service.ConveniencePayService@373ebf74
conveniencePayService : com.sangyunpark.payservice.service.ConveniencePayService@c4ed84
(1) Request : 요청에 따라 계속 새로 만듦
(2) Session : 세션 마다 계속 새로 만듦
(3) WebSocket : 자주 사용되지는 않는다.
클래스 단위
메서드 단위
@Bean @Profile("test")
-Dspring.profiles.active=sandbox, beta, production
프로파일 표현식
@Profile("!production")
!not, &and, |or