@Autowired에서 에러 발생
1689
작성한 질문수 1
package hello.core.scope;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Scope;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
public class SingletonWithPrototypeTest1 {
@Test
void prototypeFind() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class, PrototypeBean.class);
PrototypeBean prototypeBean1 = ac.getBean(PrototypeBean.class);
prototypeBean1.addCount();
assertThat(prototypeBean1.getCount()).isEqualTo(1);
PrototypeBean prototypeBean2 = ac.getBean(PrototypeBean.class);
prototypeBean2.addCount();
assertThat(prototypeBean2.getCount()).isEqualTo(1);
}
@Test
public void singletonClientUsePrototype() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class, PrototypeBean.class);
ClientBean clientBean1 = ac.getBean(ClientBean.class);
int count1 = clientBean1.logic();
assertThat(count1).isEqualTo(1);
ClientBean clientBean2 = ac.getBean(ClientBean.class);
int count2 = clientBean2.logic();
assertThat(count2).isEqualTo(2);
}
@Scope("singleton")
static class ClientBean {
private final PrototypeBean prototypeBean;
@Autowired
public ClientBean(PrototypeBean prototypeBean) {
this.prototypeBean = prototypeBean;
}
public int logic() {
prototypeBean.addCount();
return prototypeBean.getCount();
}
}
@Scope("prototype")
static class PrototypeBean {
private int count = 0;
public void addCount() {
count++;
}
public int getCount() {
return count;
}
@PostConstruct
public void init() {
System.out.println("PrototypeBean.init " + this);
}
@PreDestroy
public void destroy() {
System.out.println("PrototypeBean.destroy");
}
}
}
위 코드에서 @Autowired 부분에서
Error:(43, 10) Autowired members must be defined in valid Spring bean (@Component|@Service|...)
에러가 발생합니다. 스프링 빈으로 등록되어 있지 않아서 발생하는 오류라고 생각됩니다. 하지만 강사님 코드에서는 발생하지 않아서 제가 뭘 빠뜨린 것 같은데 그게 무엇인지 모르겠습니다.
테스트는 돌려보면 잘 작동하고 통과가 됩니다.
답변 1
[영한님]Spring Boot 및 프로젝트 생성
0
39
2
퀴즈가 안열려요
0
71
2
회원 도메인 인터페이스 개발
0
58
1
빈 스코프 개념의 중요성
0
61
1
SingletonService가 JVM이 뜰 때 생성되는게 맞나요?
0
111
2
섹션3. 11 회원객체 다이어그램
0
52
1
OCP, DIP과 @Qualifier 어노테이션에 대해서 질문합니다.
0
57
1
코드 자료
0
108
2
구현체가 동적으로 정해질 때, 팩토리 기법을 사용하나요?
0
104
2
MemberService의 인터페이스를 왜 사용하는지 궁금합니다.
0
126
1
롬복 @Setter를 써야 하는 상황이 있는건가요?
0
120
1
빈 등록 메서드의 파라미터가 빈이 아니어도 되나요?
0
104
1
테스트 속도가 나중에 영향이 있을까요?
0
108
1
gradle 설정 안떠서 질문 남깁니다!
0
210
2
build.gradle로 프로젝트를 여는 이유
0
134
1
provider 사용하는 이유
0
131
1
다음 강의 뭘 들어야 할까요
0
159
2
프로토타입 빈, 직접 destroy 호출 안 할 경우
0
87
1
beanB
0
110
2
퀴즈다시풀기
0
91
1
Gradle로 바꿔도 오류가 똑같이 발생하네요 ㅠㅠ
0
136
2
"중복 등록과 충돌" 강의에서 강사님과 다른 에러가 발생합니다.
0
110
3
run 실행했는데 결과창이 이렇게 뜨네요 왜 그런건가요>
0
148
2
도메인의 정의?
0
76
1





