最近在看《sPRing in Action 4th》,講到javaConfig的@Import和@ImportResource的使用,于是照著例子做了個小demo,加深自己的印象。在Spring中配置有xml和JavaConfig的配置方式,相比來說,使用JavaConfig的方式配置會更利于管理,類型安全。
1234567891011121314151617181920212223242526
1234567891011121314
1234567891011121314151617181920
12345678910111213@Configuration注解表示定義一個配置類,這里使用注解@Bean就好比如xml配置時的<bean>元素,如:<bean id="cdPlayer" class="com.jiaobuchong.soundsystem.CDPlayer"> <property name="cd" ref="compactDisc" /></bean>123
123@ImportResource類似于xml配置時的:<import resource="cons-injecxml" />1
1
123456
123456789101112131415161718192021222324
123456789101112131415161718
123456789101112
1234567891011121314151617181920212223
1234567891011121314151617181920212223242526272829
123456789101112131415161718192021222324252627
1234567891011121314151617181920212223242526272829303132333435看完這個demo代碼,基本上@Import和ImportResource的意義應該也就明白了,更多細節請參考《Spring in Action 4th》。
新聞熱點
疑難解答