bean1的简单属性:
<span style="font-family:SimHei;font-size:18px;">package com.sxt.domain;
import java.util.*;
public class Bean1 {
private String name;
private int age;
private String[]attr;
private List<String>lists;
private Set<String>sets;
private Map<String,String>maps;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getAttr() {
return attr;
}
public void setAttr(String[] attr) {
this.attr = attr;
}
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
public Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
}
</span>
配置文件:
<span style="font-family:SimHei;font-size:18px;"><?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="bean1" class="com.sxt.domain.Bean1">
<property name="name" value="wangli"></property>
<property name="age" value="23"></property>
<property name="attr">
<list>
<value>attr1</value>
<value>attr2</value>
</list>
</property>
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
</list>
</property>
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
<property name="maps">
<map>
<entry key="key1" value="map1"></entry>
<entry key="key2" value="map1"></entry>
</map>
</property>
</bean>
</beans>
</span>
2 spring的singleton和prototype的作用域
<bean id="bean1" class="com.sxt.domain.Bean1" scope="prototype">
......
</bean>
Bean测试结果:
bean1=(Bean1)factory.getBean("bean1");
Bean1 bean11=(Bean1)factory.getBean("bean1");
System.out.println(bean1==bean11); //false
<bean id="bean1" class="com.sxt.domain.Bean1" scope="singleton">
......
</bean>
bean1=(Bean1)factory.getBean("bean1");
Bean1 bean11=(Bean1)factory.getBean("bean1");
System.out.println(bean1==bean11); //true
默认是singleton范围。
3 spring的自动装配:
default-autowire="byType" 会按照bean中的类型来装配对象
default-autowire="byName" 会按照bean中的名字来装配对象