Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带Spring注释的控制器是否需要默认构造函数_Spring_Annotations - Fatal编程技术网

带Spring注释的控制器是否需要默认构造函数

带Spring注释的控制器是否需要默认构造函数,spring,annotations,Spring,Annotations,我有一个使用注释的spring控制器。我给这个控制器一个构造函数,它接受两个参数。我想要两种初始化控制器的方法:构造函数注入和setter注入 @Controller("viewQuestionController") @RequestMapping("/public/viewQuestions") public class ViewQuestionController { @Resource(name="questionService") private QuestionSer

我有一个使用注释的spring控制器。我给这个控制器一个构造函数,它接受两个参数。我想要两种初始化控制器的方法:构造函数注入和setter注入

@Controller("viewQuestionController")
@RequestMapping("/public/viewQuestions")
public class ViewQuestionController
{
    @Resource(name="questionService")
    private QuestionService questionService;

   /*public ViewQuestionController()
 {
    int i=0;
    i++;
 } 
   */   

public ViewQuestionController(@Qualifier("questionService") QuestionService questionService)
{
    this.questionService = questionService;
}

@Resource(name="questionService")
public void setQuestionService(QuestionService questionService)
{
    this.questionService = questionService;
}
}   
当我取消注释默认构造函数时,控制器被正确启动。然而,如果我没有,我会得到一个BeanInstationException,没有找到默认构造函数;嵌套异常是java.lang.NoSuchMethodException。
那么,我对带注释的构造函数的配置是错误的,还是spring中完全带注释的控制器总是需要默认构造函数?

如果要通过注释配置构造函数注入,需要在构造函数上放置相应的注释。我不确定如何使用
@Resource
,但
@Autowired
@Inject
支持它:

@Autowired
public ViewQuestionController(@Qualifier("questionService") QuestionService questionService) 


我认为控制器bean需要一个默认构造函数,因为它们是由框架初始化的,但是没有办法告诉框架hot提供依赖关系

再想一想,为什么不自动连接您的问题服务,Spring会处理它。 下面的代码应该很好

 @Controller("viewQuestionController")
 @RequestMapping("/public/viewQuestions")
 public class ViewQuestionController
 {
       @Autowired
       private QuestionService questionService;

       //Not providing any constructor would also be fine
       public ViewQuestionController(){}

questionService将由Spring正确初始化

我试图避免简单的自动连接,我希望在注入依赖项时提到它的名称。我听说,当一个项目的规模越来越大时,不建议使用简单的自动布线。我曾经使用简单的自动布线处理过相当大的项目,效果很好。事实上,JavaEE标准也提倡同样的理念,比如@Resource注释和采用约定而不是配置。从纯XML的角度来看,自动连线过去在理解项目方面很难管理,但由于注释和类级别的配置,自动连线是一个更好的选择。谢谢,使用@Autowired的第一个选项成功了。我没有尝试@Inject,因为这需要在应用程序中放置另一个jar。我想稍后我将研究setter注入@Resource。我想在这个问题中,对于用@Controller注释的类,用@Autowired注释构造函数是没有必要的,尽管这不会有什么坏处?如果我是对的,构造函数上的自动连接将从@Controller自动发生。@cellepo您是对的。。。自Spring4.3以来,当bean只有一个构造函数时,自动连线注释是可选的。
 @Controller("viewQuestionController")
 @RequestMapping("/public/viewQuestions")
 public class ViewQuestionController
 {
       @Autowired
       private QuestionService questionService;

       //Not providing any constructor would also be fine
       public ViewQuestionController(){}