Spring 自动连接的服务和控制器不工作
Spring Autowired service and controller not working
我在这里读了很多关于此类问题的文章,但我的代码似乎不错,但自动装配不起作用:
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);
}
我的应用程序 class 定义了 @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
仅扫描 class 中使用它的包(递归)。 InteractionBanqueImpl
在另一个包中。
用 Application
class 创建一个包 'app',然后移动到它 controllers
和其他包。应该没问题。
正如@Mati 所说,你的包有问题。
为您的应用程序创建一个根包并将所有内容移动到它下面,这样您就拥有了这样的东西:
+ myapp
Application.java
+ controller
+ service
+ serviceimpl
关于将 Application
class 放在其余代码的父包中的答案是可行的,但如果您不想更改包结构,则有另一种选择, 将使用 @ComponentScan
注释,指定包含要自动装配的组件的包,例如:
@ComponentScan(basePackages = {"serviceimpl", ...}
我在这里读了很多关于此类问题的文章,但我的代码似乎不错,但自动装配不起作用:
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);
}
我的应用程序 class 定义了 @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
仅扫描 class 中使用它的包(递归)。 InteractionBanqueImpl
在另一个包中。
用 Application
class 创建一个包 'app',然后移动到它 controllers
和其他包。应该没问题。
正如@Mati 所说,你的包有问题。
为您的应用程序创建一个根包并将所有内容移动到它下面,这样您就拥有了这样的东西:
+ myapp
Application.java
+ controller
+ service
+ serviceimpl
关于将 Application
class 放在其余代码的父包中的答案是可行的,但如果您不想更改包结构,则有另一种选择, 将使用 @ComponentScan
注释,指定包含要自动装配的组件的包,例如:
@ComponentScan(basePackages = {"serviceimpl", ...}