나는 내가이 데이터베이스에 연결 그래서생성자에서 사용할 수 있도록 종속성을 삽입하는 방법이 있습니까?
@Service("b")
Class B{
@Autowired
@Qualifier("a")
A a;
SomeObject c;
public B(){
c = a.getC();
}
같은 다른 객체 B의 건설에 사용하려는
@Component("a")
Class A{
public SomeObject getC(){
return anObject;
}
}
객체가 있습니다. 기본적으로 초기화시 데이터베이스에서 객체 c를로드하고 나중에 데이터베이스에 대한 업데이트를 가져올 수 있습니다. 문제는이 일을 시도하고 다음과 같은 초기화 오류가 발생합니다.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name defined in file
Instantiation of bean failed;
Could not instantiate bean class
Constructor threw exception; nested exception is java.lang.NullPointerException
개체 B가 생성되기 전에 종속성을 주입해야하기 때문에 이것이 가능합니까? 아니면 어떻게해야합니까?
감사합니다.이 기능은 생성자 앞에 Qualifier 주석이없는 Autowired로 작동합니다. – user1809913