如下我定义一个接口:
public interface interfacetest {
public String testinterface(String name);
}
再定义一个接口实现类:
@Component("interfacetest")
public class testinterfaceac implements interfacetest {
@Override
public String testinterface(String name) {
// TODO Auto-generated method stub
return "interfacetest";
}
}
然后我在Controller控制层,使用注解的方式引入
@Autowired
private interfacetest intest;
@RequestMapping("/jsontest.action")
public @ResponseBody String jsontest( String id){
System.out.println(intest.testinterface(id));
return "asdaghosaghaohig ashgfooio";
}
实际上在引入的时候还需要添加一个字段@Qualifier("classname"),因为我定义的接口只有一个类实现了,所以可以不加这个,
但是当多个类实现了这个接口的时候就需要加了,为了避免错误。