1.P名称空间注入(少用):
代码很简单:
public class SteClass {
private String nameSet;
public void setNameSet(String nameSet) {
this.nameSet = nameSet;
}
public void testSet(){
System.out.println("setTest===="+nameSet);
}
}
xmlns:p="http://www.springframework.org/schema/p"
<bean id="name" class="ioc.SteClass" p:nameSet="小谢"></bean>
@Test
public void test4(){
ApplicationContext con=new ClassPathXmlApplicationContext("mapper/spring.xml");
SteClass us=(SteClass) con.getBean("name");
us.testSet();
}
2. 数组:
3. list集合:
4.map集合:
5.properties 《键值对》类型:
代码如下
public class Person {
private String[]arras;
private List<String> list;
private Map<String,String> map;
private Properties properties;
public void setArras(String[] arras) {
this.arras = arras;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void test1(){
System.out.println("arras=="+arras);
System.out.println("arras=="+arras[0]);
System.out.println("map=="+map);
System.out.println("list=="+list);
System.out.println("properties=="+properties);
}
<!-- 注入复杂属性值 -->
<bean id="persion" class="ioc.Person">
<!-- 数组 -->
<property name="arras" >
<list>
<value>小谢</value>
<value>小猫</value>
<value>小猪</value>
<value>小狗</value>
</list>
</property>
<!-- 集合 -->
<property name="list" >
<list>
<value>老谢</value>
<value>老猫</value>
<value>老猪</value>
<value>老狗</value>
</list>
</property>
<!-- map -->
<property name="map" >
<map>
<entry key="1" value="小鸡"></entry>
<entry key="2" value="小谢"></entry>
<entry key="3" value="小鸭"></entry>
<entry key="4" value="小虫"></entry>
</map>
</property>
<!-- properties-->
<property name="properties">
<props>
<prop key="driverclass">class.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
</props>
</property>
</bean>
@Test
public void test5(){
ApplicationContext con=new ClassPathXmlApplicationContext("mapper/spring.xml");
Person us=(Person) con.getBean("persion");
us.test1();
}