我知道有类似的问题,但我尝试了多种解决方案,但没有一种能够解决问题。
我有一个DataFetcher类
package com.app.fetcher;
@Component
public class DataFetcher{
private ArrayList<Data> data = new ArrayList<>()
public DataFetcher() {
}
@PostConstruct
public void loadData(){
//load data from external API and store it to data collection
}
//getters and setters
}
和一个处理请求的简单控制器
package com.app.controller;
@Controller
public class DataController {
@Autowired
private DataFetcher dataFetcher;
@RequestMapping("/data")
public String showData(Model model){
model.addAttribute("data",dataFetcher.getData());
return "showData";
}
}
我的beans.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<context:component-scan
base-package="com.app">
</context:component-scan>
</beans>
我的课程位于src/main/java
,我的beans.xml位于src/main/resources
当我将此应用程序部署到tomcat8时,在部署期间(以及尝试访问该站点时)会抛出此异常
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.app.fetcher.DataFetcher] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:997)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:867)
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:490)
//another 20 entries from spring
知道怎么解决这个问题吗?我尝试用手连接豆子,但仍然得到同样的错误。
编辑:根据要求,我的web.xml(位置:位于src/main/webapp/WEB-INF/web.xml
)
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>showData</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>showData</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
和完整的DataFetcher类
package com.app.fetcher;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
@Component
public class DataFetcher {
private final static String SERVER_STATUS_QUERY = "/api/wow/realm/status";
private ArrayList<Data> data = new ArrayList<>();
public DataFetcher() {
}
@PostConstruct
private void init() {
URL url = null;
try {
url = new URL("http://eu.battle.net" + SERVER_STATUS_QUERY);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String input;
StringBuilder stringBuilder = new StringBuilder();
while ((input = in.readLine()) != null) {
stringBuilder.append(input);
}
in.close();
parseData(stringBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
private void parseData(String json) {
try {
JSONObject obj = new JSONObject(json);
JSONArray jsonArray = obj.getJSONArray("realms");
for (int i = 0; i < jsonArray.length(); i++) {
data.add(new Data(jsonArray.getJSONObject(i).getString("name"), jsonArray.getJSONObject(i).getString("type")));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public ArrayList<Data> getData() {
return data;
}
public void setData(ArrayList<Data> data) {
this.data = data;
}
}