0
Spring 5は、GenericApplicationContext#registerBean
を使用してプログラムによる登録を行います。残念なことに、このメソッドはClass
しか受け付けず、ParameterizedType
ではなく、汎用の型付きのBeanを取得できます。Spring 5:プログラムで汎用Beanを登録します。
ジェネリックBeanの登録方法は他にありますか?
Spring 5は、GenericApplicationContext#registerBean
を使用してプログラムによる登録を行います。残念なことに、このメソッドはClass
しか受け付けず、ParameterizedType
ではなく、汎用の型付きのBeanを取得できます。Spring 5:プログラムで汎用Beanを登録します。
ジェネリックBeanの登録方法は他にありますか?
プログラムで豆を登録するためのいくつかのファクトリメソッドがあります。
ConfigurableApplicationContext
からConfigurableBeanFactory
を使用し、registerSingleton
を使用してクラスタイプのBeanを登録することもできます。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext=SpringApplication.run(Application.class, args);
ConfigurableBeanFactory beanFactory=applicationContext.getBeanFactory();
beanFactory.registerSingleton("testBean", new Application().new TestClass<String>());
TestClass<String> testObj=applicationContext.getBean(TestClass.class);
testObj.testMethod("hello");
}
public class TestClass<T>{
public void testMethod(T t) {
System.out.println("Test Method");
}
}
}