<?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"> <!-- 如果不加任何特殊的配置,该Bean默认是singleton行为的 --> <bean id="chinese" class="org.crazyit.app.service.Person" lazy-init="true"> <!-- 驱动Spring执行chinese Bean的setTest()方法,以"孙悟空"为传入参数 --> <property name="test" value="孙悟空"/> </bean> </beans>
package lee; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.*; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.support.DefaultListableBeanFactory; 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"); // 搜索类加载路径下的beans.xml文件创建Resource对象 // Resource isr = new ClassPathResource("beans.xml"); // // 创建默认的BeanFactory容器 // DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // // 让默认的BeanFactory容器加载默认的 // new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(isr); } }
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 { public Person() { System.out.println("==正在执行Person无参数的构造器=="); } public void setTest(String name) { System.out.println("正在调用setName()方法,传入参数为:" + name); } }