2015-12-07 94 views
3

我看了很多关于这样的问题在这里,但似乎我的代码是好的,但自动装配不工作:弹簧自动装配Autowired服务和控制器不工作

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); 
    } 
} 

,所以我不明白,对我来说这应该做的工作,但自动装配Autowired不工作。

帮助,将不胜感激:)一个使用它的类内

回答

6

@SpringBootApplication仅扫描包(递归的)。 InteractionBanqueImpl在另一个包中。

Application类创建一个包'app',然后移动到controllers和其他包。应该没事。

+0

(对于OP)一些阅读:h ttps://spring.io/guides/gs/spring-boot/ –

+0

Thx为快速回复,但似乎我不能在eclipse中创建子包,只要我尝试将包移动到另一个包中只是在同一级别重复...有没有办法做到这一点? 我读过,Java不允许子包 – Pyr0technicien

+0

嗯,我不使用eclipse,但你可以把包放到另一个。包是源代码中的文件夹,因此您可以使用文件资源管理器访问文件夹,并通过创建适当的文件夹结构来手动执行。就像@ predrag-maric在其他答案中建议的一样。 – Mati

2

正如@Mati所说,你有一个软件包的问题。

为应用程序创建一个根包,并在其移动的一切,让你拥有它是这样的:

+ myapp 
    Application.java 
    + controller 
    + service 
    + serviceimpl 
+0

看来不可能通过日食,我不能移动一个包到另一个:( – Pyr0technicien

+0

它可以用很多方式完成,这里是其中之一:右键单击导航器中的一个类 - >重构 - >移动并键入目标包名称 –

1

的答案您有关于把你Application类的休息父包你代码的工作,但另一种,如果你不想改变你的封装结构,将使用@ComponentScan注解,指定包含要自动装配组件的软件包,例如:

@ComponentScan(basePackages = {"serviceimpl", ...} 
+0

我无法创建子包,因此这是一个很好的解决方案,Thx man! 但是,我更愿意以更好的方式组织我的包 – Pyr0technicien

相关问题