다음과 같이 스프링 부트 응용 프로그램의 다른 패키지에서 찾을 수있는 3 개의 클래스가 있습니다. @Autowired
은 왜 특정 클래스에서만 작동합니까? 스프링 부트 Autowired 실패 - null
@Configuration
public class Configurations{
@Autowired
Prop prop; //works fine
@Bean
//other bean definitions
}
@Component
public class Prop{
public void method(){};
}
public class User{
@Autowired
Prop prop; //does not work, null
public void doWork(){
prop.method();
}
}
나는 또한
@PostConstruct
을 시도하지만, 봄은 클래스 자체가 스프링 빈해야한다고 감지하면 같은 결과
public class User{
@Autowired
Prop prop; //does not work, null
@PostConstruct
public void doWork(){
prop.method();
}
}
bean annotation을 사용하여 bean annotation을 변경 한 후에 bean annotation을 사용하고 다른 메소드에서 사용할 수있는 방법으로 구성을 만들면 bean public Prop prop() {...} –