発端は怠けゴコロ
ちょっとした動作確認で、ComponentScan
で自クラスのパッケージ配下のすべてのコンポーネントがスキャンされるのを避けつつ、使いたいコンポーネントだけを DI コンテナに Bean 登録させたいけれど、「Bean定義ファイル」や「新しい JavaConfig クラス」を作るのが面倒臭くなって、勢いで動作確認したいクラスの中に書いてみたら動いてしまったでござるの巻。
StaticNaInnnerClassDeJavaConfig.java
@SpringBootApplication(scanBasePackages = { "undefined.package" })
@Component
public class StaticNaInnnerClassDeJavaConfig {
private final SomeComponent component;
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(StaticNaInnnerClassDeJavaConfig.class, args);
}
public StaticNaInnnerClassDeJavaConfig(SomeComponent component) {
this.component = component;
}
public static class JavaConfig {
@Bean
public SomeComponent someComponent(@Autowired Properties properties) {
return new SomeComponent(properties);
}
}
public static class SomeComponent {
private final String someParam;
public SomeComponent(Properties proeprties) {
this.someParam = properties.getProperty("someParam");
}
}
}