Extend class from another extended Parametrized Generic class - spring

I have a genericService support classes ( genericService, GenericServiceImpl, GenericDao,GenericHibernateDao) for generic service,dao layer.
Normaly it works fine, When i extend any type parametrized service from abstract generic services.When i extend this extended services in different service it gives:
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
error.
My GEnericHibernateDao:
#Transactional
public abstract class GenericHibernateDaoSupport extends HibernateDaoSupport implements GenericDaoTemplate
{
private Class type;
public GenericHibernateDaoSupport() {
this.type = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
#Autowired
#Qualifier(value = "productSessionFactory")
public void bindSessionFactory(SessionFactory sessionFactory)
{
setSessionFactory(sessionFactory);
}
public Class<T> getType() {
return type;
}
public void setType(Class<T> type) {
this.type = type;
}
protected Session getCurrentSession() {
return getHibernateTemplate().getSessionFactory().getCurrentSession();
}
public ID persist(T newInstance) {
return (ID) getHibernateTemplate().save(newInstance);
}
public void update(T transientObject) {
getHibernateTemplate().update(transientObject);
}
Here is my standart extended HibernateDao class:
#Repository
public class StandadHibernateDao extends standardHibernateDaoSupport<Standard, Long> implements StandardDao {
Above extend operation works fine, But when i tried something like this:
#Repository
public class ExtendStandardGatewayHibernateDao extends StandadHibernateDao implements ExtendedStandardDao {
it throws :
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType.
Do you have any idea?

Related

How to autowire a method of a generic service

I have a Service class that has a generic type and a setController method that is based on the same generic type. the generic type of the servic object is only known at the time of declaration.
The problem is now when i define a ControllerImpl where the generic type is defined the #Autowired method of setController does not use that component.
Has somebody an idea how to fix it and keep the ServiceImpl generic. (it would work when i define the typ in ServiceImpl as well).
The following example show the problem i'm facing with:
#SpringBootTest
#ActiveProfiles("local")
public class AccessTest {
#Autowired
private ServiceA<BeanA> service;
#Test
void test(){
Assertions.assertNotNull(service.controller);
}
interface ValueGetter{
}
static class BeanA implements ValueGetter{
}
static class AbstractService<B extends ValueGetter>{
Controller<B> controller;
#Autowired
void setController(#Nullable Controller<B> controller){
this.controller = controller;
}
}
interface Controller<B extends ValueGetter>{
void doSomething(B value);
}
//not inner class
#Service
public class ServiceA<B extends AccessTest.ValueGetter> extends AccessTest.AbstractService<B> {
}
//not inner class
#Component
public class ControllerImpl implements AccessTest.Controller<AccessTest.BeanA> {
#Override
public void doSomething(final AccessTest.BeanA value) {
}
}
}

How to autowire an Inteface which extends JpaRepository in an another class

#Repository
public interface Userrepo extends JpaRepository<Users, String> {
}
I want the above interface to be autowired in the below class
#Component
public class Userauth {
#Autowired
Userrepo urepo;
public boolean check(String name,String password) {
Application a=new Application();
Optional<Users> u=urepo.findById(name);
if(!u.isEmpty()) {
Users ud=u.get();
if(ud.getPassword().equals(password))
return true;
}
return false;
}
}
but its giving an error "urepo" is null
in the log getting this.
Ignored because not a concrete top-level class: file [C:\Users\Documents\workspace-spring-tool-suite-4-4.9.0.RELEASE\1Sampleproject\target\classes\com\example\demo\repos\Userrepo.class]

Spring How casting dynamic type extends of Interface

I begening to work with Spring Boot after 14 years in C# and litle issue over to be dificult now.
with activity:
#Component
public class GetBillingUserStatusActivity implements ITask<BillingStatus,
String>, ITasks {
#Override
public BillingStatus Execute(String firstInput) {
return BillingStatus.active;
}}
I have wokflow class that would execute the precedent activity like this:
#Component
public class CreateUserTokenWorkflow {
#Autowired
private ITaskResolver taskResolver;
#Autowired
private GetBillingUserStatusActivity getBillingUserStatusActivity;
public String CreateUserToken() {
var task = taskResolver.ResolveTask(getBillingUserStatusActivity);
BillingStatus response = task.Execute("test");
return response.toString();
}}
and my probleme is now on my TaskResolver class
#Component("taskResolver")
public class TaskResolver implements ITaskResolver {
#Override
public <T extends ITasks> T ResolveTask(ITasks t) {
T type = (T) GenericTypeResolver.resolveType(t.getClass(), t.getClass());
return type;
}
}
T type = (T) GenericTypeResolver.resolveType(t.getClass(), t.getClass()) => casting to ITask failed
ErrorMessage:
class java.lang.Class cannot be cast to class com.consumer.workflow.manager.ITasks (java.lang.Class is in module java.base of loader 'bootstrap'; com.consumer.workflow.manager.ITasks is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader #211f766)

How to register Converter in Spring Data Rest application

I have Spring converter which uses Spring Data REST's component called EnumTranslator
#Component
public class TranslationStringToSpecificationStatusEnumConverter implements Converter<String, Specification.Status> {
private final EnumTranslator enumTranslator;
#Autowired
public TranslationStringToSpecificationStatusEnumConverter(EnumTranslator enumTranslator) {
this.enumTranslator = enumTranslator;
}
#Override
public Specification.Status convert(String source) {
return enumTranslator.fromText(Specification.Status.class, source);
}
}
Recommended way to register such converter is to subclass RepositoryRestConfigurerAdapter as follows:
#Configuration
public class RepositoryRestConfig extends RepositoryRestConfigurerAdapter {
private final TranslationStringToSpecificationStatusEnumConverter converter;
#Autowired
public RepositoryRestConfig(TranslationStringToSpecificationStatusEnumConverter converter) {
this.converter = converter;
}
#Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(converter);
super.configureConversionService(conversionService);
}
}
When I run the Spring Boot application, it fails on the following:
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| translationStringToSpecificationStatusEnumConverter defined in file ...
↑ ↓
| org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration (field java.util.List org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.configurers)
↑ ↓
| repositoryRestConfig defined in file ...
└─────┘
So there is circular bean dependency.
How can I register the converter above so that I don't introduce circular bean dependency?
To make it work:
#Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(String.class, Status.class, new StringToTranslatedEnumConverter<>(Status.class));
super.configureConversionService(conversionService);
}
First I created utility class that help me work with Spring beans in unmanaged objects:
#Component
public final class SpringUtils {
#Autowired private ApplicationContext ctx;
private static SpringUtils instance;
#PostConstruct
private void registerInstance() {
instance = this;
}
public static <T> T getBean(Class<T> clazz) {
return instance.ctx.getBean(clazz);
}
}
Then I created the converter:
public class StringToTranslatedEnumConverter<T extends Enum<T> & TranslatedEnum> implements Converter<String, T> {
private final ConcurrentMapCache cache;
private EnumTranslator enumTranslator;
private Class<T> type;
public StringToTranslatedEnumConverter(Class<T> type) {
this.type = type;
cache = new ConcurrentMapCache(type.getName());
}
#Override
public T convert(String from) {
if (enumTranslator == null) {
enumTranslator = SpringUtils.getBean(EnumTranslator.class);
}
Cache.ValueWrapper wrapper = cache.get(from);
if (wrapper != null) {
//noinspection unchecked
return (T) wrapper.get();
}
T translatedEnum = enumTranslator.fromText(type, from);
cache.put(from, translatedEnum);
return translatedEnum;
}
}
UPDATED
TranslatedEnum - it's interface-marker, used to mark enums which translation is only need.
public interface TranslatedEnum {
}
public enum Status implements TranslatedEnum {
CREATED, DELETED
}
The solution to this problem is Spring Core specific. In order to break circle bean dependency cycle, we have to delay setting converter in RepositoryRestConfig. It can be achieved with setter injection:
#Component
public class RepositoryRestConfig extends RepositoryRestConfigurerAdapter {
private TranslationStringToSpecificationStatusEnumConverter converter;
#Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(converter);
super.configureConversionService(conversionService);
}
#Autowired
public void setConverter(TranslationStringToSpecificationStatusEnumConverter converter) {
this.converter = converter;
}
}
You can find how to solve it in this commit by Greg Turnquist: https://github.com/pmihalcin/custom-converter-in-spring-data-rest/commit/779a6477d76dc77515b3e923079e5a6543242da2

Spring Bean Factory Configuration passing input parameter

I'm trying to create a BeanFactory called TaskBeanFactory that I can Autowire into another prototype class that's running on a thread. I want a different instance of a bean returned by the Factory based on a taskName that i want to pass into it but when i start the application i get a null pointer exception because the taskName is null. I had a look at this article but i'm confused about how I should configure the Factory and then pass in the taskName.
The Factory:
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.stereotype.Component;
#Data
#Component
#NoArgsConstructor
public class TaskBeanFactory extends AbstractFactoryBean<GenericTask>{
private TaskNameEnum taskName;
public TaskBeanFactory(TaskNameEnum taskName) {
setSingleton(false);
}
#Override
public Class<?> getObjectType() {
return GenericTask.class;
}
#Override
protected GenericTask createInstance() throws Exception {
switch (taskName) {
case FILE_OPERATION:
return new FileTask();
case DATA_OPERATION:
return new DataTask();
default:
return new GenericTask();
}
}
}
The classes used by the Factory:
#Data
public class GenericTask {
private String idTask;
public void executeTask(Work work) {};
}
#Component
#Scope(value="prototype")
public class FileTask extends GenericTask {
#Override
public void executeTask(Work work) {
//some processing
}
}
#Component
#Scope(value="prototype")
public class DataTask extends GenericTask {
#Override
public void executeTask(Work work) {
//some processing
}
}
and the thread that's calling the Factory:
#Slf4j
#Data
#Scope("prototype")
#Component
public class WorkerThread implements Runnable {
#Autowired
private TaskBeanFactory taskBeanFactory;
#Autowired
private DataService dataService;
#Override
public void run() {
//iterate a Map of taskIds from the dataService
taskBeanFactory.setTaskName(TaskNameEnum.valueOf(taskEntry.getKey()));
GenericTask genericTask = taskBeanFactory.getObject();
//expecting genericTask to be of Type FileTask if called with one Key
//or of Type DataTask if called with another
}
}
}

Resources