Spring First Layer和Second layer autowiring

时间:2014-07-03 07:33:12

标签: spring

我有一个场景,其中我的ServiceImpl和Business Class实现了相同的接口。但是我无法对它们两者进行自动装配。

   @RestController
   @RequestMapping("/myservice")
   public interface myInterface{

     @RequestMapping(value="/getSomething/{input}", method=RequestMethod.GET)
       doSomething(String input);

   }

现在我有两个实现相同接口的类

@Component
@Qualifier("doSomethingImpl")
public class DoSomethingImpl implements myInterface{

    @Autowired
    @Qualifier("businessLayer")
    myInterface businessLayer;

   doSomething(@PathVariable String input){
   //my logic here
   }

}

@Component
@Qualifier("businessLayer")
public class BusinessLayer implements myInterface{


   doSomething(@PathVariable String input){
   //my logic here
   }

}

现在,当我在服务器上运行时,我遇到了以下错误

  

无法将处理程序“DoSomethingImpl”映射到URL路径   [/ myservice / getSomething / {input}]:已经存在类型的处理程序   [class com.mypackage.business.BusinessLayer]已映射。

有人可以帮我解决此错误

1 个答案:

答案 0 :(得分:1)

问题是两个控制器都映射到同一路径。我建议你将代码更改为:

   public interface myInterface{

       @RequestMapping(value="/getSomething/{input}", method=RequestMethod.GET)
       public Whatever doSomething(String input) {
          //whatever
       }

   }

@RequestMapping("something")
@RestController
public class DoSomethingImpl implements myInterface{

}

@RequestMapping("somethingElse")
@RestController
public class BusinessLayer implements myInterface{

}