2017-06-19 86 views
1

我正在研究一个正在使用Java 1.7的应用程序。我需要重写一些使用SpringFramework的Java 1.8编写的代码。不幸的是,我不熟悉新版本,我不知道如何重写此代码以使用Java 7 ...使用Java 7中的函数将方法引用转换为lambda

下面的代码部分。

ConfigRepo:

public class ConfigRepo extends RepositoryRestConfigurerAdapter { 

    @Override 
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repoRestConfig) { 
    repoRestConfig.withEntityLookup().forRepository(IConfigRepo.class, (Config config) -> { 
     ConfigPK pk = new ConfigPK(); 
     pk.setScope(config.getId().getScope()); 
     pk.setKey(config.getId().getKey()); 
     return pk; 
    }, IConfigRepo::findOne); 
} 

IConfigRepo:

public interface IConfigRepo extends CrudRepository<Config, ConfigPK> {} 

编辑: 增加了我的代码。

我不确定这是否我做了正确的一部分。我不知道这个Config配置应该如何传递。此外,我不知道我应该用这种方法做参考...

我的版本:

public class ConfigRepo extends RepositoryRestConfigurerAdapter { 

    @Override 
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repoRestConfig) { 
    repoRestConfig.withEntityLookup().forRepository(IConfigRepo.class, new Config() { 
     public ConfigPK prepareConfigPK(Config config) { 
      ConfigPK pk = new ConfigPK(); 
      pk.setScope(config.getId().getScope()); 
      pk.setKey(config.getId().getKey()); 
      return pk; 
     }, IConfigRepo::findOne); 
} 
+0

你的问题是什么? –

+0

我认为这很清楚...我正在寻求帮助将此代码重写到Java 7,因为我无法处理这个问题。 – Lui

+1

我意识到你需要帮助,但你在哪里挣扎?你尝试了什么?什么不行?你的代码在哪里? –

回答

2

功能forRepository似乎接受了三个参数:

  1. 一个Class<IConfigRepo>
  2. 接口实例Converter<Config, ConfigPK>

    public interface Converter<Config, ConfigPK> { 
        ConfigPK convert(Config config); 
    } 
    

    它实际上是一个通用接口,但是我插入了在那里使用的类型。

  3. 另一功能接口Lookup<IConfigRepo, ID>

    public interface Lookup { 
        Object lookup(IConfigRepo repository, ID identifier) 
    } 
    

    同样的通用接口的实例,但我插入使用(除了ID)类型。

因此,兼具功能性接口参数可以改写为匿名类的实例:在你的代码可以代替Java8风格Lambda和方法参考

// Java 8 
(Config config) -> { 
    ConfigPK pk = new ConfigPK(); 
    pk.setScope(config.getId().getScope()); 
    pk.setKey(config.getId().getKey()); 
    return pk; 
} 

//Java 7 
new Converter<Config, ConfigPK>() { 
    @Override 
    public ConfigPK convert(Config config) { 
     ConfigPK pk = new ConfigPK(); 
     pk.setScope(config.getId().getScope()); 
     pk.setKey(config.getId().getKey()); 
     return pk; 
    } 
} 

// Java 8 
IConfigRepo::findOne 


// Java 7 
// ??? because I don't know your type for ID 
new Lookup<IConfigRepo, ???>() { 
    @Override 
    public Object lookup(IConfigRepo repository, ??? identifier) { 
      return repo.findOne(); 
    } 
} 

作为我写在那里的参数

+0

基本上,forRepository方法来自Spring Data REST - [link](http://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest/core/config/ EntityLookupRegistrar.html#forRepository-java.lang.Class-org.springframework.core.convert.converter.Converter-org.springframework.data.rest.core.config.EntityLookupRegistrar.LookupRegistrar.Lookup-) – Lui

+0

我是否需要更新接口在我的答案,或者你可以做转移? – danielspaniol

+0

如果你可以这样做,特别是对于最后一个参数...因为这个方法的声明来自CrudRepository接口并且实现在SimpleJpaRepository中。说实话,这对我来说很难... 什么是“new MethodName()”的那些名称?对于第二个参数,它应该是新的Config()?而对于第三个参数新的CrudRepository()? – Lui

相关问题