Spring 存储库注入不起作用
Spring repository injection does not work
我正在用 Maven 编写一个测试 Spring 应用程序,该应用程序使用 Spring 数据。当我在 wildfly 18 上部署它时发生错误,因为依赖注入不起作用:
@Repository
public interface UserRepository extends CrudRepository<test.model.Utenti, Integer> {
}
@Autowired
private UserRepository userRepository;
错误是:
09:54:47,345 ERROR [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 92) Context initialization failed: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloWorldRestController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'test.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我正在使用 Sping 4.3。26.RELEASE、Spring boot 2.1。4.RELEASE、MySQL 连接器版本 8.0.18 和 Java 1.8。
如果您还没有将以下内容添加到主Class。
@ComponentScan("test")
更新
在 application.properties
文件中添加以下内容:
spring.datasource.url=<URL>
spring.datasource.username=<USERNAME>
spring.datasource.password=<Password>
spring.jpa.database=<Database for example POSTGRESQL>
spring.jpa.database-platform=<Dialect e.g. org.hibernate.dialect.PostgreSQLDialect>
我正在用 Maven 编写一个测试 Spring 应用程序,该应用程序使用 Spring 数据。当我在 wildfly 18 上部署它时发生错误,因为依赖注入不起作用:
@Repository
public interface UserRepository extends CrudRepository<test.model.Utenti, Integer> {
}
@Autowired
private UserRepository userRepository;
错误是:
09:54:47,345 ERROR [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 92) Context initialization failed: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloWorldRestController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'test.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我正在使用 Sping 4.3。26.RELEASE、Spring boot 2.1。4.RELEASE、MySQL 连接器版本 8.0.18 和 Java 1.8。
如果您还没有将以下内容添加到主Class。
@ComponentScan("test")
更新
在 application.properties
文件中添加以下内容:
spring.datasource.url=<URL>
spring.datasource.username=<USERNAME>
spring.datasource.password=<Password>
spring.jpa.database=<Database for example POSTGRESQL>
spring.jpa.database-platform=<Dialect e.g. org.hibernate.dialect.PostgreSQLDialect>