最近在看《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》。
新闻热点
疑难解答