从IntellijIDEA运行Spring应用程序时出现以下错误。
2017-04-20 18:20:50.645 ERROR 28680 --- [main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field checker in org.test.api.controller.CheckerController required a bean of type 'org.test.api.checker.Checker' that could not be found.
Action:
Consider defining a bean of type 'org.test.api.checker.Checker' in your configuration.
但是,我在路径
Checker
中有类org.test.api.checker.Checker
。这是
CheckerController
类的示例代码:@RestController
@RequestMapping("/api/{clientId}")
public class CheckerController {
@Autowired
Validator validator;
@Autowired
@Qualifier("Checker")
Checker checker;
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Context.class, new ContextEditor());
}
@RequestMapping(value = "/{userId}/{sessionId}/check", method = RequestMethod.GET)
public CheckResults check(@PathVariable String clientId,
@PathVariable String userId,
@PathVariable String sessionId) {
CheckRequest checkRequest = new CheckRequest(CheckRequest.TYPE_1, clientId, userId, sessionId);
this.validateRequest(checkRequest);
return checker.check(checkRequest);
}
}
类
Checker
:public interface Checker {
CheckResults check(CheckRequest request);
}
如何解决这个问题?
更新:
@Service
public class CheckerImpl implements Checker {
@Override
public CheckResults check(CheckRequest request)
{
// ...
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test</groupId>
<artifactId>mytestapi</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spark.version>1.6.1</spark.version>
<es.version>2.2.0</es.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.BUILD-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.spullara.mustache.java</groupId>
<artifactId>compiler</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${es.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-tdb</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-scala_2.10</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>6.5.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
最佳答案
当您通过@Qualifier("Checker")
自动装配时,请在CheckerImpl类的@Service
中定义bean名称。
@Service("Checker")
public class CheckerImpl implements Checker {
@Override
public CheckResults check(CheckRequest request)
{
// ...
}
}