<context:annotation-config> & <context:component-scan>

单词:

        annotation  凝视

        [ænə'teɪʃ(ə)n]

        component 组件


正式開始解说<context:annotation-config> 用(1)取代 与 <context:component-scan> 用(2)取代:

            (1)用来激活已经在spring容器里注冊过的bean上面的凝视(即在application.xml中配置的<bean/> 标签中的类)。

       比如: 在spring容器中注冊testA和testB两个类,<bean id="testA" class="com.text.testA" />

                                                                                           <bean id="testA" class="com.text.testB" />,

       然后使用凝视@Autowired在testA类中注入testB类, @Autowired

                                                                                                     private testB bbbb;

            (2)不仅能够激活已经在spring容器中注冊过的bean上面的凝视。还能够注冊package指定的包中的凝视类(

       @component, @controler, @service...凝视的类会被自己主动注冊到spring容器中)。


       比如:在spring容器中不配置<bean>,而是配置<context:component-scan base-package="com.text.testA,com.test.testB" />

       在testA类和testB类的上面加上凝视@component或@service或@controler。


假设(1)和(2)同一时候存在。那么spring容器会忽略(1),由于(2)的功能包括(1),保证不反复想spring注冊同样类。

posted @ 2017-04-28 08:03  jzdwajue  阅读(80)  评论(0)    收藏  举报