@Autowired、@Resource和@Inject注解的區(qū)別(最詳細(xì))
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
76套java從入門(mén)到精通實(shí)戰(zhàn)課程分享
@Target({ElementType.CONSTRUCTOR,?ElementType.METHOD,?
ElementType.PARAMETER,?ElementType.FIELD,?ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public?@interface?Autowired?{
???boolean?required()?default?true;
}
{@link?org.springframework.beans.factory.config.BeanPostProcessor?BeanPostProcessor}
*?implementation?that?autowires?annotated?fields,?setter?methods,?and?arbitrary
*?config?methods.?Such?members?to?be?injected?are?detected?through?annotations:
*?by?default,?Spring's?{@link?Autowired?@Autowired}?and?{@link?Value?@Value}
*?annotations.
*
*?Also?supports?JSR-330'
s?{@link?javax.inject.Inject?@Inject}?annotation,
*?if?available,?as?a?direct?alternative?to?Spring's?own?{@code?@Autowired}.@Target({TYPE,?FIELD,?METHOD})
@Retention(RUNTIME)
public?@interface?Resource?{
????String?name()?default?"";
?
????String?lookup()?default?"";
?
????Class>?type()?default?java.lang.Object.class;
?
????enum?AuthenticationType?{
????????????CONTAINER,
????????????APPLICATION
????}
?
????AuthenticationType?authenticationType()?default?AuthenticationType.CONTAINER;
?
????boolean?shareable()?default?true;
?
????String?mappedName()?default?"";
?
????String?description()?default?"";
}
*?{@link?org.springframework.beans.factory.config.BeanPostProcessor}?implementation
*?that?supports?common?Java?annotations?out?of?the?box,?in?particular?the?JSR-250
*?annotations?in?the?{@code?javax.annotation}?package.?These?common?Java
*?annotations?are?supported?in?many?Java?EE?5?technologies?(e.g.?JSF?1.2),
*?as?well?as?in?Java?6's?JAX-WS.
*
*?This?post-processor?includes?support?for?the?{@link?javax.annotation.PostConstruct}
*?and?{@link?javax.annotation.PreDestroy}?annotations?-?as?init?annotation
*?and?destroy?annotation,?respectively?-?through?inheriting?from
*?{@link?InitDestroyAnnotationBeanPostProcessor}?with?pre-configured?annotation?types.
*
*?
The?central?element?is?the?{@link?javax.annotation.Resource}?annotation
*?for?annotation-driven?injection?of?named?beans,?by?default?from?the?containing
*?Spring?BeanFactory,?with?only?{@code?mappedName}?references?resolved?in?JNDI.
*?The?{@link?#setAlwaysUseJndiLookup?"alwaysUseJndiLookup"?flag}?enforces?JNDI?lookups
*?equivalent?to?standard?Java?EE?5?resource?injection?for?{@code?name}?references
*?and?default?names?as?well.?The?target?beans?can?be?simple?POJOs,?with?no?special
*?requirements?other?than?the?type?having?to?match.
@Component
public?class?Student?{
????private?String?num?=?"1";
????public?String?getNum()?{
????????return?num;
????}
????public?void?setNum(String?num)?{
????????this.num?=?num;
????}
}
@Configuration
@ComponentScan(basePackages?=?{"it.cast.autowired"})
public?class?Config?{
?
????@Bean
????public?Student?student1(){
????????Student?student?=?new?Student();
????????student.setNum("2");
????????return?student;
????}
}
@Component
public?class?Teacher?{
?
????@Resource???//注意這里使用的@Resource注解,Spring支持注入Map、Conllent類(lèi)型的屬性變量
????private?Map?student;
????public?Map?getStudent()?{
????????return?student;
????}
?
????public?void?setStudent(Map?student)?{
????????this.student?=?student;
????}
?
}
public?class?Test01?{
????public?static?void?main(String[]?args)?{
????????AnnotationConfigApplicationContext?context?=?new?AnnotationConfigApplicationContext(Config.class);
????????Teacher?teacher?=?context.getBean(Teacher.class);
????????System.out.println(teacher.getStudent());
????}
}
?
//打印結(jié)果:
//Caused?by:?org.springframework.beans.factory.BeanNotOfRequiredTypeException:?Bean?named?'student'?is?expected?to?be?of?type?'java.util.Map'?but?was?actually?of?type?'it.cast.autowired.Student'
//?at?org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:392)
//?at?org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
//?at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:452)
//?at?org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:527)
//?at?org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:497)
//?at?org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:637)
//?at?org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180)
//?at?org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
//?at?org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:322)
//?...?12?more
?
@Component
public?class?Student?{
????private?String?num?=?"1";
????public?String?getNum()?{
????????return?num;
????}
????public?void?setNum(String?num)?{
????????this.num?=?num;
????}
}
@Configuration
@ComponentScan(basePackages?=?{"it.cast.autowired"})
public?class?Config?{
?
????@Bean
????public?Student?student1(){
????????Student?student?=?new?Student();
????????student.setNum("2");
????????return?student;
????}
}
@Component
public?class?Teacher?{
?
????@Autowired??//注意這里使用的@Autowired注解,Spring支持注入Map、Conllent類(lèi)型的屬性變量
????private?Map?student;
????public?Map?getStudent()?{
????????return?student;
????}
?
????public?void?setStudent(Map?student)?{
????????this.student?=?student;
????}
?
}
public?class?Test01?{
????public?static?void?main(String[]?args)?{
????????AnnotationConfigApplicationContext?context?=?new?AnnotationConfigApplicationContext(Config.class);
????????Teacher?teacher?=?context.getBean(Teacher.class);
????????System.out.println(teacher.getStudent());
????}
}
//其打印結(jié)果:
//{student=it.cast.autowired.Student@61009542,?student1=it.cast.autowired.Student@77e9807f}
@Configuration
@ComponentScan({"it.cast.resouce"})
public?class?ResourceConfig?{
?
????@Primary???//標(biāo)有Primary注解,使用@Autowired@Inject注解注解時(shí),優(yōu)先被加載
????@Bean
????public?Y?y1(){
????????Y?y?=?new?Y();
????????y.setI(0);
????????return?y;
????}
?
}
?
@Component
public?class?X?{
????@Resource???//這里使用的是@Resource注解,該注解默認(rèn)按照組件名稱(chēng)進(jìn)行裝配的,所以會(huì)優(yōu)先加載id為y的bean
????private?Y?y;
?
????public?Y?getY()?{
????????return?y;
????}
?
????public?void?setY(Y?y)?{
????????this.y?=?y;
????}
}
?
@Component
public?class?Y?{
????private?int?i?=?2;
?
????public?int?getI()?{
????????return?i;
????}
?
????public?void?setI(int?i)?{
????????this.i?=?i;
????}
}
????@Test
????public?void?ResourceConfigTest(){
????????AnnotationConfigApplicationContext?context?=
????????????????new?AnnotationConfigApplicationContext(ResourceConfig.class);
?
????????X?bean?=?context.getBean(X.class);
????????System.out.println(bean.getY().getI());
????}
????//輸出結(jié)果為:
????//?????2
????//從而驗(yàn)證了@Resource默認(rèn)按照名稱(chēng)進(jìn)行加載
@Configuration
@ComponentScan({"it.cast.resouce"})
public?class?ResourceConfig?{
?
????@Primary???//標(biāo)有Primary注解,使用@Autowired@Inject注解注解時(shí),優(yōu)先被加載
????@Bean
????public?Y?y1(){
????????Y?y?=?new?Y();
????????y.setI(0);
????????return?y;
????}
?
}
?
@Component
public?class?X?{
????@Resource???????//這里使用的是@Resource注解,該注解默認(rèn)按照組件名稱(chēng)進(jìn)行裝配的,所以會(huì)優(yōu)先加載id為y12的bean,
????private?Y?y12;??//如果找不到則按Primary注解標(biāo)注的bean進(jìn)行注入
?
????public?Y?getY()?{
????????return?y12;
????}
?
????public?void?setY(Y?y)?{
????????this.y12?=?y;
????}
}
?
@Component
public?class?Y?{
????private?int?i?=?2;
?
????public?int?getI()?{
????????return?i;
????}
?
????public?void?setI(int?i)?{
????????this.i?=?i;
????}
}
????@Test
????public?void?ResourceConfigTest(){
????????AnnotationConfigApplicationContext?context?=
????????????????new?AnnotationConfigApplicationContext(ResourceConfig.class);
?
????????X?bean?=?context.getBean(X.class);
????????System.out.println(bean.getY().getI());
????}
????//輸出結(jié)果為:
????//?????0
????//由于沒(méi)有找到id為y12的bean,所以注入了使用@Primary標(biāo)注的bean,
????//而且整個(gè)程序沒(méi)有報(bào)錯(cuò),所以驗(yàn)證了@Resource支持@Primary注解
@Configuration
@ComponentScan({"it.cast.resouce"})
public?class?ResourceConfig?{
?
????@Primary???//標(biāo)有Primary注解,使用@Autowired@Inject注解注解時(shí),優(yōu)先被加載
????@Bean
????public?Y?y1(){
????????Y?y?=?new?Y();
????????y.setI(0);
????????return?y;
????}
?
}
?
@Component
public?class?X?{
????@Autowired???
????private?Y?y;??//此時(shí)不管名稱(chēng)是y還是y12,都會(huì)使用標(biāo)有Primary注解的bean
?
????public?Y?getY()?{
????????return?y;
????}
?
????public?void?setY(Y?y)?{
????????this.y?=?y;
????}
}
?
@Component
public?class?Y?{
????private?int?i?=?2;
?
????public?int?getI()?{
????????return?i;
????}
?
????public?void?setI(int?i)?{
????????this.i?=?i;
????}
}
????@Test
????public?void?ResourceConfigTest(){
????????AnnotationConfigApplicationContext?context?=
????????????????new?AnnotationConfigApplicationContext(ResourceConfig.class);
?
????????X?bean?=?context.getBean(X.class);
????????System.out.println(bean.getY().getI());
????}
????//輸出結(jié)果為:
????//?????0
????//從而驗(yàn)證了@Autowired支持@Primary注解
@Target({?METHOD,?CONSTRUCTOR,?FIELD?})
@Retention(RUNTIME)
@Documented
public?@interface?Inject?{}??//該注解中沒(méi)有任何屬性
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。
本文鏈接:
https://blog.csdn.net/qq_35634181/article/details/104276056
粉絲福利:Java從入門(mén)到入土學(xué)習(xí)路線(xiàn)圖
???

?長(zhǎng)按上方微信二維碼?2 秒
感謝點(diǎn)贊支持下哈?
