设值注入(推荐)
1 2 3 4
| <bean id="myService" class="com.zane.test.MyServiceImpl"> <property name="serializer" ref="Serializer"/> <property name="httpService" ref="httpService"/> </bean>
|
构造器注入(死的应用)
1 2 3 4
| <bean id="myModel" class="com.zane.test.MyModel"> <constructor-arg index="0" value="${name}"/> <constructor-arg index="1" value=“20"/> </bean>
|
注入List
1 2 3 4 5 6 7 8 9
| <bean id="myTypes" class="java.util.ArrayList"> <constructor-arg> <list> <value type="com.zane.test.MyType">A</value> <value type="com.zane.test.MyType">B</value> <value type="com.zane.test.MyType">C</value> </list> </constructor-arg> </bean>
|
注入Map
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <bean id="myTypeValueMap" class="java.util.HashMap"> <constructor-arg> <map> <entry key="#{T(com.zane.test.MyType).A}"> <value type="java.lang.Integer">3</value> </entry> <entry key="#{T(com.zane.test.MyType).B}"> <value type="java.lang.Integer">4</value> </entry> <entry key="#{T(com.zane.test.MyType).C}"> <value type="java.lang.Integer">5</value> </entry> </map> </constructor-arg> </bean>
|
当注入的是第三方的jar包的key类型时,需要使用@Resource注入
1 2 3
| @Resource @Qualifier("myTypeValueMap") private Map<MyType, String> myTypeValueMap;
|
否则使用Autowired即可
1 2 3
| @Autowired @Qualifier("myTypeValueMap") private Map<MyType, String> myTypeValueMap;
|