一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - 詳解Spring注解--@Autowired、@Resource和@Service

詳解Spring注解--@Autowired、@Resource和@Service

2020-09-22 10:24五月的倉(cāng)頡 JAVA教程

本篇文章主要介紹最重要的三個(gè)Spring注解,也就是@Autowired、@Resource和@Service,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧

什么是注解

傳統(tǒng)的Spring做法是使用.xml文件來(lái)對(duì)bean進(jìn)行注入或者是配置aop、事物,這么做有兩個(gè)缺點(diǎn):

1、如果所有的內(nèi)容都配置在.xml文件中,那么.xml文件將會(huì)十分龐大;如果按需求分開.xml文件,那么.xml文件又會(huì)非常多。總之這將導(dǎo)致配置文件的可讀性與可維護(hù)性變得很低

2、在開發(fā)中在.java文件和.xml文件之間不斷切換,是一件麻煩的事,同時(shí)這種思維上的不連貫也會(huì)降低開發(fā)的效率

為了解決這兩個(gè)問(wèn)題,Spring引入了注解,通過(guò)"@XXX"的方式,讓注解與Java Bean緊密結(jié)合,既大大減少了配置文件的體積,又增加了Java Bean的可讀性與內(nèi)聚性。

本篇文章,講講最重要的三個(gè)Spring注解,也就是@Autowired、@Resource和@Service,希望能通過(guò)有限的篇幅說(shuō)清楚這三個(gè)注解的用法。

不使用注解

先看一個(gè)不使用注解的Spring示例,在這個(gè)示例的基礎(chǔ)上,改成注解版本的,這樣也能看出使用與不使用注解之間的區(qū)別,先定義一個(gè)老虎:

?
1
2
3
4
5
6
7
8
9
public class Tiger
{
 private String tigerName = "TigerKing";
 
 public String toString()
 {
  return "TigerName:" + tigerName;
 }
}

再定義一個(gè)猴子:

?
1
2
3
4
5
6
7
8
9
public class Monkey
{
 private String monkeyName = "MonkeyKing";
 
 public String toString()
 {
  return "MonkeyName:" + monkeyName;
 }
}

定義一個(gè)動(dòng)物園:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Zoo
{
 private Tiger tiger;
 private Monkey monkey;
 
 public void setTiger(Tiger tiger)
 {
  this.tiger = tiger;
 }
 
 public void setMonkey(Monkey monkey)
 {
  this.monkey = monkey;
 }
 
 public Tiger getTiger()
 {
  return tiger;
 }
 
 public Monkey getMonkey()
 {
  return monkey;
 }
 public String toString()
 {
  return tiger + "\n" + monkey;
 }
}

spring的配置文件這么寫:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.2.xsd"
 default-autowire="byType">
 
 <bean id="zoo" class="com.xrq.bean.Zoo" >
  <property name="tiger" ref="tiger" />
  <property name="monkey" ref="monkey" />
 </bean>
 
 <bean id="tiger" class="com.xrq.domain.Tiger" />
 <bean id="monkey" class="com.xrq.domain.Monkey" />
</beans>

都很熟悉,權(quán)當(dāng)復(fù)習(xí)一遍了。

@Autowired

@Autowired顧名思義,就是自動(dòng)裝配,其作用是為了消除代碼Java代碼里面的getter/setter與bean屬性中的property。當(dāng)然,getter看個(gè)人需求,如果私有屬性需要對(duì)外提供的話,應(yīng)當(dāng)予以保留。

因此,引入@Autowired注解,先看一下spring配置文件怎么寫:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.2.xsd">
 
 <context:component-scan base-package="com.xrq" />
 
 <bean id="zoo" class="com.xrq.bean.Zoo" />
 <bean id="tiger" class="com.xrq.domain.Tiger" />
 <bean id="monkey" class="com.xrq.domain.Monkey" />
</beans>

