2017-04-05 96 views
1

我得到無法解決匹配構造函數錯誤的接口和類擴展BaseRepository。我已經將代碼回滾到了我只覆蓋BaseRepository類的方法的位置,但仍然無法確定原因。無法解決匹配的構造函數 - 覆蓋BaseRepository類

通常它是由兩個具有相同名稱的方法和Spring選擇錯誤的東西引起的,但我沒有看到發生這種情況。 UserPasswordResetToken中的方法由Eclipse IDE生成。我在右鍵單擊 - >源 - >覆蓋/實現方法對話框中選擇要實現的所有類。

我在網上看到的解決方案具體在設置方法的構造函數參數值,但我沒有爲UserPasswordResetToken類使用xml或java配置。我不知道在哪裏聲明構造函數的參數值,如果我爲它做一個bean配置。我可以編寫所有這些代碼,但這並不能幫助我查看問題的原因並瞭解它。我也沒有看到我有兩個同名的方法來混淆Spring。

我是那些不僅希望得到答案的人,也是未來避免這種情況的方法之一。這條線上的任何建議都會有所幫助。修正的錯誤信息說明哪種方法導致問題也是一種改進。猜猜我對Oracle的人有一個建議。

堆棧跟蹤:

Caused by: java.lang.RuntimeException: 
org.springframework.beans.factory.UnsatisfiedDependencyExcep 
tion: Error creating bean with name 'persistenceJPAConfig': 
Unsatisfied dependency expressed through field 'env'; nested 
exception is 
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 
'IUserVerificationTokenRepository': Could not resolve 
matching constructor (hint: specify index/type/name 
arguments for simple parameters to avoid type ambiguities) 
     at 
io.undertow.servlet.core.DeploymentManagerImpl.deploy(Deploy 
mentManagerImpl.java:236) 
     at 
org.wildfly.extension.undertow.deployment.UndertowDeployment 
Service.startContext(UndertowDeploymentService.java:100) 
     at 
org.wildfly.extension.undertow.deployment.UndertowDeployment 
Service$1.run(UndertowDeploymentService.java:82) 
     ... 6 more 

PersistenceJPAConfiguration類:

@Configuration 
@EnableTransactionManagement 
@PropertySource({ "classpath:persistence-mysql.properties" }) 
@ComponentScan(basePackages = {"com.aexample.persistence"}, 
    includeFilters = @Filter(type = FilterType.REGEX, pattern="com.aexample.persistence.*")) 
@EnableJpaRepositories(basePackages = "com.aexample.persistence.repositories") 
//@EnableJpaAuditing(dateTimeProviderRef = "dateTimeProvider") 
public class PersistenceJPAConfig { 

    @Autowired 
    private Environment env; 

    public PersistenceJPAConfig() { 
     super(); 
    } 

    // beans 
    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
     final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); 
     em.setDataSource(dataSource()); 
     em.setPackagesToScan(new String[] { "com.aexample.persistence.model" }); 

     final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
     em.setJpaVendorAdapter(vendorAdapter); 
     em.setJpaProperties(additionalProperties()); 

     return em; 
    } 

    @Bean(name="dataSource") 
    public DataSource dataSource() { 
     final DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); 
     dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); 
     dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); 
     dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass"))); 

     return dataSource; 
    } 

    @Bean 
    public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) { 
     final JpaTransactionManager transactionManager = new JpaTransactionManager(); 
     transactionManager.setEntityManagerFactory(emf); 
     return transactionManager; 
    } 

    @Bean 
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { 
     return new PersistenceExceptionTranslationPostProcessor(); 
    } 

    @Bean 
    public BCryptPasswordEncoder passwordEncoder(){ 
     return new BCryptPasswordEncoder(); 
    } 

    @Bean 
    public SessionRegistry sessionRegistry(){ 
     return new SessionRegistryImpl(); 
    } 

    final Properties additionalProperties() { 
     final Properties hibernateProperties = new Properties(); 
     hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); 
     hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); 
     // hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); 
     return hibernateProperties; 
    } 

} 

