« Previous day (Apr 19, 2008) | Main | Next day (Apr 21, 2008) »
http://developers.sun.com.cn/blog/functionalca/date/20080420 星期日 2008年04月20日

Hello Spring (第 2 部分)

NetBeans IDE 6.1 新增了对 Spring 的支持,其好处之一便是可以对它进行扩展。使用 Spring 框架的一个非常简单的入口点就是它的专用 MIME 类型 x-springconfig+xml。通过此 MIME 类型,您可以在编辑器或资源管理器视图的节点中添加新菜单项目,尤其是针对 Spring 配置文件。使用该技巧,我添加了一个名称为 "Generate Java from Beans" 的菜单项:

选择该菜单项时会发生什么呢?我们在文件末尾生成了一些 Java 代码,用于访问各个 Bean。随后,您可以根据需要将它们复制到自己的 Java 类中。将它集成到 Java 类中可能会比较好,这样 bean 就可以出现在代码完成中。但这些操作对我来说要求还比较高,我还需要研究一段时日。目前,我只支持 util:list bean,但是可以方便地将它扩展到所有其他类型。也就是说,选择上面的菜单项之后,IDE 将生成所有注释掉的内容,如下所示:

参见 CookieAction 中的相关 performAction。需要特别注意 org.openide.xml.XMLUtil,它确实是非常有用的(即便您没有意识到这点):

protected void performAction(Node[] activatedNodes) {

//Figure out the name of the configuration file,
//plus its packages, plus "src",
//which is what the org.springframework.core.io.FileSystemResource class needs:

DataObject dobj = activatedNodes[0].getLookup().lookup(DataObject.class);
FileObject fo = dobj.getPrimaryFile();
setFileName(fo.getPath().substring(fo.getPath().indexOf("src")));

try {
//Figure out the document and parse it:
EditorCookie editorCookie = activatedNodes[0].getLookup().lookup(EditorCookie.class);
StyledDocument styledDoc = editorCookie.openDocument();
String allText = styledDoc.getText(0, styledDoc.getLength());
InputSource source = new InputSource(new StringReader(allText));
//No validation, not namespace aware, no entity resolver, no error handler:
Document doc = XMLUtil.parse(source, false, false, null, null);

//Figure out the list of elements:
NodeList list = doc.getElementsByTagName("*");
int docLength = list.getLength();
for (int i = 0; i < docLength; i++) {
org.w3c.dom.Node node = list.item(i);

//Figure out the list of attributes:
NamedNodeMap map = node.getAttributes();
int mapLength = map.getLength();
for (int j = 0; j < mapLength; j++) {
org.w3c.dom.Node attr = map.item(j);

//Insert the template, with values filled in,
//if the attribute is "id":

if (attr.getNodeName().equals("id")) {
setBeanId(attr.getNodeValue());
styledDoc.insertString(styledDoc.getLength(),
"\n<!-- How to access the \"" + attr.getNodeValue() +
"\" bean:\n" + getTemplate(), null);
}

}
}
} catch (SAXException ex) {
Exceptions.printStackTrace(ex);
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}

}

另外,模板如下所示:

private String getTemplate() {
template =
"\nBeanFactory factory = new XmlBeanFactory(new FileSystemResource(\"" + getFileName() + "\"));" +
"\nArrayList list = (ArrayList) factory.getBean(\""+getBeanId()+"\");" +
"\nIterator it = list.iterator();" +
"\nint count = 0;" +
"\nwhile (it.hasNext()) {" +
"\n count = count + 1;" +
"\n System.out.println(\""+getBeanId()+" \" + count + \": \" + it.next().toString());" +
"\n}" +
"\n-->\n";
return template;
}

注:本篇文章翻译自 Geertjan's Weblog。原文地址:http://blogs.sun.com/geertjan/entry/hello_spring_part_2

    
    

 



发表于 jerry [JavaSE] ( 四月 20, 2008 01:14 下午 ) Permalink | 评论[0]

Hello Spring (第 1 部分)

我第一次结识 Spring 框架是通过它的 Util 模式。我一直使用 NetBeans IDE 6.1 Beta,因此这一版本的 NetBeans IDE 支持一组专门针对 Spring 的特性,这是史无前例的。它不仅提供了对 Web 应用程序的支持(我将在未来的博客文章中讨论这一点),还提供了对 Java SE 应用程序的支持。下面是我在 Java SE 应用程序中创建的第一个配置文件:

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-2.5.xsd">
           
    <util:list id="emails">
        <value>john@smith.org</value>
        <value>jack@harry.org</value>
        <value>peter@piper.org</value>
        <value>pavel@prochazka.org</value>
    </util:list>
    
</beans>

在编写上述文件时,上下文相关的代码完成功能为我提供了帮助:

使用新模板创建它:

我需要选择一个或多个名称空间,这样便完全不用考虑Spring配置文件的报头:

我使用下面这个简单的 Java 类访问(和使用)上述 Spring 配置文件:

 

package hellospring;

import java.util.ArrayList;
import java.util.Iterator;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class Main {

    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src/hellospring/demo.xml"));
        ArrayList list = (ArrayList) factory.getBean("emails");
        Iterator it = list.iterator();
        int count = 0;
        while (it.hasNext()) {
            count = count + 1;
            System.out.println("Email " + count + ": " + it.next().toString());
        }
    }
    
}

我还能够在 Spring 配置文件中使用 Java 代码完成功能,特别是针对类的属性:

然后,我可以单击类引用,这将在编辑器中打开该类。

 

最后,我可以组织自己的 Spring 文件。在 Java SE 应用程序的 Project Properties 节点中,当 Spring JAR 位于类路径中时会出现一个新节点,用于组合 Spring 配置文件。

 

最后,有关新 Spring 支持的更多信息,请参阅 Ramon Ramos 的博客文章 Improved Spring Framework Support in NetBeans 6.1: XML-Config Files

注:本篇文章翻译自 Geertjan's Weblog。原文地址:http://blogs.sun.com/geertjan/entry/hello_spring



发表于 jerry [JavaSE] ( 四月 20, 2008 12:58 下午 ) Permalink | 评论[1]