<!-- create the task definition -->
<taskdef name="runbean" classpathref="testpath"
classname="jbetancourt.ant.task.spring.springcontexttask"/>
<target name="simpleappcontextusewithdefaults">
<runbean beanlocations="applicationcontext.xml"></runbean>
</target>
<target name="publish">
<spring
beanlocations="applicationcontext.xml"
beanname="sitegenerator"
methodname="generatesite"
host="${host.site.url}"
port="${site.port}">
made a few tweaks. removed some sentence fragments.
</spring>
</target>
call="generatesite("${host.site.url}","${site.port}")"
or better:
<methodcall><![cdata[ generatesite("${host.site.url}","${site.port}") ]]></methodcall> <target name="publish">
<spring beanlocations="applicationcontext.xml" beanname="sitegenerator">
<methodcall> generatesite("${host.site.url}","${site.port}") </methodcall>
made a few tweaks. removed some sentence fragments.
</spring>
</target>
<spring id="metrics" beanlocations="metricscontext.xml" beanname="main"
exampleattribute="a value" and so forth . . ./>
<target name="computemetrics">
<spring refid="metrics" call="computencss"/>
<spring refid="metrics" call="computeccm"/>
<spring refid="metrics" call="findbugs"/>
</target>
<target name="gendocs">
<!- here are calls to other types of docs '/>
<!- now call the metric docs '/>
<spring refid="metrics" call="createdocs"/>
</target>
<target name="computemetrics">
<spring beanlocations="metricscontext.xml" beanname="computencss"/>
<spring beanlocations="metricscontext.xml" beanname="computeccm"/>
<spring beanlocations="metricscontext.xml" beanname="findbugs"/>
</target>
<methodcall> <![cdata[ notifydeveloper(names(${dev}) ]]> </methodcall> /*
* springcontexttask.java
* created on jan 9, 2005
* creator: josef betancourt
* project: springcontexttask
* -----------------------------------------------------------------------------
*
* copyright 2005 by josef betancourt
*
* licensed under the apache license, version 2.0 (the "license");
* you may not use this file except in compliance with the license.
* you may obtain a copy of the license at
*
* http://www.apache.org/licenses/license-2.0
*
* unless required by applicable law or agreed to in writing, software
* distributed under the license is distributed on an "as is" basis,
* without warranties or conditions of any kind, either express or implied.
* see the license for the specific language governing permissions and
* limitations under the license.
*
* -----------------------------------------------------------------------------
*
*
*/
package jbetancourt.ant.task.spring;
import java.util.enumeration;
import java.util.properties;
import jbetancourt.ant.task.abstractcontexttask;
import org.apache.tools.ant.buildexception;
import org.apache.tools.ant.project;
import org.springframework.beans.beansexception;
import org.springframework.beans.factory.config.propertyoverrideconfigurer;
import org.springframework.context.applicationcontext;
import org.springframework.context.configurableapplicationcontext;
import org.springframework.context.support.filesystemxmlapplicationcontext;
/**
*
* ant task extension that invokes a pojo within a spring application
* context, a "springdef".
*
* tested with ant 1.6.2, jdk 1.4.2, spring 1.1.4, and ognl 2.6.7.
*
* this task is added to ant with a taskdef or one of the new ant 1.6+
* approaches.
*
*
*
* example taskdef:
* <taskdef name="springtask"
* classname="jbetancourt.springcontexttask"
* classpathref="taskdefclasspath">
*
* it is then used simply by accepting the defaults as:
* <target name="test1">
* <springtask beanlocations="applicationcontext.xml">
* </springtask></target>
*
* a more complex use is:
* <target name="publishbean">
* <springtask
* beanlocations="applicationcontext.xml"
* beanname="sitegenerator"
* host="${host.site.url}"
* port="${site.port}"
* methodname="generatesite">
* in-line site post change text
* </springtask>
* </target>
*
*
* use of methodcall element with cdata expression.
*
* <target name="test8">
* <springtask beanlocations="applicationcontext.xml"
* beanname="antbean1">
* <methodcall><![cdata[execute("goodbye")]]>
* </methodcall>
* </springtask>
* </target>
*
*
*
*
*
* @author jbetancourt
* @since jan 9, 2005
*
*/
public class springcontexttask extends abstractcontexttask {
/** runtime spring context that that will be set or created */
private applicationcontext applicationcontext;
/**
*
* get the container managed bean.
*
* @param beanname
* @return the pojo, singleton or non-singleton.
*/
public object getbeanfromcontainer(string beanname) throws buildexception {
try {
if(applicationcontext == null){
applicationcontext = new filesystemxmlapplicationcontext(
getbeanlocations().list());
}
return applicationcontext.getbean(getbeanname());
} catch (beansexception e) {
throw new buildexception("failure: could not get bean '" +
getbeanname() + "' from context",e);
}
}
/**
* invoke target bean setters with ant specified dynamic properties.
*
*/
public void insertmanagedbeanproperties(){
// how to programmatically post process a spring bean?
// see spring forum thread for source of this approach.
// http://forum.springframework.org/viewtopic.php?p=11833#11833
propertyoverrideconfigurer poc = new propertyoverrideconfigurer();
// the keys must be of form 'beanname.key'.
// create a new properties with this format.
properties props = addkeyprefix(getdynamicproperties(), getbeanname());
poc.setproperties(props);
((configurableapplicationcontext)applicationcontext).
addbeanfactorypostprocessor(poc);
((configurableapplicationcontext)applicationcontext).refresh();
}
/**
* for each key in props, convert to 'beanname.key' format.
*
* @return with keys modified
* @throws buildexception
*/
private properties addkeyprefix(properties initprops,
string prefix) throws buildexception {
// todo: this seems like a wrong approach here.
// can the passed in props be manipulated instead?
properties props = new properties();
for (enumeration en = initprops.propertynames();
en.hasmoreelements();) {
string key = (string) en.nextelement();
// let ant resolve any property replacements.
string resolvedtext = getproject().replaceproperties(initprops.getproperty(key));
props.put(prefix + "." + key, resolvedtext);
}
return props;
}
/**
* get the spring context that was created or set.
* @return could be null
*/
public applicationcontext getapplicationcontext() {
return applicationcontext;
}
/**
*
*
* @param applicationcontext non-null
* the applicationcontext to set.
*/
public void setapplicationcontext(applicationcontext applicationcontext) {
log("setting applicationcontext: " + applicationcontext,
project.msg_debug);
this.applicationcontext = applicationcontext;
}
/* (non-javadoc)
* @see jbetancourt.ant.task.externalcontainer#createcontainer()
*/
public void createcontainer() throws buildexception {
try {
if(applicationcontext == null){
applicationcontext = new filesystemxmlapplicationcontext(
getbeanlocations().list());
}
} catch (beansexception e) {
throw new buildexception("failure: could not create spring container.",e);
}
}
}
public void test2(){
executetarget("test2");
}
public void test3(){
expectbuildexception("test3", "beanlocation or class must be specified");
} <target name="test3"> <!-- no context path or class specified; should fail -->
<springtask methodname="length"/></target>
<bean id="antbean1" class="jbetancourt.testbean1"/>
<bean id="beanwithprops" class="jbetancourt.testbean1"/>
<bean id="main" class="org.springframework.beans.factory.
config.methodinvokingfactorybean">
<property name="targetobject"><ref local="antbean"/></property>
<property name="targetmethod"><value>execute<
/value></property>
</bean>
<target name="test18">
<springtask beanlocations="etc/contexts/applicationcontext.xml"
beanname="postbugreport" methodname="doservice"></springtask>
</target>
bean定义如下:
<bean id="securityinterceptor" class="jbetancourt.securityinterceptor">
</bean>
<bean id="postbugreport" class="org.springframework.aop.framework.proxyfactorybean">
<property name="target"><ref local="antbean"/></property>
<property name="proxyinterfaces">
<list>
<value>jbetancourt.isecurity</value>
</list>
</property>
<property name="interceptornames">
<list>
<value>securityinterceptor</value>
</list>
</property>
</bean>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器