日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

annotation-config vs component-scan – Spring Core--转

發(fā)布時(shí)間:2025/4/5 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 annotation-config vs component-scan – Spring Core--转 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文地址:http://techidiocy.com/annotation-config-vs-component-scan-spring-core/

<context:annotation-config> and <context:component-scan> are two of the most basic concepts available in the Spring Core introduced in Spring 3 that every Spring user should understand.It is important that to understand the usage of each of them and how they are different to each other.

annotation-config :??Annotation config main job is to activate all the annotations that are present in java beans and those are?already registered?either by defining in your application context file or being registered while component scanning. Important point is they need to be registered.

component-scan :?Component scan can do everything that annotation config does , in addition to it it also registers the java classes as spring bean those are annotated with @Component , @Service ,@Repository etc.

Let’s see an example that can clear the difference in annotation-config vs component-scan in much more simpler way –
I have three java classes that I have declared in my application context.
SuperUser.java:

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 package com.lotusmedia.typesuper; import com.lotusmedia.typenormal.NormalUser; import com.lotusmedia.typenormal.TempUser; public class SuperUser { ????private NormalUser normalUser; ????private TempUser tempUser; ????public SuperUser(){ ????????System.out.println("Super User Created ->"+this); ????} ????public void setNormalUser(NormalUser normalUser) { ????????System.out.println("Setting Normal User ->"+normalUser); ????????this.normalUser = normalUser; ????} ????public void setTempUser(TempUser tempUser) { ????????System.out.println("Setting Temp User ->"+tempUser); ????????this.tempUser = tempUser; ????} }

NormalUser.java :

1 2 3 4 5 6 7 8 package com.lotusmedia.typenormal; public class NormalUser { ????public NormalUser(){ ????????System.out.println("Normal User Created->"+this); ????} }

TempUser.java :

1 2 3 4 5 6 7 8 package com.lotusmedia.typenormal; public class TempUser { ????public TempUser(){ ????????System.out.println("Temporary User Created->"+this); ????} }

Now here is my application context file where I am injecting normalUser and TempUser into SuperUser.
applicationContext.xml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <beans xmlns="http://www.springframework.org/schema/beans" ????xmlns:context="http://www.springframework.org/schema/context" ????xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ????xsi:schemaLocation=" ????????http://www.springframework.org/schema/beans???? ????????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ????????http://www.springframework.org/schema/context ????????http://www.springframework.org/schema/context/spring-context-3.0.xsd ???????"> ?<bean id="normalUser" class="com.lotusmedia.typenormal.NormalUser"></bean> ?<bean id="tempUser" class="com.lotusmedia.typenormal.TempUser"></bean> ?<bean id="superUser" class="com.lotusmedia.typesuper.SuperUser"> ????<property name="normalUser" ref="normalUser"></property> ????<property name="tempUser" ref="tempUser"></property> ?</bean> </beans>

And here is the executor class.
Executor.java :

1 2 3 4 5 6 7 8 9 10 11 package com.lotusmedia.run; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Executor { ????public static void main(String args[]){ ????????ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ????} }

Now when I executed this program I got output like this in my console and it is what we were expecting.

1 2 3 4 5 Normal User Created->com.lotusmedia.typenormal.NormalUser@75e845c2 Temporary User Created->com.lotusmedia.typenormal.TempUser@1cec6b00 Super User Created ->com.lotusmedia.typesuper.SuperUser@6564dbd5 Setting Normal User ->com.lotusmedia.typenormal.NormalUser@75e845c2 Setting Temp User ->com.lotusmedia.typenormal.TempUser@1cec6b00

Till now everything is good and as expected. Now lets do some modifications and introduce annotations in our beans. So , I have modified?SuperUser.java?to use annotations for wiring the properties.(notice the autowired annotation).

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 package com.lotusmedia.typesuper; import org.springframework.beans.factory.annotation.Autowired; import com.lotusmedia.typenormal.NormalUser; import com.lotusmedia.typenormal.TempUser; public class SuperUser { ????private NormalUser normalUser; ????private TempUser tempUser; ????public SuperUser(){ ????????System.out.println("Super User Created ->"+this); ????} ????????@Autowired ????public void setNormalUser(NormalUser normalUser) { ????????System.out.println("Setting Normal User ->"+normalUser); ????????this.normalUser = normalUser; ????} ????????@Autowired ????public void setTempUser(TempUser tempUser) { ????????System.out.println("Setting Temp User ->"+tempUser); ????????this.tempUser = tempUser; ????} }

