gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:22:06 25 4
gpt4 key购买 nike

我似乎禁用了 jpa 存储库。出现此错误:

SEVERE: StandardWrapper.Throwable org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in file [C:\Users\jasiu\workspace2.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\BuyMyTime\WEB-INF\classes\web\UserController.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [data.UserRepository] found for dependency [data.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [data.UserRepository] found for dependency [data.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at 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 位于“data”包中:

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

Controller :

@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>

请寻求帮助。

最佳答案

您的项目结构是什么?我将您的代码复制到了一个包含 UserRepository 的项目中,位于 basepackage.data 中,并且发现了存储库。

我认为这是因为你在默认包下有配置和数据子项。我通过将数据、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"})

希望有帮助。

关于java - Spring JPA 对存储库的数据扫描不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38957503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com