鍍金池/ 問(wèn)答/Java/ @Service是標(biāo)記在接口上還是實(shí)現(xiàn)類上?

@Service是標(biāo)記在接口上還是實(shí)現(xiàn)類上?

@Service是標(biāo)記在接口上還是實(shí)現(xiàn)類上?

回答
編輯回答
萌面人

@Service注解、@Controller注解以及@Repository注解都是Spring中的注解,加上這些注解的目的是可以區(qū)分JavaEE三層架構(gòu)中的三個(gè)不同層次,其目的都是控制反轉(zhuǎn),將Java對(duì)象交給Spring容器創(chuàng)建。@Service注解是標(biāo)注在實(shí)現(xiàn)類上的。請(qǐng)看下面的代碼:

@Service
public class ProductInfoServiceImpl implements ProductInfoService {
    // 代碼實(shí)現(xiàn)
}
2018年3月17日 00:23
編輯回答
胭脂淚

@Service注解是標(biāo)注在實(shí)現(xiàn)類上的,因?yàn)?code>@Service是把spring容器中的bean進(jìn)行實(shí)例化,也就是等同于new操作,只有實(shí)現(xiàn)類是可以進(jìn)行new實(shí)例化的,而接口則不能,所以是加在實(shí)現(xiàn)類上的。

2017年2月25日 07:00
編輯回答
使勁操

標(biāo)記類上 目的是new對(duì)象

2017年6月7日 23:11
編輯回答
影魅
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    String value() default "";
}

以上是@Service注解的代碼。它實(shí)際上起作用的就是@Component,也就是說(shuō)只是個(gè)普通的bean。所以肯定是標(biāo)注具體類的??!

2017年2月18日 19:02