📄 ioc-bean.xtp
字号:
package example;import java.util.*;public class MessageBean { List _msgs = new LinkedList(); public void addMessage(Message msg) { _msgs.add(msg); System.out.println("MessageBean.addMessage(): " + msg); } // this never get's called, because MessageBean has no parent public void setParent(Object obj) { System.out.println("MessageBean.setParent(): " + obj); }}</example><example title="Message.java">package example;public class Message { String _text; public void addText(String text) { _text = text; System.out.println("Message.addText(): " + text); } public void setParent(Object obj) { System.out.println("Message.setParent(): " + obj); } public String toString() { return super.toString() + ": " + _text; }}</example><example title="Message.java">package example;public class CustomMessage extends Message { public void addText(String text) { _text = text; System.out.println("CustomMessage.addText(): " + text); } public void setParent(Object obj) { System.out.println("CustomMessage.setParent(): " + obj); }}</example><results>Message.setParent(): example.MessageBean@ffb35eMessage.addText(): This is message 1MessageBean.addMessage(): example.Message@1591b4d: This is message 1Message.setParent(): example.MessageBean@ffb35eMessage.addText(): This is message 2MessageBean.addMessage(): example.Message@10f965e: This is message 2CustomMessage.setParent(): example.MessageBean@ffb35eCustomMessage.addText(): This is message 3MessageBean.addMessage(): example.CustomMessage@12164ea: This is message 3</results><p>In practice, it may make more sense to use <code>createSubBean</code> or<code>addSubBean</code> to set a parent-child relationship for beans, insteadof <code>setParent</code>. The possible issue with addSubBean is that@PostConstruct methods are called before addSubBean. The possible issue withcreateSubBean is that it'snot possible to use a resin:type with createSubBean. So the setParent isuseful when the @PostConstruct method needs the parent, and you need to useresin:type.</p></s2></s1><s1 name="xml" title="Configuring beans from XML files"><p>The facilities provided by Resin make it very easy to read XML files. Java classes that follow the java bean pattern are defined to match the schema of the xml file.</p><example title="rss-example.xml"><rss version="0.91"> <channel> <title>Hogwarts</title> <link>http://hogwarts.com</link> <description>Hogwart's News</description> <image> <title>Hogwarts</title> <url>http://hogwarts.com/images/logo.gif</url> <link>http://hogwarts.com</link> <width>88</width> <height>31</height> <description>Hogwart's News</description> </image> <item> <title>New Course Additions</title> <link>http://hogwarts.com/news/00123.html</link> <description>New course's are now available at Hogwart's.</description> </item> <item> <title>Dumbledore is back!</title> <link>http://hogwarts.com/news/00122.html</link> <description> After a short hiatus, Professor Dumbledore is back as Headmaster of Hogwart's. </description> </item> </channel></rss></example><example title="example/rss/Rss.java">package example.rss;import java.util.ArrayList;public class Rss { private String _version; private ArrayList<Channel> _channels = new ArrayList<Channel>; public void setVersion(String version) { _version = version; } public String getVersion() { return _version; } public void addChannel(Channel channel) { _channels.add(channel); } public ArrayList<Channel> getChannels() { return _channels; }}</example><example title="example/rss/Channel.java">package example.rss;import java.util.ArrayList;public class Channel { private String _title; private String _link; private String _description; private String _language; private Image _image; private ArrayList<Item> _items = new ArrayList<Item>; public void setTitle(String title) { _title = title; } public String getTitle() { return _title; } public void setLink(String link) { _link = link; } public String getLink() { return _link; } public void setDescription(String description) { _description = description; } public String getDescription() { return _description; } public void setImage(Image image) { _image = image; } public Image getImage() { return _image; } public void addItem(Item item) { _items.add(item); } public ArrayList<Items> getItems() { return _items; }}</example><example title="example/rss/Image.java">package example.rss;public class Image { private String _title; private String _url; private String _link; private int _width; private String _height; private String _description; public void setTitle(String title) { _title = title; } public String getTitle() { return _title; } public void setLink(String link) { _link = link; } public String getLink() { return _link; } public void setWidth(int width) { _width = width; } public int getWidth() { return _width; } public void setHeigth(int height) { _height = height; } public int getHeight() { return _height; } public void setDescription(String description) { _description = description; } public String getDescription() { return _description; }}</example><example title="example/rss/Item.java">package example.rss;public class Item { private String _title; private String _link; private String _description; public void setTitle(String title) { _title = title; } public String getTitle() { return _title; } public void setLink(String link) { _link = link; } public String getLink() { return _link; } public void setDescription(String description) { _description = description; } public String getDescription() { return _description; }}</example><s2 title="NodeBuilder"><p><a href="javadoc|com.caucho.config.NodeBuilder|"/> is used to configure beans from an xml file.</p><example title="NodeBuilder constructs beans from rss-example.xml"> import com.caucho.config.NodeBuilder; import com.caucho.vfs.Path; ... Rss rss = new Rss(); NodeBuilder builder = new NodeBuilder(); Path rssPath = new Path("WEB-INF/rss-example.xml"); builder.configure(rss,rssPath);</example></s2><s2 title="RNC validation"><example title="RNC validation"> import com.caucho.config.NodeBuilder; import com.caucho.vfs.Path; ... Rss rss = new Rss(); NodeBuilder builder = new NodeBuilder(); Path rssPath = new Path("WEB-INF/rss-example.xml"); <b>Path schemaPath = new Path("WEB-INF/rss.rnc")</b> <b>builder.setCompactSchema(schemaPath);</b> builder.configure(rss,rssPath);</example></s2><s2 name="import" title="resin:import"><p>resin:import is used to read configuration information from another file.</p><example title="rss-example.xml"><rss version="0.91"> <resin:import> <fileset dir="channels/"> <include name="*.xml"/> </fileset> </resin:import></example><example title="channels/hogwarts.xml"><channel> <title>Hogwarts</title> <link>http://hogwarts.com</link> <description>Hogwart's News</description> <image> <title>Hogwarts</title> <url>http://hogwarts.com/images/logo.gif</url> <link>http://hogwarts.com</link> <width>88</width> <height>31</height> <description>Hogwart's News</description> </image> <item> <title>New Course Additions</title> <link>http://hogwarts.com/news/00123.html</link> <description>New course's are now available at Hogwart's.</description> </item> <item> <title>Dumbledore is back!</title> <link>http://hogwarts.com/news/00122.html</link> <description> After a short hiatus, Professor Dumbledore is back as Headmaster of Hogwart's. </description> </item></channel></example><s3 title="See also"><ul><li><a href="config-control.xtp#resin:import">resin:import</a></li><li><a href="../examples/ioc-injection/index.xtp">Dependency Injection Tutorial</a></li></ul></s3></s2> <!-- import --></s1> <!-- xml --></body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -