无法解决匹配的构造函数 - 重写BaseRepository类

时间:2017-04-05 16:04:20

标签: java spring spring-repositories

我在扩展BaseRepository的接口和类中得到了无法解决匹配的构造函数错误。我已将代码回滚到我只覆盖BaseRepository类的方法但仍然无法确定原因。

通常它是由两个具有相同名称的方法和Spring选择错误的方法引起的,但我不会发现这种情况。 UserPasswordResetToken中的方法由Eclipse IDE生成。我在Right-click-> source-> Override / Implement Methods对话框中选择了要实现的所有类。

我在网上看到的解决方案在设置方法的constructor-arg值时是特定的,但我没有对UserPasswordResetToken类使用xml或java配置。如果我为它做一个bean配置,我不知道在哪里声明构造函数-arg值。我可以编写所有这些代码,但这并不能帮助我了解问题的原因并了解它。我也不知道我在哪里有两个同名的方法来混淆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);    

}

1 个答案:

答案 0 :(得分:0)

坚持......我自己很困惑。我只是重读了堆栈跟踪。我正在研究IUserPasswordTokenRepository类,但堆栈跟踪现在读取 IUserVerificationTokenRepositoryClass 。我修复了PasswordToken类并且没有注意到它!

我之前有两种删除方法,一种是字符串,另一种是令牌参数。我删除了String one并重新考虑了一切。我没有注意到堆栈跟踪中的类名已经改变,因为它们的名称相似。所以我确实看到了这个问题。

感谢您的期待!