And from configuration file I have removed the properties injection, so now my context file looks like this.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <beans xmlns="http://www.springframework.org/schema/beans" ????xmlns:context="http://www.springframework.org/schema/context" ????xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ????xsi:schemaLocation=" ????????http://www.springframework.org/schema/beans???? ????????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ????????http://www.springframework.org/schema/context ????????http://www.springframework.org/schema/context/spring-context-3.0.xsd ???????"> ?<bean id="normalUser" class="com.lotusmedia.typenormal.NormalUser"></bean> ?<bean id="tempUser" class="com.lotusmedia.typenormal.TempUser"></bean> ?<bean id="superUser" class="com.lotusmedia.typesuper.SuperUser"></bean> </beans>

Now I have executed my Executor class again and here is the output.

1 2 3 Normal User Created->com.lotusmedia.typenormal.NormalUser@4cc39a20 Temporary User Created->com.lotusmedia.typenormal.TempUser@485fcf29 Super User Created ->com.lotusmedia.typesuper.SuperUser@a19b1de

This time you will see that properties injection didn’t take place only the new beans were created, now you might be wondering why it didn’t happen as we had marked those properties with the Autowired annotation. So, here is answer , in spring by default annotations don’t do anything by themselves , if you want to use them you have to enable them in your application.
Now enabling annotations in spring is very simple and you have to add only one line of code in your context file to enable them and here is the magic line.

1 <context:annotation-config/>

Updated?applicationContext?file :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <beans xmlns="http://www.springframework.org/schema/beans" ????xmlns:context="http://www.springframework.org/schema/context" ????xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ????xsi:schemaLocation=" ????????http://www.springframework.org/schema/beans???? ????????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ????????http://www.springframework.org/schema/context ????????http://www.springframework.org/schema/context/spring-context-3.0.xsd ???????"> ?<context:annotation-config/> ?<bean id="normalUser" class="com.lotusmedia.typenormal.NormalUser"></bean> ?<bean id="tempUser" class="com.lotusmedia.typenormal.TempUser"></bean> ?<bean id="superUser" class="com.lotusmedia.typesuper.SuperUser"></bean> </beans>

Now this time when I executed the Executor class again , below is the output that I got and this is what we were expecting.

1 2 3 4 5 Normal User Created->com.lotusmedia.typenormal.NormalUser@679bfb30 Temporary User Created->com.lotusmedia.typenormal.TempUser@7977b9b Super User Created ->com.lotusmedia.typesuper.SuperUser@37fd6bea Setting Normal User ->com.lotusmedia.typenormal.NormalUser@679bfb30 Setting Temp User ->com.lotusmedia.typenormal.TempUser@7977b9b

So,till now all good, let’s play more now I am going to remove the bean declarations from the Xml file and use @Component annotation to register it as a bean.Here are the updated files.
applicationContext.xml :

1 2 3 4 5 6 7 8 9 10 11 12 <beans xmlns="http://www.springframework.org/schema/beans" ????xmlns:context="http://www.springframework.org/schema/context" ????xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ????xsi:schemaLocation=" ????????http://www.springframework.org/schema/beans???? ????????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ????????http://www.springframework.org/schema/context ????????http://www.springframework.org/schema/context/spring-context-3.0.xsd ???????"> ?<context:annotation-config/> ?</beans>

SuperUser.java :?(notice the @Component annotation)

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 30 31 package com.lotusmedia.typesuper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.lotusmedia.typenormal.NormalUser; import com.lotusmedia.typenormal.TempUser; @Component public class SuperUser { ????private NormalUser normalUser; ????private TempUser tempUser; ????public SuperUser(){ ????????System.out.println("Super User Created ->"+this); ????} ????@Autowired ????public void setNormalUser(NormalUser normalUser) { ????????System.out.println("Setting Normal User ->"+normalUser); ????????this.normalUser = normalUser; ????} ????@Autowired ????public void setTempUser(TempUser tempUser) { ????????System.out.println("Setting Temp User ->"+tempUser); ????????this.tempUser = tempUser; ????} }

NormalUser.java :

1 2 3 4 5 6 7 8 9 10 11 package com.lotusmedia.typenormal; import org.springframework.stereotype.Component; @Component public class NormalUser { ????public NormalUser(){ ????????System.out.println("Normal User Created->"+this); ????} }

TempUser.java :

1 2 3 4 5 6 7 8 9 10 11 package com.lotusmedia.typenormal; import org.springframework.stereotype.Component; @Component public class TempUser { ????public TempUser(){ ????????System.out.println("Temporary User Created->"+this); ????} }

Now when I executed my Executor class again , this time nothing happened no new bean has been created and no properties has been injected. This is what we were expecting right ? as annotation-config is not suffice and can’t register beans marked with @Component annotation. So , here comes the component scan in play , what it will do , it will scan all the packages provided as an argument to it and will register all the beans marked @Component annotation , and once the bean is registered it annotation-config will inject them.

