私は、次のようにスプリングブートアプリケーションの異なるパッケージにある3つのクラスを持っています: @Autowired
はなぜ特定のクラスでのみ動作しますか? SpringブートAutowiredに失敗しました - ヌル
@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
を試してみましたが、春には、クラス自体がのSpring Beanであることを検出した場合と同じ結果
public class User{
@Autowired
Prop prop; //does not work, null
@PostConstruct
public void doWork(){
prop.method();
}
}
その後、Beanアノテーションを使用してBeanアノテーションを使用し、他のメソッドでも使用できるようにコンフィグレーションでメソッドを作成することができます。bean public Prop prop(){...} –