简体   繁体   English

如何在Spring Boot中为通用类提供类类型?

[英]How to provide the classtype for generic classes in spring-boot?

I'd like to create a little framework, and want to provide some default classes that should be loaded is the user does not provide an implementation class. 我想创建一个小的框架,并希望提供一些应该加载的默认类,因为用户没有提供实现类。

This behviour shall be similar to the spring-boot . 这种行为应类似于spring-boot

Problem: Many of the classes I'd like to create should be generic, and would just required the classtype to be defined by the user. 问题:我想创建的许多类都应该是通用的,并且只需要由用户定义类类型即可。

Anyhow this forces the user to explicit implement each of these classes and provide the classtype. 无论如何,这迫使用户显式实现这些类中的每一个并提供类类型。

Is there any way I could word around this? 我有什么办法可以解决这个问题吗? How could the user provide the classtype that should be used on the generics, without having to explicit implement the generic class? 用户如何提供必须在泛型上使用的类类型,而不必显式实现泛型类? Is that possible? 那可能吗?

framework: 框架:

@Configuration
public class AppConfig {

    @Bean
    @ConditionalOnMissingBean
    public BaseValidator<T extends MyReq> defaultValidator() {
        return new BaseValidator<T extends MyReq>();
    }
}

custom app: 自定义应用:

@Configuration
@EnableAutoConfiguration
@Import(AppConfig.class)
public CustomConfig {

}

//I'd like to prevent that the user has to implement each generic class as follows: //我想防止用户必须实现每个通用类,如下所示:

@Component
public class MyValidator extends BaseValidator<CustomReq> {

}

Is it possible that the user just provides the CustomReq.class , and the BaseValidator is automatically instaniated with that provided classtype? 用户是否可能仅提供CustomReq.class ,并且BaseValidator会自动使用提供的类类型实例化?

You can overcome it with Class<T extends MyReq> constructor arg, like this: 您可以使用Class<T extends MyReq>构造函数arg克服它,如下所示:

public class BeanPropertyRowMapper<T> implements RowMapper<T> {

     private Class<T> mappedClass;

     public BeanPropertyRowMapper(Class<T> mappedClass) {
        initialize(mappedClass);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring-Boot:在Intellij中进行调试-如何查看重新加载的类的名称 - Spring-Boot: Debugging in Intellij - how to see name of reloaded classes Spring-Boot需要构造函数中的泛型类型 - Spring-Boot Generic type in constructor needed Spring-Boot MapStruct 通用实现 - Spring-Boot MapStruct Generic implementation 如何通过Spring-boot 1.3.0.RC1为oauth2提供自定义安全配置 - How to provide custom security configuration for oauth2 with spring-boot 1.3.0.RC1 如何在 spring-boot 应用程序中为 @Query(value=xyz) 提供最终值 - How to provide final value for @Query(value=xyz) in spring-boot application 使用spring-boot内部@Controllers和@RequestParam提供搜索方法 - use spring-boot internal @Controllers with @RequestParam to provide search method 通过Spring-Boot同时提供JSON响应并下载文件 - Provide JSON response and download file simultaneously with Spring-Boot intellij - spring-boot未被识别(未映射的Spring-boot注释配置类) - intellij - spring-boot is not being recognised (unmapped Spring-boot annotated configuration classes) 如何创建一个 spring/spring-boot 库? - How to create a spring/spring-boot library? 通用和通用html thyemleaf数据表页面spring-boot java - common and generic html thyemleaf datatable page spring-boot java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM