<?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"?> <- - root element Spring configuration file, use the spring-beans-4.0.xsd semantic constraints!> <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 is a Chinese-based implementation class -> <the bean ID =" chinese " class =" org.crazyit .app.service.impl.Chinese "> <-! driver calls the chinese setAxe () method, passing as a parameter the container stoneAxe -> <Property name =" Ax "= REF"stoneAxe "/> </ the bean> <-! stoneAxe configuration example, which is the implementation class StoneAxe -> <the bean ID = "stoneAxe"class= "org.crazyit.app.service.impl.StoneAxe" /> <-! steelAxe configuration example, which is the implementation class SteelAxe -> <the 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/>网站: <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 [email protected] * @version 1.0 {BeanTestclasspublic* / public static void main (String [] args) throws Exception { // create Spring container the ApplicationContext CTX = new new the ClassPathXmlApplicationContext ( "the beans.xml" ); // Get chinese instance the Person ctx.getBean P = ( "chinese", the Person. class ); // call useAxe () method p.useAxe (); } }
Package Penalty for org.crazyit.app.service; / ** * the Description: * the website: <a href=" http://www.crazyit.org "> crazy Java league </a> * the Copyright (C), 2001-2016, Yeeku.H.Lee * This Program IS protected by a Copyright Laws. * a Program the Name: * a a Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 * / public interface Ax { // Ax interfaces, there are a method of cut public String CHOP (); }
Package Penalty for org.crazyit.app.service; / ** * the Description: * the website: <a href=" http://www.crazyit.org "> crazy Java league </a> * the Copyright (C), 2001-2016, Yeeku.H.Lee * This Program IS protected by a Copyright Laws. * a Program the Name: * a a Date: * @author Yeeku.H.Lee [email protected] * @version 1.0 * / public interface the Person { // definition of a method of using the ax 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 [email protected] * @version 1.0 */ public class Chinese implements Person { private Axe axe; // 设值注入所需的setter方法 public void setAxe (Ax Ax) { the this .axe = Ax; } // implemented method of Person interface useAxe public void useAxe () { // call Ax of CHOP () method, // show Ax Person object depends on the object System.out .println (axe.chop ()); } }
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 [email protected] * @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 [email protected] * @version 1.0 */ public class StoneAxe implements Axe { public String chop() { return "石斧砍柴好慢"; } }