Now you might be thinking why we need annotation-config when we already have component-scan in our context file ,yes you are thinking in right dierction,?as I have already said that component scan can do everything what annotation config does.So, we can safely remove annotation-config from our configuration file and can add component-scan there. So. here is my updated applicationContext file.

1 2 3 4 5 6 7 8 9 10 11 12 <beans xmlns="http://www.springframework.org/schema/beans" ????xmlns:context="http://www.springframework.org/schema/context" ????xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ????xsi:schemaLocation=" ????????http://www.springframework.org/schema/beans???? ????????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ????????http://www.springframework.org/schema/context ????????http://www.springframework.org/schema/context/spring-context-3.0.xsd ???????"> ?????<context:component-scan base-package="com.lotusmedia.typenormal,com.lotusmedia.typesuper"/>? </beans>

Now when i executed my Executor class again I got the below mentioned output as we were expecting , all the 3 beans have been created and been injected properly.

1 2 3 4 5 Super User Created ->com.lotusmedia.typesuper.SuperUser@426295eb Normal User Created->com.lotusmedia.typenormal.NormalUser@207f5580 Setting Normal User ->com.lotusmedia.typenormal.NormalUser@207f5580 Temporary User Created->com.lotusmedia.typenormal.TempUser@4f4db0e3 Setting Temp User ->com.lotusmedia.typenormal.TempUser@4f4db0e3

I hope this example has clarified most of the confusion between annotation-config vs component-scan , if you still have any doubt/question please feel free to drop me a comment, I will be more than happy to answer you.

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/5645906.html

總結(jié)

以上是生活随笔為你收集整理的annotation-config vs component-scan – Spring Core--转的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产在线一区不卡 | 嫩草在线播放 | 欧美性极品xxxx做受 | 性欧美丰满熟妇xxxx性 | 国产精品久久毛片av大全日韩 | 中文字幕无码人妻少妇免费 | 国产福利资源在线 | 亚洲成人xxx| 欧美大胆a视频 | 成人免费看类便视频 | 免费在线亚洲 | 91视频看看 | 禁断介护老人中文字幕 | 国产精品xxxx喷水欧美 | 都市激情麻豆 | 欧美高清videos高潮hd | 一区二区三区天堂 | 国产免费av电影 | 一本到久久 | 国产成人精品一区二区三区在线观看 | 医生强烈淫药h调教小说视频 | 少妇aaaa| 欧美另类视频在线观看 | 天天干夜夜爽 | 91女神在线| 熟妇熟女乱妇乱女网站 | 琪琪午夜伦理影院7777 | 亚洲精品亚洲人成人网 | 91精品视频免费在线观看 | 亚洲欧美中文日韩在线 | jzzjzz日本丰满少妇 | 成年人免费视频网站 | www.av天天 | 在线看片日韩 | 亚洲精品字幕在线 | 性猛交ⅹxxx富婆video | 色狠狠操 | 99久久一区二区 | 韩日免费视频 | 18av在线播放 | 夜色视频网站 | 国产精品扒开腿做爽爽爽视频 | 九九九九久久久久 | 93看片淫黄大片一级 | 韩国成人在线 | 超碰免费在线播放 | 茄子视频懂你更多在线观看 | 无码一区二区三区免费 | 国产精品免费久久久久 | 97干干 | 97人人爽人人爽人人爽人人爽 | 香蕉久久精品日日躁夜夜躁 | 欧美大奶在线 | 天堂一区二区三区四区 | 中文在线资源 | 人禽l交视频在线播放 视频 | 日本大尺度做爰呻吟 | 亚洲色综合 | 老妇free性videosxx| 精品夜夜澡人妻无码av | 激情亚洲天堂 | 在线观看污污视频 | 日本成人在线免费视频 | 最污的网站 | wwwxxx欧美| 日韩国产三级 | 美日韩一区二区 | 男女爽爽 | 国产一二精品 | 探花系列在线观看 | 日产精品久久久久 | 熟妇人妻va精品中文字幕 | 麻豆一区二区 | 亚洲欧美日韩成人在线 | 伊人久久大 | 日韩欧美亚洲国产 | aaaa免费视频 | 久久久久久久久免费看无码 | 亚洲精品77777 | 水蜜桃色314在线观看 | 日韩免费视频一区 | 亚洲一区二区视频网站 | 欧洲精品在线播放 | 国产91精品一区 | n0659极腔濑亚美莉在线播放播放 | 又色又爽又黄18网站 | 午夜在线 | 二级黄色录像 | 日韩精品一区二区在线 | 东北少妇不带套对白 | 色综合天天网 | 波多野结衣黄色网址 | 成人黄色三级视频 | 国产美女引诱水电工 | 91丨国产 | 国产喷白浆一区二区三区 | 无码一区二区三区 | 国产激情视频在线 | 日本不卡免费 |