p.s*_*eef 10 java spring spring-data-rest spring-boot
我想为实体实现RestRepositoryResource(以及一些额外的标准功能)的自动配置.我试图通过注释类@Configuration或@SpringBootApplication注释类来实现它.
这样的事情:
@EnableRestRepo(single="foo", collection="foos",entity=Foo.class, id=String.class)
@SpringBootApplication
public class App{
    public void main(String[] args){
        SpringApplication.run(App.class,args);
    }
}
@Entity
public class Foo{
    String id;
    String bar;   
    ... getters & setters   
}
Run Code Online (Sandbox Code Playgroud)
这应该设置一个(或类似的功能,如果需要,我可以创建自己的端点),@RestRepositoryResource如下所示:
@RestRepositoryResource(itemResourceRel = "foo", collectionResourceRel = "foos")
public interface Repo extends CrudRepository<Foo,String> {
    @RestResource(rel = "foo")
    Foo findOneById(@Param("id") String id);
}
Run Code Online (Sandbox Code Playgroud)
这里的目标是在配置一些基本功能时减少一些锅炉板.显然,这个例子将扩展一些更自动配置的东西,但这应该以类似的方式工作.
问题不RestRepositoryResource在于关于使用需要参数和泛型类的注释的自动配置.我不介意花一些时间实现这个,但我不知道从哪里开始.
这样的事情是否可能,如果是这样,怎么样?
不确定我是否 100% 理解您的意思,但是这里的示例代码运行良好并基于注释创建 beans 运行时。注释也有一些元数据。
通用接口,稍后会被代理:
public interface GenericRepository<T extends GenericType, Long> extends JpaRepository<GenericType, Long> {
Run Code Online (Sandbox Code Playgroud)
}
对不同实体进行注释:
@Target(ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@OverrideAutoConfiguration(enabled = false)
@ImportAutoConfiguration
@Import({RestResourceAutoConfiguration.class})
public @interface EnableRestRepo {
  Class<?> entity();
  String id();
}
Run Code Online (Sandbox Code Playgroud)
一个可以在运行时注册bean的配置类:
@Configuration
@AutoConfigureAfter(value = WebMvcAutoConfiguration.class)
@ConditionalOnClass({CrudRepository.class})
public class RestResourceAutoConfiguration implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
      Reflections reflections = new Reflections("jav");
      Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(EnableRestRepo.class);
      for (Class<?> c : annotated) {
        EnableRestRepo declaredAnnotation = c.getDeclaredAnnotation(EnableRestRepo.class);
        Class<?> entity = declaredAnnotation.entity();
        String id = declaredAnnotation.id();
        Supplier<GenericRepository> genericRepositorySupplier = () -> (GenericRepository) Proxy.newProxyInstance( // register a proxy of the generic type in spring context
                c.getClassLoader(),
                new Class[]{GenericRepository.class},
                new MyInvocationHandler(entity));
            beanDefinitionRegistry.registerBeanDefinition(id + "-" + UUID.randomUUID().toString(),
                    new RootBeanDefinition(GenericRepository.class, genericRepositorySupplier)
        );
    }
}
Run Code Online (Sandbox Code Playgroud)
META-INF 下的 spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
jav.RestResourceAutoConfiguration
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           203 次  |  
        
|   最近记录:  |