注意第10行,使用必須告訴spring一下我要使用注解了,告訴的方式有很多,<context:component-scan base-package="xxx" />是一種最簡(jiǎn)單的,spring會(huì)自動(dòng)掃描xxx路徑下的注解。

看到第12行,原來(lái)zoo里面應(yīng)當(dāng)注入兩個(gè)屬性tiger、monkey,現(xiàn)在不需要注入了。再看下,Zoo.java也很方便,把getter/setter都可以去掉:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Zoo
{
 @Autowired
 private Tiger tiger;
 
 @Autowired
 private Monkey monkey;
 
 public String toString()
 {
  return tiger + "\n" + monkey;
 }
}

這里@Autowired注解的意思就是,當(dāng)Spring發(fā)現(xiàn)@Autowired注解時(shí),將自動(dòng)在代碼上下文中找到和其匹配(默認(rèn)是類型匹配)的Bean,并自動(dòng)注入到相應(yīng)的地方去。

有一個(gè)細(xì)節(jié)性的問(wèn)題是,假如bean里面有兩個(gè)property,Zoo.java里面又去掉了屬性的getter/setter并使用@Autowired注解標(biāo)注這兩個(gè)屬性那會(huì)怎么樣?答案是Spring會(huì)按照xml優(yōu)先的原則去Zoo.java中尋找這兩個(gè)屬性的getter/setter,導(dǎo)致的結(jié)果就是初始化bean報(bào)錯(cuò)。

OK,假設(shè)此時(shí)我把.xml文件的13行、14行兩行給去掉,再運(yùn)行,會(huì)拋出異常:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Zoo': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xrq.domain.Tiger com.xrq.bean.Zoo.ttiger; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xrq.domain.Tiger] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
 at com.xrq.test.MyTest.main(MyTest.java:13)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xrq.domain.Tiger com.xrq.bean.Zoo.ttiger; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xrq.domain.Tiger] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:571)
 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
 ... 13 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.xrq.domain.Tiger] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
 ... 15 more

因?yàn)?,@Autowired注解要去尋找的是一個(gè)Bean,Tiger和Monkey的Bean定義都給去掉了,自然就不是一個(gè)Bean了,Spring容器找不到也很好理解。那么,如果屬性找不到我不想讓Spring容器拋出異常,而就是顯示null,可以嗎?可以的,其實(shí)異常信息里面也給出了提示了,就是將@Autowired注解的required屬性設(shè)置為false即可:

?
1
2
3
4
5
6
7
8
9
10
11
12
public class Zoo
{
 @Autowired(required = false)
 private Tiger tiger;
 
 @Autowired(required = false)
 private Monkey monkey;
 public String toString()
 {
  return tiger + "\n" + monkey;
 }
}

此時(shí),找不到tiger、monkey兩個(gè)屬性,Spring容器不再拋出異而是認(rèn)為這兩個(gè)屬性為null。

@Autowired接口注入

上面的比較簡(jiǎn)單,我們只是簡(jiǎn)單注入一個(gè)Java類,那么如果有一個(gè)接口,有多個(gè)實(shí)現(xiàn),Bean里引用的是接口名,又該怎么做呢?比如有一個(gè)Car接口:

?
1
2
3
4
public interface Car
{
 public String carName();
}

兩個(gè)實(shí)現(xiàn)類BMW和Benz:

?
1
2
3
4
5
6
7
8
@Service
public class BMW implements Car
{
 public String carName()
 {
  return "BMW car";
 }
}
?
1
2
3
4
5
6
7
8
@Service
public class Benz implements Car
{
 public String carName()
 {
  return "Benz car";
 }
}

寫一個(gè)CarFactory,引用Car:

?
1
2
3
4
5
6
7
8
9
10
@Service
public class CarFactory
{
 @Autowired
 private Car car;
 public String toString()
 {
  return car.carName();
 }
}

不用說(shuō),一定是報(bào)錯(cuò)的,Car接口有兩個(gè)實(shí)現(xiàn)類,Spring并不知道應(yīng)當(dāng)引用哪個(gè)實(shí)現(xiàn)類。這種情況通常有兩個(gè)解決辦法:

