Reputation: 1729
I'm using Spring 4. I have the following Java Config snippet:
@Bean
public Service<FooType> serviceBean() {
return new ServiceImpl<FooType>(FooType.class);
}
And the following Spring Bean:
public class ServiceImpl<T> implements Service {
private final Class<T> clazz;
public ValidateOrderServiceImpl(final Class<T> clazz)
this.clazz = clazz;
}
}
I would prefer to not have to duplicate the type parameter, but if I dont pass in as a constructor, T is null, i.e. the following does not work. Is this expected. I have seen some related posts, which discuss erasure - and also how the type parameter can be used to resolve the correct bean to use - but have not seen a definitive on my question...
// Java Config Snippet (desired)
@Bean
public Service<FooType> serviceBean() {
return new ServiceImpl<FooType>();
}
// Spring Bean (desired)
public class ServiceImpl<T> implements Service {
// T populated..
private final Class<T> clazz;
}
Thanks
Upvotes: 0
Views: 72
Reputation: 280136
What you had originally is fine and (pretty much) the only way to get the Class
object for the corresponding type argument.
You can get rid of the explicit type argument here
@Bean
public Service<FooType> serviceBean() {
return new ServiceImpl<>(FooType.class);
// ^^
}
Upvotes: 1