2/21/2008

The First Seasar2 Example

At first, I show the very simple example to use S2Container. I created following 3 files into "jp.sf.amateras.seasar.example" package.

example.dicon

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN"
          "http://www.seasar.org/dtd/components24.dtd">
<components>
  <component class="java.util.ArrayList" name="list">
    <initMethod name="add"><arg>"Naoki Takezoe"</arg></initMethod>
  </component>
</components>
<components>
  <component class="jp.sf.amateras.seasar.example.Hello" name="hello">
    <property name="target">list</property>
  </component>
</components> 

Hello.java

package jp.sf.amateras.seasar.example;
import java.util.List;

public class Hello {
 private List target;
 public void setTarget(List target){
   this.target = target;
 }
 public void sayHello(){
   for(int i=0;i<target.size();i++){
     System.out.println("Hello, " + target.get(i));
   }
 }
}

Main.java

package jp.sf.amateras.seasar.example;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;

public class Main {
 public static void main(String[] args){
   S2Container container = S2ContainerFactory.create(
              "jp/sf/amateras/seasar/example/example.dicon");
   Hello hello = (Hello)container.getComponent("hello");
   hello.sayHello();
 }
}

Run the "Main" class as a Java application, it displays "Hello, Naoki Takezoe".

2 件のコメント:

Nguyễn Hồ Phương さんのコメント...

no display hello

匿名 さんのコメント...

edit file example.dicon :


"Naoki Takezoe"


list



--> This sample work fine :)