一.创建项目
项目名称:spring092901
二.添加jar包
commons-logging.jar
junit-4.4.jar
log4j.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
三.添加配置文件
1.在项目中创建conf目录
/conf
2.在conf目录下添加配置文件
配置文件名称: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"
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/util http://www.springframework.org/schema/util/spring-util.xsd">
</beans>
四.创建实体bean
1.在src下创建包
包名:cn.jbit.spring092901.domain
2.在包下创建bean
bean名称:CountryGeneralSituation.java
bean内容:
public class CountryGeneralSituation {
private String countryName;//国家名称
private String womb;//发源地
private String relic;//遗址
//省略get and set
}
五.创建业务bean
1.在src下创建包
包名:cn.jbit.spring092901.collection
2.在包下创建bean
bean名称:ArrayFromRef.java
bean内容:
public class ArrayFromRef {
private CountryGeneralSituation[] conCountryGeneralSituations;
public CountryGeneralSituation[] getConCountryGeneralSituations() {
return conCountryGeneralSituations;
}
public void setConCountryGeneralSituations(
CountryGeneralSituation[] conCountryGeneralSituations) {
this.conCountryGeneralSituations = conCountryGeneralSituations;
}
}
3.在核心配置文件中配置bean
<bean id="chinaBean" class="cn.jbit.spring092901.domain.CountryGeneralSituation">
<property name="countryName" value="中国"></property>
<property name="relic" value="黄河流域、长江流域"></property>
<property name="womb" value="大地湾遗址、良渚遗址、陶寺遗址"></property>
</bean>
<bean id="arrayFromBean" class="cn.jbit.spring092901.collection.ArrayFromRef">
<property name="conCountryGeneralSituations">
<ref bean="chinaBean"/>
</property>
</bean>
六.测试
1.在项目中创建test目录
/test
2.在test目录下创建包
cn.jbit.spring092901.collection
3.在包下 创建测试类
类名:ArrayFromRefTest.java
类内容:
public class ArrayFromRefTest {
@Test
public void testCGS(){
ClassPathXmlApplicationContext cxac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ArrayFromRef afr = (ArrayFromRef) cxac.getBean("arrayFromBean");
CountryGeneralSituation[] cgss = afr.getConCountryGeneralSituations();
for (CountryGeneralSituation countryGeneralSituation : cgss) {
System.out.println(countryGeneralSituation.getCountryName());
}
}
项目名称:spring092901
二.添加jar包
commons-logging.jar
junit-4.4.jar
log4j.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
三.添加配置文件
1.在项目中创建conf目录
/conf
2.在conf目录下添加配置文件
配置文件名称: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"
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/util http://www.springframework.org/schema/util/spring-util.xsd">
</beans>
四.创建实体bean
1.在src下创建包
包名:cn.jbit.spring092901.domain
2.在包下创建bean
bean名称:CountryGeneralSituation.java
bean内容:
public class CountryGeneralSituation {
private String countryName;//国家名称
private String womb;//发源地
private String relic;//遗址
//省略get and set
}
五.创建业务bean
1.在src下创建包
包名:cn.jbit.spring092901.collection
2.在包下创建bean
bean名称:ArrayFromRef.java
bean内容:
public class ArrayFromRef {
private CountryGeneralSituation[] conCountryGeneralSituations;
public CountryGeneralSituation[] getConCountryGeneralSituations() {
return conCountryGeneralSituations;
}
public void setConCountryGeneralSituations(
CountryGeneralSituation[] conCountryGeneralSituations) {
this.conCountryGeneralSituations = conCountryGeneralSituations;
}
}
3.在核心配置文件中配置bean
<bean id="chinaBean" class="cn.jbit.spring092901.domain.CountryGeneralSituation">
<property name="countryName" value="中国"></property>
<property name="relic" value="黄河流域、长江流域"></property>
<property name="womb" value="大地湾遗址、良渚遗址、陶寺遗址"></property>
</bean>
<bean id="arrayFromBean" class="cn.jbit.spring092901.collection.ArrayFromRef">
<property name="conCountryGeneralSituations">
<ref bean="chinaBean"/>
</property>
</bean>
六.测试
1.在项目中创建test目录
/test
2.在test目录下创建包
cn.jbit.spring092901.collection
3.在包下 创建测试类
类名:ArrayFromRefTest.java
类内容:
public class ArrayFromRefTest {
@Test
public void testCGS(){
ClassPathXmlApplicationContext cxac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ArrayFromRef afr = (ArrayFromRef) cxac.getBean("arrayFromBean");
CountryGeneralSituation[] cgss = afr.getConCountryGeneralSituations();
for (CountryGeneralSituation countryGeneralSituation : cgss) {
System.out.println(countryGeneralSituation.getCountryName());
}
}
}\
本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1559518