Java Spring自动连线服务和控制器不工作
我在这里读了很多关于这类问题的文章,但我的代码似乎不错,但autowire不起作用:Java Spring自动连线服务和控制器不工作,java,spring,maven,autowired,Java,Spring,Maven,Autowired,我在这里读了很多关于这类问题的文章,但我的代码似乎不错,但autowire不起作用: Error creating bean with name 'optionController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private se
Error creating bean with name 'optionController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private service.InteractionBanque controllers.OptionController.interactionBanque; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.InteractionBanque] 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)}
以下是我的控制器的代码:
package controllers;
package controllers;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import model.Banque;
import model.Client;
import service.InteractionBanque;
import serviceimpl.InteractionBanqueImpl;
@Controller
public class OptionController {
@Autowired
private InteractionBanque interactionBanque;
@RequestMapping(value="/virement",method=RequestMethod.GET)
public String index(Model model, @ModelAttribute Client client) {
model.addAttribute("virement", new Virement());
return "virement";
}
@RequestMapping(value="/virement",method=RequestMethod.POST)
public String index(@ModelAttribute Virement virement, Model model) {
return "options";
}
}
以及我的服务代码:
package serviceimpl;
import java.util.HashMap;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import dao.BanqueDAO;
import daoimpl.BanqueDaoImpl;
import model.Banque;
import model.Client;
import service.InteractionBanque;
import utils.SendRequest;
@Service
public class InteractionBanqueImpl implements InteractionBanque {
public static final int END_ID_BANQUE = 5;
public static final String LOGIN_URL = "/account";
public boolean connecter(Client client) {
some code
}
}
以及接口的代码:
package service;
public interface InteractionBanque {
boolean connecter(Client client);
}
我的应用程序类定义了@SpringBootApplication
,它应该用于连接所有内容:
package controllers;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
所以我不明白,对我来说这应该可以,但自动连线不起作用
非常感谢您的帮助:)
@SpringBootApplication
只扫描使用它的类中的包(递归)<代码>InteractionBanqueImpl在另一个包中
使用
应用程序
类创建包“app”,然后移动到它控制器
和其他包。应该没问题。正如@Mati所说,您的软件包有问题
为您的应用程序创建一个根包,并将其下的所有内容移动,这样您就有了如下内容:
+ myapp
Application.java
+ controller
+ service
+ serviceimpl
关于将
应用程序
类放入其余代码的父包中,您的答案是可行的,但如果您不想更改包结构,另一种选择是使用@ComponentScan
注释,指定包含要自动连线的组件的包,例如:
@ComponentScan(basePackages = {"serviceimpl", ...}
Thx的快速回复,但似乎我不能在eclipse中创建一个子包,每当我尝试将一个包移动到另一个包时,它只是在同一级别上复制。。。有办法吗?我读到Java不允许子包。我不使用eclipse,但您可以将包放入另一个包中。包是源代码中的文件夹,因此您可以使用文件资源管理器进入其中,并通过创建适当的文件夹结构手动执行。就像@predrag maric在其他答案中建议的那样。在eclipse中似乎不可能,我无法将包移动到其他包中:(有很多种方法可以完成,这里有一种方法:右键单击导航器->重构->移动中的类,然后键入目标包名称。我无法创建子包,所以这是一个很好的解决方案,Thx man!不过我更愿意以更好的方式组织我的包。)