【applicationContext.xml】
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- scope属性,表示创建对象的方式,
值:prototype表示获取的是多实例 ,每次执行一次,都会创建一个新的对象
一旦换成 singleton 表示成一个单实例,只有唯一的一个实例,内存地址不变
-->
<bean id="per" class="com.demo.dao.Person"></bean>
<bean id="dao" class="com.demo.dao.PersonDAOImpl">
<property name="name" value="张三"></property>
<property name="age" value="23"></property>
<property name="pc" >
<!-- Person pc= new com.demo.dao.Person() -->
<bean class="com.demo.dao.Person"></bean>
</property>
<property name="list">
<list>
<bean class="com.demo.dao.PersonDAOImpl"></bean>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
<value>5</value>
</list>
</property>
<property name="obj">
<list>
<value>112</value>
<value>120</value>
<value>114</value>
<value>113</value>
</list>
</property>
<property name="set">
<set>
<value>5</value>
<value>6</value>
<value>5</value>
<value>6</value>
<ref bean="per"/>
</set>
</property>
<property name="map">
<map>
<entry key="key01" value-ref="per"></entry>
<entry key="key02" value="12"></entry>
<entry key="key03">
<value>13</value>
</entry>
</map>
</property>
<property name="prop">
<props>
<prop key="prop1">123</prop>
<prop key="prop2">1234</prop>
</props>
</property>
<property name="listNull">
<null></null>
</property>
<!--
Person per=new Person();
person=per;
-->
<property name="person" ref="per"></property>
</bean>
</beans>
-------------------------------------
【ReadApplicationContext.java】
public class ReadApplicationContext {
public static void main(String[] args) {
//spring 配置文件的路径,从类路径下加载
String url="applicationContext.xml";
//String url1="ioc/applicationContext.xml";
//读取spring 的配置文件,获取唯一对象名
ApplicationContext act= new ClassPathXmlApplicationContext(url);
//ClassPathXmlApplicationContext act= new ClassPathXmlApplicationContext(url);
//从spring容器中获取boy 的对象
PersonDAO dao1=(PersonDAO)act.getBean("dao");
dao1.save();
}
}
--------------------------------------
【PersonDAOImpl.java】
public class PersonDAOImpl implements PersonDAO {
//属性
private String name;
private Integer age;
private Person person;
private Person pc;
//设置集合
private Set set;
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
private List list;
private Object[] obj;
private Map map;
private Properties prop;
//list给空值
private List listNull;
public void save(){
System.out.println("我要 insert 数据--------");
System.out.println("名字是:"+this.name+"年龄是:"+this.age);
if(pc!=null){
//spring 自己使用了bean标签创建了一个对象,假设名字叫a 然后spring 自己讲创建的对象a
//传递给了内部需要的属性
System.out.println("spring 使用自己的方式创建了bean对象,但是使用的是spring内部创建对象的方式"+pc.getPname()+"==="+pc.getPtel());
}
if(person!=null){
System.out.println(person.getPname()+"==="+person.getPtel());
}
System.out.println("========集合类,数组,属性类");
if(list.size()>0){
for (int i = 0; i < list.size(); i++) {
System.out.println("list:"+list.get(i));
}
}
if(obj.length>0){
for (int i = 0; i <obj.length; i++) {
System.out.println("obj数组:"+obj[i]);
}
}
if(map!=null){
System.out.println("map对象:"+map.get("key01"));
System.out.println(map.get("key02"));
}
if(set!=null && set.size()>0){
Iterator it=set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
if(prop!=null){
System.out.println("prop1:"+prop.getProperty("prop1"));
System.out.println(prop.getProperty("prop2"));
}
System.out.println("listNUll的 空值:"+listNull);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Person getPc() {
return pc;
}
public void setPc(Person pc) {
this.pc = pc;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Object[] getObj() {
return obj;
}
public void setObj(Object[] obj) {
this.obj = obj;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
public List getListNull() {
return listNull;
}
public void setListNull(List listNull) {
this.listNull = listNull;
}
}
-----------------------------------