[Java] bean 등록을 하지 않는 객체에서 bean 등록 객체 사용하기.
new로 객체 생성을 했을 때, 그 생성한 객체를 통해 bean 등록이 되어있는 service 객체를 사용하고 싶을 때가 있다. 예를 들어, Thread 안에서 서비스 객체를 불러서 쓰고 싶을 때가 그런 경우인데. 이때 Thread 클래스에서 그냥 @Autowired할 경우 nullpointerexcecption이 떨어진다. 그러면 어떻게 해야 하나? 만들어진 서비스 빈을 빈 네임으로 불러서 가져오면 된다. 1. Bean을 가져오기 위해서는 ApplicationContext가 일단 필요하다. import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.sprin..
공장 (factory)/- Programming..
2019. 11. 6. 19:37