list和set方式基本一样,没做实例,主要是list和map方式,同时试验了一下自动装配属性。自动装备有五种方式,主要是byName和byType这样更直接。 但今后项目不打算启用自动装配。
package com.springstudy.vo;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Customer {
List<Order> lstOrds;
//赠送产品只能对应一个订单
Map<String,Order> mapOrds;
//品类-代金券
Properties props;
public List<Order> getLstOrds() {
return lstOrds;
}
public void setLstOrds(List<Order> lstOrds) {
this.lstOrds = lstOrds;
}
public Map<String, Order> getMapOrds() {
return mapOrds;
}
public void setMapOrds(Map<String, Order> mapOrds) {
this.mapOrds = mapOrds;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
}
本次使用util命名空间发现,不用夹杂版本号了,以前后面总是跟随版本号,实际上这很不利于升级。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="order" class="com.springstudy.vo.Order">
<property name="id" value="1"></property>
</bean>
<bean id="order2" class="com.springstudy.vo.Order">
<property name="id" value="2"></property>
</bean>
<!-- 借助util命名空间,配置一个公用的list集合 ,util中还提供了map和set等-->
<util:list id="lstOrds">
<bean class="com.springstudy.vo.Order">
<property name="id" value="10"></property>
</bean>
<bean class="com.springstudy.vo.Order">
<property name="id" value="11"></property>
</bean>
<bean class="com.springstudy.vo.Order">
<property name="id" value="14"></property>
</bean>
<ref bean="order" />
</util:list>
<!--自动装配的主要方式是byName和byType,但从本源上来说,如果一个也没匹配上,那么就报告异常,如下采用byName方式,
而且确定稳定的行为可能更实际,所以自动装配更适合小范围使用,毕竟稳定压倒一切捷径方式,所以这个属性项目不采用。
-->
<bean id="customer2" class="com.springstudy.vo.Customer" autowire="byName">
</bean>
<!-- 展示了list,map,properties配置方式 -->
<bean id="customer" class="com.springstudy.vo.Customer">
<property name="lstOrds">
<list>
<ref bean="order" />
<bean class="com.springstudy.vo.Order">
<property name="id" value="3"></property>
</bean>
</list>
</property>
<property name="mapOrds">
<map>
<entry key="100000971956" value-ref="order"></entry>
<entry key="2912105">
<ref bean="order2" />
</entry>
<entry key="291210599">
<bean class="com.springstudy.vo.Order">
<property name="id" value="3"></property>
</bean>
</entry>
</map>
</property>
<property name="props">
<props>
<prop key="Cat10598">485118-546548-2198765-879845</prop>
<prop key="Cat10508">895118-556548-2198765-109845</prop>
</props>
</property>
</bean>
</beans>
之下是基本测试:只是回显看看结果:
package com.springstudy.test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springstudy.vo.Customer;
class CustomerTest {
static ApplicationContext ctx;
Customer springCustomer=ctx.getBean("customer", Customer.class);
Customer springCustomer2=ctx.getBean("customer2", Customer.class);
@BeforeAll
static void setUpBeforeClass() throws Exception {
ctx = new ClassPathXmlApplicationContext("PCBU01.xml");
}
@Test
void test() {
springCustomer.getLstOrds().forEach(x->System.out.println(x.getId()));
springCustomer.getMapOrds().forEach((x,y)->System.out.println(x+"->"+y.getId()));
springCustomer.getProps().forEach((x,y)->System.out.println(x+"->"+y));
springCustomer2.getLstOrds().forEach(x->System.out.println(x.getId()));
}
}
1
3
100000971956->1
2912105->2
291210599->3
Cat10598->485118-546548-2198765-879845
Cat10508->895118-556548-2198765-109845
10
11
14
1