存储库的Spring JPA数据扫描不起作用

时间:2016-08-15 14:53:16

标签: java spring hibernate spring-mvc spring-data-jpa

好像我已经禁用了jpa存储库。有这个错误:

  

严重:StandardWrapper.Throwable   org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为' userController'的bean时出错在文件[C:\ Users \ jasiu \ workspace2.metadata.plugins \ org.eclipse.wst.server.core \ tmp0 \ wtpwebapps \ BuyMyTime \ WEB-INF \ classes \ web \ UserController.class]中定义:通过表达的不满意的依赖关系构造函数参数0:没有为依赖项[data.UserRepository]找到类型为[data.UserRepository]的限定bean:期望至少有一个bean符合此依赖项的autowire候选者。依赖注释:{};嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项[data.UserRepository]的[data.UserRepository]类型的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{}       在org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)

配置:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages={"data"})
@ComponentScan(basePackages={"data"},
excludeFilters={
    @Filter(type=FilterType.ANNOTATION, value = EnableWebMvc.class)
})
public class RootConfig {

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory emf, DataSource dataSource){
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);
    transactionManager.setDataSource(dataSource);
    return transactionManager;
}

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/HelloWorld");
    dataSource.setUsername("login");
    dataSource.setPassword("haslo");
    return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);
    em.setPackagesToScan("data");
    em.setJpaVendorAdapter(jpaVendorAdapter);
    em.setJpaProperties(additionalProperties());
    return em;
}

Properties additionalProperties() {
    Properties properties = new Properties();
    //properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    return properties;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter(){
    HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    //adapter.setDatabase();
    adapter.setShowSql(true);
    adapter.setGenerateDdl(false);
    adapter.setDatabasePlatform("org.hibernate.dialect.HSQLDialect");
    return adapter;
}

}

Repo在包"数据":

@Repository
@Transactional
public interface UserRepository extends JpaRepository<User, Long>{
    User findByNick(String nick);
}

控制器:

@Controller
public class UserController {

@Autowired
protected UserRepository userRepository;
...
}

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.app</groupId>
  <artifactId>BuyMyTime</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>BuyMyTime Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
        <spring.version>4.3.1.RELEASE</spring.version>
    </properties>
  <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.10.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.0.1.Final</version>
     </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>3.0.0.ALPHA03</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>BuyMyTime</finalName>
  </build>
</project>

请提供任何帮助。

3 个答案:

答案 0 :(得分:2)

您的项目结构是什么?我在basepackage.data中使用UserRepository复制了你的代码,并发现了存储库。

我认为这是因为你在默认包下有配置和数据子。我通过将数据,Web和配置包移动到一个通用包下来实现它。

以下是我的配置注释

package basepackage.config;

@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages={"basepackage.data"})
@EntityScan(basePackageClasses=User.class)
@ComponentScan(basePackages={"basepackage"})
public class MyConfig {

@Autowired
private UserRepository repo;

@PostConstruct
public void init(){
    for(int i=0; i<10; i++){
        repo.save(new User("u-"+i));
    }

}

}

您还可以使用basePackageClasses定义扫描。

@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackageClasses={UserRepository.class})
@EntityScan(basePackageClasses=User.class)
@ComponentScan(basePackages={"basepackage"})

希望有所帮助。

答案 1 :(得分:0)

我修复了它。 就我而言,问题出在实现Spring JPA Auditing的@Configuration类上。 我删除了不必要的@EnableTransactionManagement和@EnableJpaRepositories,并再次扫描了存储库。

答案 2 :(得分:0)

我也修复了,只是在我的主类中添加了以下注释。

@EnableMongoRepositories(basePackages = {"com.example.repository"})