Is there a mechanism in Spring which provides a way for automation of generic beans construction?
For, example if I have a class definition like:
class Foo<T> {
private final T type;
...
}
and dependency like:
@Autowired
private Foo<String> foo;
I'd like to use some mechanism in Spring which provides, in some form, T from dependency definition (in above example, String) and provides a way for automatic instance creation?
By defining this in your Spring config :
@Bean
public Foo<String> foo() {
return new Foo<>("bar");
}
Or instead of the value, you can specify the type :
@Bean
public Foo<String> foo() {
return new Foo<>(String.class);
}
If you use Spring Boot, you can add @ConditionalOnProperty
or @ConditionalOnBean
to dynamize the bean instantiation.
(update) If you want to avoid declaration, you should extend DefaultListableBeanFactory
and ApplicationContext
. Here is a working example with Spring Boot :
@Controller
@EnableAutoConfiguration
public class BeanFactoryTest {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(BeanFactoryTest.class);
app.setApplicationContextClass(CustomAppContext.class);
app.run(args);
}
@Autowired
private Foo<String> foo1;
@Autowired
private Foo<String> foo2;
@Autowired
private Foo<Integer> foo3;
@PostConstruct
public void initialize() {
System.out.println(foo1); // prints BeanFactoryTest$Foo@344b8190
System.out.println(foo2); // prints BeanFactoryTest$Foo@344b8190
System.out.println(foo3); // prints BeanFactoryTest$Foo@5b69d40d
}
public static class CustomAppContext extends AnnotationConfigApplicationContext {
public CustomAppContext() {
super(new CustomBeanFactory());
}
}
public static class CustomBeanFactory extends DefaultListableBeanFactory {
@Override
protected Map<String, Object> findAutowireCandidates(String beanName, Class<?> requiredType, DependencyDescriptor descriptor) {
Map<String, Object> map = super.findAutowireCandidates(beanName, requiredType, descriptor);
if (Foo.class.isAssignableFrom(requiredType)) {
ResolvableType type = ResolvableType.forField(descriptor.getField());
ResolvableType genericType = type.getGeneric(0);
Class<?> genericTypeRaw = genericType.getRawClass();
boolean hasInstance =
map.values()
.parallelStream()
.map(Foo.class::cast)
.map(Foo::getType)
.filter(genericTypeRaw::isAssignableFrom)
.findFirst()
.isPresent();
if (!hasInstance) {
super.registerResolvableDependency(requiredType, new Foo<>(genericTypeRaw));
map = super.findAutowireCandidates(beanName, requiredType, descriptor);
}
}
return map;
}
}
public static class Foo<T> {
private final Class<T> type;
public Foo(Class<T> type) {
this.type = type;
}
public Class<T> getType() {
return type;
}
}
}
It seems you can use the ResolvableType in Spring 4. http://docs.spring.io/spring/docs/4.0.0.RC2/javadoc-api/org/springframework/core/ResolvableType.html There is also a detail sample https://spring.io/blog/2013/12/03/spring-framework-4-0-and-java-generics
Hope this help.
The question is a little old, but doesn't contain an answer with example, so...
Using ResolvableType :
public <E> Foo<E> produceFoo(final InjectionPoint ip) {
ResolvableType resolved = ResolvableType.forField(ip.getField());
Foo<E> fooInstance = new FooImpl<>();
Class<E> parameterClass = (Class<E>) resolved.getGeneric(0).resolve();
fooInstance.doSomethingWithParametrized(parameterClass);
return fooInstance;
}
InjectionPoint cames from: import org.springframework.beans.factory.InjectionPoint;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.