1、刪除其中一個(gè)實(shí)現(xiàn)類,Spring會(huì)自動(dòng)去base-package下尋找Car接口的實(shí)現(xiàn)類,發(fā)現(xiàn)Car接口只有一個(gè)實(shí)現(xiàn)類,便會(huì)直接引用這個(gè)實(shí)現(xiàn)類

2、實(shí)現(xiàn)類就是有多個(gè)該怎么辦?此時(shí)可以使用@Qualifier注解:

?
1
2
3
4
5
6
7
8
9
10
11
12
@Service
public class CarFactory
{
 @Autowired
 @Qualifier("BMW")
 private Car car;
 
 public String toString()
 {
  return car.carName();
 }
}

注意@Qualifier注解括號(hào)里面的應(yīng)當(dāng)是Car接口實(shí)現(xiàn)類的類名,我之前試的時(shí)候一直以為是bean的名字,所以寫了"bMW",結(jié)果一直報(bào)錯(cuò)。

@Resource

把@Resource注解放在@Autowired下面說(shuō),是因?yàn)樗鼈冏饔梅浅O嗨疲@個(gè)就簡(jiǎn)單說(shuō)了,例子過(guò)后點(diǎn)明一下@Resource和@Autowired的區(qū)別。先看一下@Resource,直接寫Zoo.java了:

?
1
2
3
4
5
6
7
8
9
10
11
12
@Service
public class Zoo
{
 @Resource(name = "tiger")
 private Tiger tiger;
 @Resource(type = Monkey.class)
 private Monkey monkey;
 public String toString()
 {
  return tiger + "\n" + monkey;
 }
}

這是詳細(xì)一些的用法,說(shuō)一下@Resource的裝配順序:

1、@Resource后面沒(méi)有任何內(nèi)容,默認(rèn)通過(guò)name屬性去匹配bean,找不到再按type去匹配

2、指定了name或者type則根據(jù)指定的類型去匹配bean

3、指定了name和type則根據(jù)指定的name和type去匹配bean,任何一個(gè)不匹配都將報(bào)錯(cuò)

然后,區(qū)分一下@Autowired和@Resource兩個(gè)注解的區(qū)別:

1、@Autowired默認(rèn)按照byType方式進(jìn)行bean匹配,@Resource默認(rèn)按照byName方式進(jìn)行bean匹配

2、@Autowired是Spring的注解,@Resource是J2EE的注解,這個(gè)看一下導(dǎo)入注解的時(shí)候這兩個(gè)注解的包名就一清二楚了

Spring屬于第三方的,J2EE是Java自己的東西,因此,建議使用@Resource注解,以減少代碼和Spring之間的耦合。

@Service

上面這個(gè)例子,還可以繼續(xù)簡(jiǎn)化,因?yàn)閟pring的配置文件里面還有12行~14行三個(gè)bean,下一步的簡(jiǎn)化是把這三個(gè)bean也給去掉,使得spring配置文件里面只有一個(gè)自動(dòng)掃描的標(biāo)簽,增強(qiáng)Java代碼的內(nèi)聚性并進(jìn)一步減少配置文件。

要繼續(xù)簡(jiǎn)化,可以使用@Service。先看一下配置文件,當(dāng)然是全部刪除了:

?
1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.2.xsd">
 <context:component-scan base-package="com.xrq" />
</beans>

是不是感覺(jué)很爽?起碼我覺(jué)得是的。OK,下面以Zoo.java為例,其余的Monkey.java和Tiger.java都一樣:

?
1
2
3
4
5
6
7
8
9
10
11
12
@Service
public class Zoo
{
 @Autowired
 private Tiger ttiger;
 @Autowired
 private Monkey mmonkey;
 public String toString()
 {
  return ttiger + "\n" + mmonkey;
 }
}

這樣,Zoo.java在Spring容器中存在的形式就是"zoo",即可以通過(guò)ApplicationContext的getBean("zoo")方法來(lái)得到Zoo.java。@Service注解,其實(shí)做了兩件事情:

