<?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"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <! - chinese configuration example, which implementing class is a Chinese -> <bean id="chinese" class="org.crazyit.app.service.impl.Chinese"> <! - The following is only a constructor- Arg child element, Spring Chinese driver calls the constructor with a parameter to create objects -> <constructor-arg ref="steelAxe" type="org.crazyit.app.service.Axe"/> </bean> <! - stoneAxe configuration example, which is the implementation class StoneAxe -> <bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/> <! - steelAxe configuration example, which is the implementation class SteelAxe -> <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/> </beans>
package lee; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.crazyit.app.service. * ; / ** * Description: * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * Author YeekuHLee kongyeeku163com * @Version 1.0 * / public class BeanTest { public static void main(String[] args)throws Exception { // Create a container Spring ApplicationContext ctx = new new ClassPathXmlApplicationContext ( "beans.xml" ); // get chinese instance of the Person = the p-ctx.getBean ( "chinese", the Person. Class ); // call useAxe () method p.useAxe ( ); } }
package org.crazyit.app.service; /** * Description: * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * Author YeekuHLee kongyeeku163com * @version 1.0 */ public interface Axe { // Ax Interface has had a chop () method public String chop (); }
package org.crazyit.app.service; /** * Description: * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * Author YeekuHLee kongyeeku163com * @version 1.0 */ public interface Person { // define a method of using the ax public void useAxe (); }
package org.crazyit.app.service.impl; import org.crazyit.app.service. * ; / ** * Description: * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * Author YeekuHLee kongyeeku163com * @version 1.0 */ public class Chinese implements Person { Private Ax Ax; // configuration parameters required injection constructor public Chinese (Ax Ax) { this.axe = axe; } // implement Person interface useAxe () method public void useAxe () { // call axe the chop () method // show axe Person object depends on the object System.out.println (axe.chop ()); } }
package org.crazyit.app.service.impl; import org.crazyit.app.service. * ; / ** * Description: * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * Author YeekuHLee kongyeeku163com * @version 1.0 */ public class SteelAxe implements Axe { public String chop() { return "Gang Fu firewood really fast" ; } }
package org.crazyit.app.service.impl; import org.crazyit.app.service. * ; / ** * Description: * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * Author YeekuHLee kongyeeku163com * @version 1.0 */ public class StoneAxe implements Axe { public String chop() { return "very slow axes chopping wood" ; } }