环境搭建:eclipse 4.6.0版本, 安装对应版本的插件springsource-tool-suite-3.8.1.RELEASE-e4.6-updatesite.zip
jar包:
测试类:
package com.cwh.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args){
//创建spring的IOC容器(此时会调bean的无参构造器)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从IOC容器获取bean实例
//1.按bean的Id来获取bean实例
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
//2.按bean的类型来获取bean实例(前提是配置文件只有一个bean是这个类型,所以这里获得的跟上一行的对象是同一个)
HelloWorld helloWorld2 = context.getBean(HelloWorld.class);
// System.out.println(helloWorld);
ClassPathXmlApplicationContext context2 = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context2.getBean("studentDetail");
System.out.println(student);
}
}
applicationContext:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
配置bean
通过set方法注入bean的属性值,实际开发中最常用的方法
实体类需要有set方法,还有无参构造器,因为此处的property个数不确定,可能是0
-->
<bean id="helloWorld" class="com.cwh.spring.beans.HelloWorld">
<property name="name" value="wh"></property>
</bean>
<!--
通过构造器注入(实体类要有相应的构造器)
可以参照构造器形参顺序直接输入value,也指定index或者type。也可以此来区分不同的构造器
-->
<bean id="student" class="com.cwh.spring.beans.Student">
<constructor-arg value="180" index="2"></constructor-arg>
<constructor-arg value="cwh" type="java.lang.String"></constructor-arg>
<constructor-arg value="18" index="0"></constructor-arg>
</bean>
<bean id="studentDetail" class="com.cwh.spring.beans.Student">
<constructor-arg value="180"></constructor-arg>
<constructor-arg value="cwh"></constructor-arg>
<constructor-arg value="18"></constructor-arg>
<constructor-arg value="110"></constructor-arg>
<!-- 引用外部bean
<constructor-arg ref="detail"></constructor-arg>
-->
<!-- 也可以创建内部bean -->
<constructor-arg name="personDetail" >
<bean class="com.cwh.spring.beans.PersonDetail">
<property name="location" value="wuhe"></property>
<!-- <property name="score" value="99"></property> -->
</bean>
</constructor-arg>
<!-- 此处可以跟已经初始化的级联属性赋值,必须是已经初始化的,否则报错 -->
<property name="personDetail.score" value="100"></property>
</bean>
<bean id="detail" class="com.cwh.spring.beans.PersonDetail">
<property name="location" value="wuhe"></property>
<property name="score" value="99"></property>
</bean>
</beans>
applicationContext2:
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 此处作为父bean,抽象成不可被IOC容器实例化的抽象bean,只能用于继承(可以不写class属性)。
也可去掉abstract写成实例化的普通bean(class属性必须有) -->
<bean id="detail" class="com.cwh.spring.collection.PersonDetail" abstract="true">
<property name="location" value="wh"></property>
<property name="score" value="98"></property>
</bean>
<!-- 此处的子bean,继承父bean的属性 。如果此处重新设定字段的值,会覆盖父bean的值-->
<bean id="detail1" parent="detail">
<property name="score" value="100"></property>
</bean>
<bean id="detail2" parent="detail">
</bean>
<!-- util命名空间 bean外部集合写法 -->
<util:map id="map1">
<entry key="d1" value-ref ="detail1"></entry>
<entry key="d2" value-ref="detail2"></entry>
</util:map>
<util:list id="list1">
<ref bean="detail1"/>
<ref bean="detail2"/>
</util:list>
<!-- 配置map集合 -->
<!--
scope:作用域,singleton是单例,容器只有一个这样的实例
prototype是原型的,每次创建对象,容器都会实例化一个新的
-->
<bean id="student2" class="com.cwh.spring.collection.Student2" scope="prototype">
<property name="name" value="yoyo"></property>
<property name="age" value="24"></property>
<property name="personDetail" >
<map>
<entry key="d1" value-ref ="detail1"></entry>
<entry key="d2" value-ref="detail2"></entry>
</map>
</property>
</bean>
<!-- 配置list集合 -->
<bean id="student" class="com.cwh.spring.collection.Student">
<property name="hight" value="180"></property>
<property name="name" value="cwh"></property>
<property name="age" value="18"></property>
<property name="weight" value="110"></property>
<property name="personDetails">
<list>
<ref bean="detail1"/>
<ref bean="detail2"/>
</list>
</property>
</bean>
<!-- 配置properties -->
<bean id="dataSource" class="com.cwh.spring.collection.DataSource">
<property name="properties">
<props>
<prop key="userName">root</prop>
<prop key="passWord">chenwenhuan</prop>
<prop key="jdbcUrl">testUrl</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
<!-- p命名空间,比propert便捷 -->
<bean id="student3" class="com.cwh.spring.collection.Student2" p:age="20" p:name="cc" p:personDetail-ref="map1"></bean>
<!-- 依赖,依赖的bean,会在本bean初始化前,会被先初始化。如果没有容器没有这个bean,会报错(此处依赖并不会注入值) -->
<bean id="student5" class="com.cwh.spring.collection.Student2" p:age="20" p:name="bb" depends-on="map1"></bean>
<!--引用bean外部集合写法 -->
<bean id="student4" class="com.cwh.spring.collection.Student">
<property name="hight" value="180"></property>
<property name="name" value="cwh"></property>
<property name="age" value="18"></property>
<property name="weight" value="110"></property>
<property name="personDetails" ref="list1">
</property>
</bean>
</beans>
实体类:
package com.cwh.spring.collection;
import java.util.List;
public class Student {
private int hight;
private String name;
private int age;
private double weight;
private List<PersonDetail> personDetails;
public int getHight() {
return hight;
}
public void setHight(int hight) {
this.hight = hight;
}
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 double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Student(int hight, String name, double weight) {
super();
this.hight = hight;
this.name = name;
this.weight = weight;
}
public Student() {
}
public List<PersonDetail> getPersonDetails() {
return personDetails;
}
public void setPersonDetails(List<PersonDetail> personDetails) {
this.personDetails = personDetails;
}
[@Override](https://my.oschina.net/u/1162528)
public String toString() {
return "Student [hight=" + hight + ", name=" + name + ", age=" + age + ", weight=" + weight + ", personDetails="
+ personDetails + "]";
}
}
package com.cwh.spring.collection;
import java.util.Map;
public class Student2 {
private String name;
private int age;
private Map<String, Object> personDetail;
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 Map<String, Object> getPersonDetail() {
return personDetail;
}
public void setPersonDetail(Map<String, Object> personDetail) {
this.personDetail = personDetail;
}
[@Override](https://my.oschina.net/u/1162528)
public String toString() {
return "Student2 [name=" + name + ", age=" + age + ", personDetail=" + personDetail + "]";
}
}
package com.cwh.spring.collection;
public class PersonDetail {
private String location;
private String score;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getScore() {
return score;
}
public void setScore(String scort) {
this.score = scort;
}
[@Override](https://my.oschina.net/u/1162528)
public String toString() {
return "PersonDetail [location=" + location + ", scort=" + score + "]";
}
}
package com.cwh.spring.collection;
import java.util.Properties;
public class DataSource {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
[@Override](https://my.oschina.net/u/1162528)
public String toString() {
return "DataSource [properties=" + properties + "]";
}
}