Spring 中的 Service 有多个实现类,怎么注入?
方法一:Controller中注入service的时候使用@Autowired自动注入,
@Qualifier("beanId")
来指定注入哪一个。方法二:Controller中注入service的时候使用
@Resource(type = 类名.class)
来指定注入哪一个。方法三:
每个service的impl都可以指定名称(使用 @Service(“名称”)
)Controller中注入service的时候使用名称来指定注入哪一个(使用 @Resource(name="名称")
)。
1@Service注解,其实做了两件事情:
1、声明TeacherServiceImpl.java是一个bean。因为TeacherServiceImpl .java是一个bean,其他的类才可以使用@Autowired将TeacherServiceImpl 作为一个成员变量自动注入。
2、TeacherServiceImpl.java在bean中的id是"teacherServiceImpl ",即类名且首字母小写。
注意:不能有同名的,不然要报错。
2@Autowired注解的意思就是:
当Spring发现
@Autowired
注解时,将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去。@Resource
的作用相当于@Autowired
。3@Autowired和@Resource两个注解的区别:
1.
@Autowired
是Spring的注解,@Resource
是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了。2.
@Autowired
默认按照byType方式进行bean匹配,@Resource
默认按照byName方式进行bean匹配。3.@Autowired默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:
@Autowired(required=false)
。方法一、二代码如下:
接口
publicinterfaceHumanService{
public String name();
}
接口实现类
@ServicepublicclassTeacherServiceImplimplementsHumanService{
@Override
public String name(){
System.out.println("teacher");
return"teacher";
}
}
@ServicepublicclassDoctorServiceImplimplementsHumanService{
@Override
public String name(){
System.out.println("doctor");
return"doctor";
}
}
控制器
@RestController
publicclassHumanController{
// @Resource(type = DoctorServiceImpl.class) //方法二
@Autowired
@Qualifier("teacherServiceImpl")
private HumanService humanService;
@RequestMapping("/name")
public String name(){
return humanService.name();
}
}
方法三代码如下:
接口
publicinterfaceHumanService{
public String name();
}
接口实现类
@Service("teacherService")
publicclassTeacherServiceImplimplementsHumanService{
@Override
public String name(){
System.out.println("teacher");
return"teacher";
}
}
@Service("doctorService")
publicclassDoctorServiceImplimplementsHumanService{
@Override
public String name(){
System.out.println("doctor");
return"doctor";
}
}
控制器
@RestController
publicclassHumanController{
@Resource(name="doctorService")
private HumanService humanService;
@RequestMapping("/name")
public String name(){
return humanService.name();
}
}
来源:cnblogs.com/zoe-java/p/11530888.html
构建高质量的技术交流社群,欢迎从事编程开发、技术招聘HR进群,也欢迎大家分享自己公司的内推信息,相互帮助,一起进步!
文明发言,以交流技术
、职位内推
、行业探讨
为主
广告人士勿入,切勿轻信私聊,防止被骗
关键词
注解
方法
接口
名称
System.out.println
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。