1、聲明Zoo.java是一個(gè)bean,這點(diǎn)很重要,因?yàn)閆oo.java是一個(gè)bean,其他的類才可以使用@Autowired將Zoo作為一個(gè)成員變量自動(dòng)注入

2、Zoo.java在bean中的id是"zoo",即類名且首字母小寫

如果,我不想用這種形式怎么辦,就想讓Zoo.java在Spring容器中的名字叫做"Zoo",可以的:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@Service
@Scope("prototype")
public class Zoo
{
 @Autowired
 private Monkey monkey;
 @Autowired
 private Tiger tiger;
 public String toString()
 {
  return "MonkeyName:" + monkey + "\nTigerName:" + tiger;
 }
}

這樣,就可以通過(guò)ApplicationContext的getBean("zoo")方法來(lái)得到Zoo.java了。

這里我還多加了一個(gè)@Scope注解,應(yīng)該很好理解。因?yàn)镾pring默認(rèn)產(chǎn)生的bean是單例的,假如我不想使用單例怎么辦,xml文件里面可以在bean里面配置scope屬性。注解也是一樣,配置@Scope即可,默認(rèn)是"singleton"即單例,"prototype"表示原型即每次都會(huì)new一個(gè)新的出來(lái)。

補(bǔ)充細(xì)節(jié)

最后再補(bǔ)充一個(gè)我發(fā)現(xiàn)的細(xì)節(jié)。假如animal包下有Tiger、domain包下也有Tiger,它們二者都加了@Service注解,那么在Zoo.java中即使明確表示我要引用的是domain包下的Tiger,程序運(yùn)行的時(shí)候依然會(huì)報(bào)錯(cuò)。

細(xì)想,其實(shí)這很好理解,兩個(gè)Tiger都使用@Service注解標(biāo)注,意味著兩個(gè)Bean的名字都是"tiger",那么我在Zoo.java中自動(dòng)裝配的是哪個(gè)Tiger呢?不明確,因此,Spring容器會(huì)拋出BeanDefinitionStoreException異常,Caused by:

?
1
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'monkey' for bean class [com.xrq.domain.Monkey] conflicts with existing, non-compatible bean definition of same name and class [com.xrq.animal.Monkey]

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持服務(wù)器之家!

原文鏈接:http://www.cnblogs.com/xrq730/p/5313412.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩亚洲一区二区三区在线观看 | 扒开腿开嫩苞 | 3d动漫免费 | 99视频久久 | 国产专区亚洲欧美另类在线 | 亚洲成人第一 | 校花小雪灌满了男人们的浓浆 | 国产精品视频久久久久 | 草莓茄子丝瓜番茄小蝌蚪 | 风间由美理论片在线观看 | 国产午夜一区二区在线观看 | 欧美日韩亚洲综合在线一区二区 | 国内精品露脸在线视频播放 | 日韩性生活片 | 第一次做m被调教经历 | 四虎永久在线精品免费影视 | 欧美极品brazzers 高清 | 好湿好紧太硬了我太爽了网站 | 夫妻性生活一级黄色片 | 成人网免费视频 | 无限好资源第一片免费韩国 | 黑人粗又长| 黄漫免费观看 | 无人知晓小说姜璟免费阅读 | 农村老妇1乱69系列小说 | 精品一区二区国语对白 | 国产成人www | 色老板影视 | 男人天堂官方网站 | 国内自拍成人网在线视频 | 精品国产美女AV久久久久 | 免费国产高清精品一区在线 | 久久精品嫩草影院免费看 | 成年男女免费视频观看性 | 亚洲欧洲日产国码天堂 | 精品国产麻豆免费人成网站 | 青青久久久国产线免观 | 国产成人夜色91 | 日韩在线 中文字幕 | sihu国产午夜精品一区二区三区 | 亚洲嫩模吧粉嫩粉嫩冒白浆 |