<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/common-logging"/> <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Sprin4.0.4"/> <classpathentry kind="output" path="bin"/> </classpath>
<?xml version="1.0" encoding="UTF-8"?> <projectDescription> <name>myspring</name> <comment></comment> <projects> </projects> <buildSpec> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription>
<?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"> <!-- 配置名为person的Bean,其实现类是org.crazyit.app.service.Person类 --> <bean id="person" class="org.crazyit.app.service.Person"> <!-- 控制调用setAxe()方法,将容器中axe Bean作为传入参数 --> <property name="axe" ref="axe"/> </bean> <!-- 配置名为axe的Bean,其实现类是org.crazyit.app.service.Axe类 --> <bean id="axe" class="org.crazyit.app.service.Axe"/> <!-- 配置名为win的Bean,其实现类是javax.swing.JFrame类 --> <bean id="win" class="javax.swing.JFrame"/> <!-- 配置名为date的Bean,其实现类是java.util.Date类 --> <bean id="date" class="java.util.Date"/> </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 kongyeeku@163.com * @version 1.0 */ public class BeanTest { public static void main(String[] args)throws Exception { // 创建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 获取id为person的Bean Person p = ctx.getBean("person" , Person.class); // 调用useAxe()方法 p.useAxe(); } }
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 class Axe { public String chop() { return "使用斧头砍柴"; } }
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 class Person { private Axe axe; // 设值注入所需的setter方法 public void setAxe(Axe axe) { this.axe = axe; } public void useAxe() { System.out.println("我打算去砍点柴火!"); // 调用axe的chop()方法, // 表明Person对象依赖于axe对象 System.out.println(axe.chop()); } }
来源:https://www.cnblogs.com/tszr/p/12370284.html