<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 下面配置相当于如下代码: FactoryBean factory = new org.crazyit.app.factory.GetFieldFactoryBean(); factory.setTargetClass("java.awt.BorderLayout"); factory.setTargetField("NORTH"); north = factory.getObject(); --> <bean id="north" class="org.crazyit.app.factory.GetFieldFactoryBean"> <property name="targetClass" value="java.awt.BorderLayout"/> <property name="targetField" value="NORTH"/> </bean> <!-- 下面配置相当于如下代码: FactoryBean factory = new org.crazyit.app.factory.GetFieldFactoryBean(); factory.setTargetClass("java.sql.ResultSet"); factory.setTargetField("TYPE_SCROLL_SENSITIVE"); north = factory.getObject(); --> <bean id="theValue" class="org.crazyit.app.factory.GetFieldFactoryBean"> <property name="targetClass" value="java.sql.ResultSet"/> <property name="targetField" value="TYPE_SCROLL_SENSITIVE"/> </bean> </beans>
package lee; import org.springframework.context.*; import org.springframework.context.support.*; /** * 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)throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 下面2行代码获取的FactoryBean的产品 System.out.println(ctx.getBean("north")); System.out.println(ctx.getBean("theValue")); // 下面代码可获取的FactoryBean本身 System.out.println(ctx.getBean("&theValue")); } }
package org.crazyit.app.factory; import java.lang.reflect.*; import org.springframework.beans.factory.FactoryBean; /** * 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 GetFieldFactoryBean implements FactoryBean<Object> { private String targetClass; private String targetField; // targetClass的setter方法 public void setTargetClass(String targetClass) { this.targetClass = targetClass; } // targetField的setter方法 public void setTargetField(String targetField) { this.targetField = targetField; } // 返回工厂Bean所生产的产品 public Object getObject() throws Exception { Class<?> clazz = Class.forName(targetClass); Field field = clazz.getField(targetField); return field.get(null); } // 获取工厂Bean所生产的产品的类型 public Class<? extends Object> getObjectType() { return Object.class; } // 返回该工厂Bean所生成的产品是否为单例 public boolean isSingleton() { return false; } }