<?xml version="1.0" encoding="GBK"?>
<project name="spring" basedir="." default="">
<property name="src" value="src"/>
<property name="dest" value="classes"/>
<path id="classpath">
<fileset dir="../../lib">
<include name="**/*.jar"/>
</fileset>
<pathelement path="${dest}"/>
</path>
<target name="compile" description="Compile all source code">
<delete dir="${dest}"/>
<mkdir dir="${dest}"/>
<copy todir="${dest}">
<fileset dir="${src}">
<exclude name="**/*.java"/>
</fileset>
</copy>
<javac destdir="${dest}" debug="true" includeantruntime="yes"
deprecation="false" optimize="false" failonerror="true">
<src path="${src}"/>
<classpath refid="classpath"/>
<compilerarg value="-Xlint:deprecation"/>
</javac>
</target>
<target name="run" description="Run the main class" depends="compile">
<java classname="lee.BeanTest" fork="yes" failonerror="true">
<classpath refid="classpath"/>
</java>
</target>
</project>
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的根元素和Schema
导入p:命名空间和util:命名空间的元素 -->
<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-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置chinese实例,其实现类是Chinese -->
<bean id="chinese" class="org.crazyit.app.service.impl.Chinese"
p:age-ref="chin.age" p:axe-ref="stoneAxe"
p:schools-ref="chin.schools"
p:axes-ref="chin.axes"
p:scores-ref="chin.scores"/>
<!-- 使用util:constant将指定类的静态Field定义成容器中的Bean -->
<util:constant id="chin.age" static-field=
"java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
<!-- 使用util.properties加载指定资源文件 -->
<util:properties id="confTest"
location="classpath:test_zh_CN.properties"/>
<!-- 使用util:list定义一个List集合,指定使用LinkedList作为实现类,
如果不指定默认使用ArrayList作为实现类 -->
<util:list id="chin.schools" list-class="java.util.LinkedList">
<!-- 每个value、ref、bean...配置一个List元素 -->
<value>小学</value>
<value>中学</value>
<value>大学</value>
</util:list>
<!-- 使用util:set定义一个Set集合,指定使用HashSet作为实现类,
如果不指定默认使用HashSet作为实现类-->
<util:set id="chin.axes" set-class="java.util.HashSet">
<!-- 每个value、ref、bean...配置一个Set元素 -->
<value>字符串</value>
<bean class="org.crazyit.app.service.impl.SteelAxe"/>
<ref bean="stoneAxe"/>
</util:set>
<!-- 使用util:map定义一个Map集合,指定使用TreeMap作为实现类,
如果不指定默认使用HashMap作为实现类 -->
<util:map id="chin.scores" map-class="java.util.TreeMap">
<entry key="数学" value="87"/>
<entry key="英语" value="89"/>
<entry key="语文" value="82"/>
</util:map>
<!-- 配置steelAxe实例,其实现类是SteelAxe -->
<bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
<!-- 配置stoneAxe实例,其实现类是StoneAxe -->
<bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/>
</beans>
a=轻量级Java EE企业应用实战
b=疯狂Java讲义
a=u8f7bu91cfu7ea7Java EEu4f01u4e1au5e94u7528u5b9eu6218
b=u75afu72c2Javau8bb2u4e49
package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class BeanTest
{
public static void main(String[] args)
{
// 创建Spring容器
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
// 获取chinese实例
Person p = ctx.getBean("chinese" , Person.class);
// 调用useAxe()方法
p.useAxe();
System.out.println(ctx.getBean("confTest"));
}
}
package org.crazyit.app.service;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public interface Axe
{
public String chop();
}
package org.crazyit.app.service;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public interface Person
{
public void useAxe();
}
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class SteelAxe
implements Axe
{
public String chop()
{
return "钢斧砍柴真快";
}
}
package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class StoneAxe
implements Axe
{
public String chop()
{
return "石斧砍柴好慢";
}
}
package org.crazyit.app.service.impl;
import java.util.*;
import org.crazyit.app.service.*;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class Chinese implements Person
{
private Axe axe;
private int age;
private List schools;
private Map scores;
private Set axes;
// axe的setter方法
public void setAxe(Axe axe)
{
this.axe = axe;
}
// age的setter方法
public void setAge(int age)
{
this.age = age;
}
// schools的setter方法
public void setSchools(List schools)
{
this.schools = schools;
}
// scores的setter方法
public void setScores(Map scores)
{
this.scores = scores;
}
// axes的setter方法
public void setAxes(Set axes)
{
this.axes = axes;
}
// 实现Person接口的useAxe()方法
public void useAxe()
{
System.out.println(axe.chop());
System.out.println("age属性值:" + age);
System.out.println(schools);
System.out.println(scores);
System.out.println(axes);
}
}