IUserPasswordResetToken接口:

public interface IUserPasswordResetTokenRepository extends BaseRepository<UserPasswordResetToken, Long> { 


    void delete(UserPasswordResetToken token); 

    public List<UserPasswordResetToken> findAll(); 

    public UserPasswordResetToken findOne(Long id); 

    public UserPasswordResetToken save(UserPasswordResetToken persisted); 

    UserPasswordResetToken findByToken(String token); 

    UserPasswordResetToken findByUser(User user); 

    Stream<UserPasswordResetToken> findAllByExpiryDateLessThan(Date now); 

    void deleteByExpiryDateLessThan(Date now); 

    @Modifying 
    @Query("delete from UserPasswordResetToken t where t.expiryDate <= ?1") 
    void deleteAllExpiredSince(Date now); 
} 

UserPasswordResetToken類別:

public abstract class UserPasswordResetTokenRepositoryImpl implements IUserPasswordResetTokenRepository { 

    private IUserPasswordResetTokenRepository repository; 


    /** 
    * @param repository 
    */ 
    public UserPasswordResetTokenRepositoryImpl(IUserPasswordResetTokenRepository repository) { 
     super(); 
     this.repository = repository; 
    } 

    @Override 
    public void delete(UserPasswordResetToken deleted) { 

     repository.delete(deleted); 

    } 

    @Override 
    public List<UserPasswordResetToken> findAll() { 

     List<UserPasswordResetToken> theTokens = repository.findAll(); 

     return theTokens; 
    } 

    @Override 
    public UserPasswordResetToken findOne(Long id) { 

     UserPasswordResetToken theToken = repository.findOne(id); 

     return theToken; 
    } 

    @Override 
    public UserPasswordResetToken save(UserPasswordResetToken persisted) { 

     UserPasswordResetToken theToken = repository.save(persisted); 

     return theToken; 

    } 

    @Override 
    public UserPasswordResetToken findByToken(String token) { 

     UserPasswordResetToken theToken = repository.findByToken(token); 

     return theToken; 
    } 

    @Override 
    public Stream<UserPasswordResetToken> findAllByExpiryDateLessThan(Date now) { 

     Stream<UserPasswordResetToken> theTokenStream = repository.findAllByExpiryDateLessThan(now); 

     return theTokenStream; 

    } 

    @Override 
    public UserPasswordResetToken findByUser(User user) { 

     UserPasswordResetToken theToken = repository.findByUser(user); 

     return theToken; 
    } 

    @Override 
    public void deleteByExpiryDateLessThan(Date now) { 

     repository.deleteByExpiryDateLessThan(now); 

    } 

    @Override 
    public void deleteAllExpiredSince(Date now) { 

     repository.deleteAllExpiredSince(now); 

    } 

} 

BaseRepository類

import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.data.repository.NoRepositoryBean; 
import org.springframework.data.repository.Repository; 


@NoRepositoryBean 
public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID>{ 

    void delete(T deleted); 

    List<T> findAll(); 

    T findOne(ID id); 

    T save(T persisted);  

} 
+0

你能告訴我BaseRepository.class來自哪裏,還是你的實現。如果這是您的實施,請發佈代碼。 –

+0

啊,好點。代碼已發佈。它延伸了彈簧框架。 data.repository類。所以是的,我有接口擴展接口。 –

回答

0

HOLD ON ......我迷惑自己。我只是重讀堆棧跟蹤。我正在研究IUserPasswordTokenRepository類,但堆棧跟蹤現在讀取的內容爲IUserVerificationTokenRepositoryClass。我修復了PasswordToken類並沒有注意到它!

我以前有兩種刪除方法,一種是字符串和一種令牌參數。我刪除了一個字符串並重新運行一切。我沒有注意到堆棧跟蹤中的類名已更改,因爲它們的名稱相似。所以我確實看到了這個問題。

感謝您的期待!