Spring 실행 될 때 초기에 한 번 호출하여
메모리에 저장 후 사용하고 싶을 때
@PostConstruct 을 사용해주면 됩니다.
@Component
public class PostConTest {
@Autowired
Mapper mapper;
public static String str;
public static ResDao resDao;
@PostConstruct
public void init(){
str = "testtest";
resDao = mapper.selectTest();
}
}
@Component 어노테이션을 가진 독립적인 클래스를 생성해줍니다.
디버깅을 해보시면 Spring was가 다 실행되기 전에
우선 init() 함수가 실행 됩니다.
이때 @PostContuct가 없었다면 @Autowired Mapper는 null로 들어오지만
선언을 해준다면 정상 작동하는 것을 확인 할 수 있습니다.
@Service
public class PostConSerivce(){
public void selectInfo(){
PostConTest pc = new PostConTest();
String aaa = pc.str;
ResDao resDao = pc.resDao;
}
}
@PostConstruct로 실행되어 static으로 저장해놓은 변수를
다른 클래스에서 활용하여 사용할 수 있습니다.
aaa = 'testtest'로 정상 출력됩니다.
이상 포스트 마칩니다.