<?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.SpringTest" 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" 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"> <!-- 下面配置相当于如下Java代码: JFrame win = new JFrame("我的窗口"); win.setVisible(true); --> <bean id="win" class="javax.swing.JFrame"> <constructor-arg value="我的窗口" type="java.lang.String"/> <property name="visible" value="true"/> </bean> <!-- 下面配置相当于如下Java代码: JTextArea jta = JTextArea(7, 40); --> <bean id="jta" class="javax.swing.JTextArea"> <constructor-arg value="7" type="int"/> <constructor-arg value="40" type="int"/> </bean> <!-- 使用MethodInvokingFactoryBean驱动Spring调用普通方法 下面配置相当于如下Java代码: win.add(new JScrollPane(jta)); --> <bean class= "org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="win"/> <property name="targetMethod" value="add"/> <property name="arguments"> <list> <bean class="javax.swing.JScrollPane"> <constructor-arg ref="jta"/> </bean> </list> </property> </bean> <!-- 下面配置相当于如下Java代码: JPanel jp = new JPanel(); --> <bean id="jp" class="javax.swing.JPanel"/> <!-- 使用MethodInvokingFactoryBean驱动Spring调用普通方法 下面配置相当于如下Java代码: win.add(jp , BorderLayout.SOUTH); --> <bean class= "org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="win"/> <property name="targetMethod" value="add"/> <property name="arguments"> <list> <ref bean="jp"/> <util:constant static-field="java.awt.BorderLayout.SOUTH"/> </list> </property> </bean> <!-- 下面配置相当于如下Java代码: JButton jb1 = new JButton("确定"); --> <bean id="jb1" class="javax.swing.JButton"> <constructor-arg value="确定" type="java.lang.String"/> </bean> <!-- 使用MethodInvokingFactoryBean驱动Spring调用普通方法 下面配置相当于如下Java代码: jp.add(jb1); --> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="jp"/> <property name="targetMethod" value="add"/> <property name="arguments"> <list> <ref bean="jb1"/> </list> </property> </bean> <!-- 下面配置相当于如下Java代码: JButton jb2 = new JButton("取消"); --> <bean id="jb2" class="javax.swing.JButton"> <constructor-arg value="取消" type="java.lang.String"/> </bean> <!-- 使用MethodInvokingFactoryBean驱动Spring调用普通方法 下面配置相当于如下Java代码: jp.add(jb2); --> <bean class= "org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="jp"/> <property name="targetMethod" value="add"/> <property name="arguments"> <list> <ref bean="jb2"/> </list> </property> </bean> <!-- 使用MethodInvokingFactoryBean驱动Spring调用普通方法 下面配置相当于如下Java代码: win.pack(); --> <bean class= "org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="win"/> <property name="targetMethod" value="pack"/> </bean> </beans>
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 SpringTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); } }
package org.crazyit.app.util; /** * 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 ValueGenerator { public int getValue() { return 2; } public static int getStaticValue() { return 9; } }
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 int age; private Son son; //age属性的setter和getter方法 public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } //son属性的setter和getter方法 public void setSon(Son son) { this.son = son; } public Son getSon() { return this.son; } }
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 Son { private int age; //age属性的setter和getter方法 public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } }
来源:https://www.cnblogs.com/tszr/p/12371925.html